[英]Firefox Web extension. Accessing popup content in content script
Directories目录
----MyExtension
|----popup.html
|----popup.js
|----content.js
|----background.js
|----manifest.json
mainfest.json mainfest.json
{
"manifest_version": 2,
...........
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"browser_action": {
"default_title": "Practice",
"default_popup": "popup.html"
},
"permissions": [
"<all_urls>",
"tabs",
"storage",
"activeTab"
],
"background": {
"scripts": ["background.js"]
}
}
....
popup.html弹出窗口.html
<html>
<head>
....
<script src="popup.js"></script>
</head>
<body>
<input id="status" type="chckbox">
</body>
</html>
popup.js弹出窗口.js
$(document).ready(function(){
$on = $("#status");
//sends the settings to background to save it
$on.on("click",function(){
$obj = {"on":$on.prop("checked")}
browser.runtime.sendMessage($obj);
console.log("sending....");
})
})
What im trying to do is simply send a message to background script if the check box in popup.html is checked.如果 popup.html 中的复选框被选中,我想要做的就是简单地向后台脚本发送一条消息。 The problem is I cannot access the browser namespace in popup.js because its not content script or background script.
问题是我无法访问 popup.js 中的浏览器命名空间,因为它不是内容脚本或后台脚本。 And i cannot access the check box from the content scrip as it not linked to popup.html (if its linked i get reference error browser is not defined. I've tried countless google searches and spent hours reading web extension docs still cannot find an answer how to go around it any help appreciated.
而且我无法从内容脚本访问复选框,因为它没有链接到 popup.html(如果它的链接我得到参考错误浏览器未定义。我已经尝试了无数的谷歌搜索并花了几个小时阅读网络扩展文档仍然找不到回答如何解决它任何帮助表示赞赏。
I have good news for you - you can access the browser namespace in your browser action poupus, otherwise they would be pretty useless :)我有个好消息要告诉你——你可以在你的浏览器操作 poupus 中访问浏览器命名空间,否则它们将毫无用处:)
Which means that something else is broken.这意味着别的东西坏了。
First, if you didn't do it yet, open 'browser toolbox' with Ctrl+Shift+Alt+I to see that you probably have a bit different kind of error there.首先,如果您还没有这样做,请使用 Ctrl+Shift+Alt+I 打开“浏览器工具箱”以查看您可能在那里遇到了一些不同类型的错误。
Then include this in your background.js :然后将其包含在您的 background.js 中:
function handleMessage(request, sender, sendResponse) {
console.log("Message from somewhere: ", request);
}
browser.runtime.onMessage.addListener(handleMessage);
To actually read the message.实际阅读邮件。
See:看:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction
JavaScript running in the popup gets access to all the same WebExtension APIs as your background scripts, but its global context is the popup, not the current page displayed in the browser.
在弹出窗口中运行的 JavaScript 可以访问与后台脚本相同的所有 WebExtension API,但其全局上下文是弹出窗口,而不是浏览器中显示的当前页面。 To affect web pages you need to communicate with them via messages.
要影响网页,您需要通过消息与它们通信。
Edit , unrelated:编辑,无关:
$on = $("#status");
You do realize that by doing so you refer to/create a global variable '$on', right?您确实意识到这样做是在引用/创建一个全局变量“$on”,对吗? Did you mean:
你的意思是:
var $on = $("#status");
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.