Before any of the below, last name should be set in localStorage from
the options or background page.
In name.js:
chrome.extension.sendRequest({greeting:"last name"},
function(response) {
// Do something with response.last_name here; for instance:
console.log(response.last_name) // Print last_name to the console
}
);
In background page (not options page):
var last_name = localStorage.getItem('last name'); // or whatever
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "last name") {
sendResponse({"last_name": last_name});
// First last_name is quoted so that bad things don't happen;
// I would assume this is bad practice.
// Using a different name would probably be better. ;-)
} else {
sendResponse({});
// Important! Always send a response,
// even if the request is unexpected!
}
}
);
I think that's about right. There are other examples floating around in
this discussion group if mine doesn't work for you. And feel free to ask
if you don't understand something I did there.
--
Ben
--
Ben
You don't always need a background page to handle options, but you do
in most cases. The reason is -- as Ben hinted at -- when an option
page is closed, it goes out of scope and is no longer capable of
passing messages (or doing anything else).
Content scripts can't execute "chrome.extension.getBackgroundPage"
(for security reasons, they have limited access to the extension API).
--
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.
That's because the alert(x) is outside the callback, where 'x' may not
have been set yet. If you put the alert(x) inside the callback, you
should see that x=response.data .
>
> If what I try to do is wrong, could you please give me some advice or
> tell me how to do that?
>
> to "fix" it I did something like:
>
> chrome.extension.sendRequest({'key' : key}, function (value) {
> localStorage.setItem(key, value);
> });
>
> which works because it is done on body load i'm requesting the keys I
> need and save them to localStorage for later use
> however there should be another way D:
>
--
Ben
--
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.