[英]Chrome Extension get UI element from background script
I am starting to develop a chrome extension. 我开始开发chrome扩展程序。 In my case I have the following manifest.json: 就我而言,我有以下manifest.json:
{
"name": "mY Notifier",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {
"scripts": [
"myscript.js"
],
"persistent": false
},
"permissions": [
"tabs","alarms"
]
}
So, as you can see i have a default popup html page and a background script. 因此,如您所见,我有一个默认的弹出html页面和一个后台脚本。
I need a Background script because I need to update the icon badge from time to time when the Extension is enabled (not just if the user clicks on the icon). 我需要一个背景脚本,因为启用扩展后,我需要不时更新图标徽章(不仅是用户单击图标时)。 This works fine. 这很好。
In my popup.html
i have a couple of checkboxes. 在我的popup.html
我有几个复选框。 Now in my background script i want to check which of the checkboxes are enabled. 现在,在我的后台脚本中,我想检查启用了哪些复选框。 How can i do this? 我怎样才能做到这一点? I cannot refer to my ui elements in popup.html
using elementById. 我无法使用elementById在popup.html
引用我的ui元素。 How can this be done? 如何才能做到这一点?
You can't access to popup.html
from myscript.js
because popup.html
don't loaded before the click on a button. 您无法从myscript.js
访问popup.html
,因为popup.html
在单击按钮之前未加载。 You can call functon
of myscript.js
from popup.html
: 您可以拨打functon
的myscript.js
从popup.html
:
popup.js code:
var bg = chrome.extension.getBackgroundPage();
bg.someFunction(stateOfCkeckboxes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.