call popup.html javascript function from background page - google-chrome

How to call javascript function in popup.html from background page ( in chrome extensions)?

Unfortunately it is not possible to get a direct reference to the popup page, the main reason is that the page might not be open when you try and call it.
You have to use message passing to pass a message between popups and background pages.
I would ask is there a specific need to have the function in the popup and not have it re-factored into a separate shared file.

Would be best to place that functionality in a Background page instead and within your popup you can do:
var bkg = chrome.extension.getBackgroundPage()
bkg.someBackgroundPageFunction();

Thanks for the contributions. I found this reference which solved the problem.

Related

Is it possible to navigateURL without popping up a browser

I have a project that runs swf files. What i'm trying to do is to navigate to a url in that exact window without popping up a browser.
I've tried this :
navigateToURL(new URLRequest("http://www.google.com"),"_self");
but this always pops up a browser windows.
Thanks
I don't think this behaiviour is customizable. You can try to use ExternalInterface to call JavaScript funtion that will do what you need.

Global popup in jqm

I want to make a global popup, so I can access it from different pages. I was reading about this and the solution was to add it directly to body tag in index.html tag, so I did that and now I can open it from my other page (page in which html this popup is not added) using this code
$("#about-create-new-game-popup" ).enhanceWithin().popup();
setTimeout(function(){
$('#about-create-new-game-popup').popup('open');
}, 2000);
The problem is this popup is shown during the application load as it is added to index.html page. Can someone please tell me what am I doing wrong here. Thanks.
I have had this problem before.
What you have to do is define it as a popup before the page is loaded.
A popup will only show when you open it, but in the beginning when the page is loaded it's just another div that's why it's shown. Define it as a popup as soon as possible and it should be hidden.
simply add this code in the beginning:
$("#about-create-new-game-popup" ).popup();

Open Help web page from Google script HTMLservice interface

I want to put a Help button on an HTMLservice interface. I have tried it directly on the button and also by calling the function below
function helpClicked() {
document.getElementById('payment').value = "Help Clicked...";
window.open("www.google.com")
}
This changes the payment field to validate that the button was clicked but does not open a new browser window with the url. Is there any way to make this happen?
Use a link (a tag) directly with the correct URL instead of a button and use css to make it look like a button (if you need one). You can still hook triggers to the link, but you won't need to do the window.open yourself, therefore avoiding this problem/limitation.

How to load options.html in jQuery-colorbox, upon a context menu item click?

I am adding 'options' to my chrome extension. I wonder how can I user colorbox) from background page or content scripts, when context menu item is clicked.
I have spent more than a day on this without success. Is there anybody who has done this? Any help is appreciated.
While you certainly can't use this from a background page, since colorbox shows UI, you have a few options from a content script:
You could do an XHR to fetch the contents of the background page (chrome.extension.getURL may be helpful) and write the contents of the result into the colorbox.
You could embed an <iframe src="/path/to/options.html"> in your colorbox.
I'm assuming you know how to bind JS functions to context menus? Just in case, the contextMenu docs.
Have you tried either of these options?

mailto link not working in chrome extension popup

This issue drove me nuts for 2 days. I made a simple chrome extension which calls a server-side program that returns HTML that I then stuff into a div in the popup. It was all fine, except for the simple anchor link containing a "mailto:xxx#yyy.com" href. An email message composition window would not pop up.
Workaround: Add target="_blank" attribute
I would like to know why this is necessary.
It might have something to do with extensions running in separate processors from the browser, and therefore a target attribute is needed so that a new tab/window can be opened... there are some websites that don't work when displayed inside extension popups for this reason, because the extension frame won't navigate to certain pages...
I know this is an old question, but I ran into a similar situation. I had to send an email, but I had to do it with a button instead of a link and had to finagle this:
function sendEmail(){
var mail = 'mailto:xxx#yyy.com?subject=Subject&body=Body';
var newWin = window.open(mail);
setTimeout(function(){newWin.close()}, 100);
}
It's not ideal, because it opens a new window that's visible to the user instead of doing it instantly. In fact, my first attempt was this (which works in an HTML file, but doesn't work in my extension):
function sendEmail(){
var mail = 'mailto:xxx#yyy.com?subject=Subject&body=Body';
window.open(mail).close();
}
Not sure why adding a timer makes it work in this instance as opposed to just doing it like in a regular HTML file, but that worked for me so I thought I'd share.