Possible Duplicate:
Port error while changing chrome extension from manifest v1 to v2
Looking to make my first Chrome Extension, but I've ran into a pickle when I started to look at using the option
page's localStorage
with the www.example.com/example.html
's localStorage
. I've looked around for how to pass messages back and forth between the two, with this page on Message Passing, but seems like I'd need to set up the eventListener, and use SendRequest
s in background.html
which isn't allowed in manifest : 2
:
There were warnings when trying to install this extension:
'background_page' requires manifest version of 1 or lower.
edit: you need to make the changes to manifest
as seen here, to use `bac
The Message Passing page talks about creating a Port, but it's unclear on where to put the listener. If I put the first half into the content.js
script, then the other half into options.js
, the listener it only active when the options.html
is open.
So my question is: How do I pass settings I've chosen in options.html
to be usable in my contentscript.js
which manipulates the current webpage?
Here's my entire set up:
background.html
<!doctype html>
<html>
<head>
<title>Background Page</title>
<script src="jquery.min.js"/>
<script src='background.js'/>
</script>
</head>
<body>
</body>
</html>
background.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getLocalStorage")
sendResponse({data: localStorage[request.key]});
else
sendResponse({}); // snub them.
});
contentscript.js
chrome.extension.sendRequest({method: "getLocalStorage", key: "status"}, function(response) {
console.log(response.data);
});
manifest.js
{
"manifest_version": 2,
"name": "Image replacer",
"version": "1.0",
"description": "A script to replace all images in the browser with images from any given Imgur gallery",
"browser_action": {
"name": "Image replace BA",
"icons": ["icon.png"],
"default_icon": "icon.png"
},
"background": {
"page": "background.html"
},
"permissions": ["storage"],
"content_security_policy": "script-src 'self' https://www.imgur.com; object-src 'self'",
"content_scripts": [{
"js": ["jquery.min.js", "contentscript.js"],
"matches": ["http://*/*", "https://*/*"]
}],
"options_page": "options.html"
}