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.
--
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.
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:
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>
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>
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.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
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
But,in other pages,such like content_script.js,how could i use
variables defined in the background page?How can i get them?
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.
is this a request? or is there a way that you can access functions
from background without chrome.extension.getBackgroundPage() ?