I am building a Chrome Extension. I was wondering if there is a way to remember js variables for a tab even if the page changes. For eg. If I am on example1 and go to example2 on the same tab, I should retain the variables which were set on example1. I don't want to use Chrome Storage.
I can't use localStorage or sessionStorage because chrome has different storages for different domains. What are the ways by which this can be acplished ?
I am building a Chrome Extension. I was wondering if there is a way to remember js variables for a tab even if the page changes. For eg. If I am on example1. and go to example2. on the same tab, I should retain the variables which were set on example1.. I don't want to use Chrome Storage.
I can't use localStorage or sessionStorage because chrome has different storages for different domains. What are the ways by which this can be acplished ?
Share Improve this question asked Jun 21, 2014 at 14:45 user3763053user3763053 6- Use a cookie storing stringified JSON, which has the URL of the page as its key. – Rory McCrossan Commented Jun 21, 2014 at 14:46
- @RoryMcCrossan I guess I can't access cookies which were set on example1., from example2.. – user3763053 Commented Jun 21, 2014 at 14:49
- chrome has different storages for different domains. What are the ways by which this can be acplished Answer is no, local storage for your extension is isolated regardless of any domain – Mr. Alien Commented Jun 21, 2014 at 14:50
- @Mr.Alien I am talking about localStorage, and as far as I know localStorage is different for different domain names. I just rechecked it. – user3763053 Commented Jun 21, 2014 at 14:56
- @user3763053 I've developed extensions, and I've used localstorage as well, look for chrome.storage and you will get my point – Mr. Alien Commented Jun 21, 2014 at 14:56
3 Answers
Reset to default 4Apart from using localStorage( through background page), you can directly save those variables in the background page by messaging from content script to the background page. If those variables are in the extension page(other that content or injected scripts) you can easily save them by chrome.extension.getBackgroundPage().window.variableYouWant
method and access those variables in the content script itself by messaging.
If those variables are in the content script use messaging to send those variables to background page like this:
chrome.runtime.sendMessage(object);
object
could be any Object you might want to send.
on background page have a lisner like this:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
myVariabeInBackground = message.yourObjectProperties;
});
when you require those variables you can use chrome.extension.getBackgroundPage().window.myVariabeInBackground
in your extension page(other than content script) or you can send message from background page to content script by calling chrome.tabs.sendMessage(tabId, messageObject)
in the background page itself and receive at the content script by having a listner like this:
chrome.runtime.onMessage.addListener(
function(request, sender) {}); //request is your sent object.
For localStorage also you can do the same thing to get that stored variable into your content script.
And No, you cant directly access the background page from content script.
PS: variables in background page will reset if extension is reloaded. If you want to use those variables even after reloading the extension use local storage. Extension does not reload on browser actions.
For more details read https://developer.chrome./extensions/messaging
use localstorage. the extension is pletely isolated from the page in function and variable storage. you will need to use localstorage in a background page, so when you change tabs, the variable is never lost.
You should look into chrome.storage
API as an alternative to localStorage
+Messaging.
It is asynchronous pared to localStorage
, but so is Messaging, and Messaging just for variable storage leads to a lot of boilerplate.
See documentation here, and a recent overview (by me) of pros/cons here.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744271747a4566134.html
评论列表(0条)