[crx] Passing a variable between background and popup document

3,559 views
Skip to first unread message

Boomerang

unread,
May 4, 2010, 2:26:47 AM5/4/10
to Chromium-extensions
I am creating an extension using Page Actions. I know that the
namespaces are different between various scripts that operate within
an extension and that limits variable visibility and scope. I am also
new to writing chrome extensions. I'm trying to convert my extension
to use a popup instead of a window. The window does what I need
reasonably simply. Anyway, I've now got the popup window showing, but
I need to pass in a URL to the popup.html document. Obviously, global
variables are out. When I use chrome.windows.create, I can pass in
the necessary arguments through this function (see example below) and
parse the args coming into the other side of the HTML script. If I'm
using a popup, I don't see a way to pass in any arguments in this same
way.

For my window version, I use:

chrome.pageAction.onClicked.addListener(function(tab) {
chrome.windows.create({url: "chrome-extension://<appid>/
popup.html?" + tab.url, width: 600, height: 600});

With the above, I can pass in the URL from the current tab/script to
the called newly created window/script as part of the URL arguments.

Using a tab, this event is not fired so I can't use this feature to
pass in anything to the popup easily. Defining the popup in the .json
file limits the ability to specifically open the popup.html and pass
in arguments. So, this leaves message passing or some other external
variable storage methodology as the only way to get information into
this popup script from an external script.

With the popup, I've tried message passing between the two HTML
scripts, but I can't seem to get them to communicate. I've tried the
examples verbatim and I've tried to modify them. Again, I can't seem
to pass any information between the scripts using message passing.
Obviously, I must be doing something wrong as message passing doesn't
seem to be that hard to do.

I also thought I might be able to redefine the popup using
chrome.pageAction.setPopup and supply arguments that way.
Unfortunately, I can't even seem redefine the popup without using
arguments much less with. If I set up anything using
chrome.pageAction.setPopup, the popup won't even open. The popup only
seems to open if it's predefined in the manifest.json file.

If there's a simple way to do what I'm trying to do, I seem to be
missing it. Any ideas would be appreciated. I'd really like to move
to using a popup, but I'll stick with a window if there's no easy way
to get information into this script.

I'm probably missing or overlooking something very simple here.
Please help.

Thanks.

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

Arne Roomann-Kurrik

unread,
May 4, 2010, 3:28:26 PM5/4/10
to Boomerang, Chromium-extensions
The popup can access variables on the background page.  For example, if you have the following code in background.html:

    var myURL = 'http://example.com';

The popup can obtain this value using the following code:

    var bg = chrome.extension.getBackgroundPage();
    var myURL = bg.myURL;

You can also call functions in the background page in this way.

Hope this helps,
~Arne

Andrey

unread,
May 5, 2010, 6:11:15 AM5/5/10
to Arne Roomann-Kurrik, Boomerang, Chromium-extensions
/** @constructor */
var MySingleton = function() {
  this.sharedDataBetweenBackgroundWindows_ = { /*...*/ };
};

MySingleton.getInstance = function() {
  var PROP_NAME = '__MySingleton__';
  var bg = chrome.extension.getBackgroundPage();
  return (bg[PROP_NAME] = bg[PROP_NAME] || new MySingleton());
};

MySingleton.prototype.getSharedData() {
  return this.sharedDataBetweenBackgroundWindows_;
};

// ...
Reply all
Reply to author
Forward
0 new messages