想要制作我的第一个 Chrome 扩展程序,但是当我开始考虑将option
页面localStorage
与www.example.com/example.html
's一起使用时,我遇到了麻烦localStorage
。我已经四处寻找如何在两者之间来回传递消息,这个页面在Message Passing上,但似乎我需要设置 eventListener,并使用SendRequest
s in background.html
which is not allowed in manifest : 2
:
There were warnings when trying to install this extension:
'background_page' requires manifest version of 1 or lower.
编辑:您需要对manifest
此处所见的进行更改,以使用 `bac
消息传递页面讨论了创建端口,但不清楚将侦听器放置在哪里。如果我将前半部分放入content.js
脚本中,然后将另一半放入脚本中,options.js
监听器仅在打开时才处于活动状态options.html
。
所以我的问题是:如何传递我选择的设置options.html
以在我的contentscript.js
哪个操作当前网页中使用?
这是我的整个设置:
背景.html
<!doctype html>
<html>
<head>
<title>Background Page</title>
<script src="jquery.min.js"/>
<script src='background.js'/>
</script>
</head>
<body>
</body>
</html>
背景.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getLocalStorage")
sendResponse({data: localStorage[request.key]});
else
sendResponse({}); // snub them.
});
内容脚本.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"
}