Question

I'm trying to accessing localStorage in content scripts, so I use Message Passing (manifest v2). But I'm not so clear about one thing : where & when should chrome.extension.onMessage (which is in my options.js) and chrome.extension.sendMessage (in contentscript.js) be invoked?

For me I use jQuery and I wrap both them in the $(document).ready(function() { //... }. But the extension will only run without exceptions when the options page and the injected page are opened at the same time, else it would give a exception like

*Port error: Could not establish connection. Receiving end does not exist.*

I searched Stack Overflow for a while and find that some people use background_page, but I think I just need a options page, not a background page. Is the background html/js really necessary that options page should use them together?

Was it helpful?

Solution

The script in options page are only active when the options page is running. So, when options page is closed it won't be able to receive messages.

You need to use background page. Options page and background page uses the same localStorage, so you can access localStorage in background page without any problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top