Accessing background page from within popups

2,319 views
Skip to first unread message

Pauan

unread,
Mar 1, 2010, 10:47:08 AM3/1/10
to Chromium-extensions
Right now, if you want to access variables or call functions defined
in the background page (from within a popup), you need to either:

1) Send a request to the background page, which will then run the
code.
2) Use chrome.extension.getBackgroundPage();

I think it can be much simpler and more intuitive, however: execute
the popup within the context of the background page. In other words,
if you have the variable "foo" defined in the background page, the
popup can just grab and use that right away, without needing to call
chrome.extension.getBackgroundPage().foo.

Now I'm sure some of you will think, "but having variables that are
exclusive to the popup can be useful!" and you would be correct. But
there is a very simply solution to that: anonymous functions.

(function () {
var bar;
// Only the popup can see the variable bar!
}());

Using the above, you can create variables that only the popup can see.
Now there's no longer any need for
chrome.extension.getBackgroundPage(), since popups can reference
background variables directly, while simultaneously allowing for popup-
only variables.

Erik Kay

unread,
Mar 1, 2010, 6:02:40 PM3/1/10
to Pauan, Chromium-extensions
Is the goal to simply save typing?  I'm not sure I see any other benefits to this.  Just cache the background page at the top of your popup in this case.

var bg = chrome.extension.getBackgroundPage().
console.log(bg.foo);



--
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.


Pauan

unread,
Mar 2, 2010, 12:12:04 AM3/2/10
to Chromium-extensions
There are a few reasons: simplicity, easier to read, more natural and
fitting into the way JavaScript works, less confusing, etc.

In my popup, most of the work is done with variables and functions
defined in the background page. So I end up with stuff like this:

var B = chrome.extension.getBackgroundPage();

B.state.somethingElse = getData();

if (B.state.shouldDoSomething) {
B.actions.doSomethingWith(B.foo);
}

In addition to greatly reducing readability, it's very contrary to the
way JavaScript works. It looks ugly and is complex and confusing.
Also, people have had difficulty using console.log within popups. In
fact, there's a thread right here about this difficulty:

http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/fbf70bc0e6a56bfc/960cb096a205ba1e

Executing the popup within the context of the background page would
solve all of those problems, without any loss of functionality.
Overall, it's a much cleaner, simpler, and better design that causes
less confusion and problems.

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

Pauan

unread,
Mar 2, 2010, 3:00:08 AM3/2/10
to Chromium-extensions
There is further evidence of this confusing behavior. Right now,
extensions have three primary contexts:

1) Background pages, which have elevated privileges but do not
interact directly with the page.
2) Content scripts, which are restricted but do interact directly with
the page.
3) Other pages, which have elevated privileges, do not interact
directly with the page, but must use
chrome.extension.getBackgroundPage() to interact with the background
page.

There is no reason to have three categories like that. They should be
simplified to two:

1) Elevated pages (background page, popups, options, etc.) that are
run in the same context.
2) Content scripts that are restricted and must use message passing to
communicate.

This is much simpler from the extension developer's standpoint. It is
easier to reason about and more intuitive. Instead, we have a clunky
model that separates the background page from other elevated pages
(like popups) for no real reason, adding complexity and going against
developer expectations.

On Mar 1, 3:02 pm, Erik Kay <erik...@chromium.org> wrote:

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

Tim Ker

unread,
Mar 2, 2010, 1:27:34 PM3/2/10
to Chromium-extensions
Currently I like the way the extension system works

I always assumed the pages like popup.html should be treated like
objects.
The background page has state that lasts for the life time of the
browser.
Whereas the popups etc can come and go at any time, also there can be
many popups open at one time (inspecting views),

If these all ran in the same context you would get horrible variable
overlap.
Or popups can directly access background page, but not the other way
around... sounds like some sort of wierd inheritance model and I think
it would be a LOT more confusing to almost all developers.

Pauan

unread,
Mar 2, 2010, 4:49:31 PM3/2/10
to Chromium-extensions
That's why you would use anonymous functions so there's no variable
overlap except where you want it. *Shrug* I guess my JavaScript idiom
is just weird.

PhistucK

unread,
Mar 28, 2010, 2:57:15 AM3/28/10
to Pauan, Chromium-extensions
I also do not agree that your proposed way is better and is more fitting into the way JavaScript works.
For example, when you open a popup page from the same website, they can communicate between one another, but they have no mutual page context. How is having the same page context for several pages fitting into the way JavaScript works?
I think your proposed behavior is really confusing.

☆PhistucK


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Pauan

unread,
Mar 29, 2010, 2:48:21 AM3/29/10
to Chromium-extensions
You are correct that it does not fit in at all with how the DOM works.
The DOM is *very* object-oriented, and my suggestion is functional-
oriented.

At the time I posted my suggestion, I was thinking in a more
functional style, and forgot that most people think in more object-
oriented terms. That was my mistake.

P.S. It fits in with the way JavaScript works because JavaScript
(though it has support for object-orientation) is at it's core a very
functional language. Using lambdas and closures is very natural in
JavaScript, and that is what I suggested to use. This provides a clean
abstraction using core components already available in JavaScript
without the need for a special "chrome.extension.getBackgroundPage()"
function.

But alas, the DOM does not behave that way, and since web developers
use the DOM a lot, it makes sense to cater to their perspective.
Though I do think I had some good ideas here, after considering it
from the perspective of the DOM, I decided that it would be better
implemented in a different system, and that the Chrome way of doing
things (though clunky at times) is more consistent with most
developer's expectations.

With that in mind, I think the current way of doing things is
acceptable, if solely for the sake of developer consistency and
backwards compatibility with older versions of Chrome.

On Mar 27, 11:57 pm, PhistucK <phist...@gmail.com> wrote:
> I also do not agree that your proposed way is better and is more fitting
> into the way JavaScript works.
> For example, when you open a popup page from the same website, they can
> communicate between one another, but they have no mutual page context. How
> is having the same page context for several pages fitting into the way
> JavaScript works?

> I think *your* proposed behavior is really confusing.
>
> ☆PhistucK

T_t

unread,
Mar 29, 2010, 6:15:02 AM3/29/10
to Chromium-extensions
But,in other pages,such like content_script.js,how could i use
variables defined in the background page?How can i get them?

Antony Sargent

unread,
Mar 29, 2010, 1:43:18 PM3/29/10
to T_t, Chromium-extensions
But,in other pages,such like content_script.js,how could i use
variables defined in the background page?How can i get them?

You can't use them directly because regular pages and the content scripts operating on them live in a separate process from the background/popup pages. Instead, you need to use messaging:

jay

unread,
Apr 7, 2010, 2:16:01 AM4/7/10
to Chromium-extensions
is this a request? or is there a way that you can access functions
from background without chrome.extension.getBackgroundPage() ?


On Mar 1, 11:47 am, Pauan <pcxunlimi...@gmail.com> wrote:
> Right now, if you want to access variables or call functions defined

> in thebackgroundpage (from within apopup), you need to either:
>
> 1) Send a request to thebackgroundpage, which will then run the


> code.
> 2) Use chrome.extension.getBackgroundPage();
>
> I think it can be much simpler and more intuitive, however: execute

> thepopupwithin the context of thebackgroundpage. In other words,
> if you have the variable "foo" defined in thebackgroundpage, thepopupcan just grab and use that right away, without needing to call


> chrome.extension.getBackgroundPage().foo.
>
> Now I'm sure some of you will think, "but having variables that are

> exclusive to thepopupcan be useful!" and you would be correct. But


> there is a very simply solution to that: anonymous functions.
>
> (function () {
>     var bar;

>     // Only thepopupcan see the variable bar!
>
> }());
>
> Using the above, you can create variables that only thepopupcan see.

Antony Sargent

unread,
Apr 7, 2010, 12:46:58 PM4/7/10
to jay, Chromium-extensions
On Tue, Apr 6, 2010 at 11:16 PM, jay <lil...@gmail.com> wrote:
is this a request? or is there a way that you can access functions
from background without chrome.extension.getBackgroundPage() ?

No, you need to use chrome.extension.getBackgroundPage() if you want to access the javascript context of the background page from another one of your extension pages (popup, options, etc.).
 
Reply all
Reply to author
Forward
0 new messages