简体   繁体   English

Chrome扩展程序从后台脚本获取UI元素

[英]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 : 您可以拨打functonmyscript.jspopup.html

popup.js code:

var bg = chrome.extension.getBackgroundPage();
bg.someFunction(stateOfCkeckboxes);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的 Chrome 扩展程序的后台脚本中获取变量? - How to get a variable from my Chrome Extension's background script? 在Chrome扩展程序中从后台页面获取配置到内容脚本 - Get configs from background page to content script in chrome extension Chrome 消息扩展:从注入的脚本到后台 - Chrome message extension : From injected script to background Chrome扩展程序 - 从背景到上下文脚本的Blob - Chrome Extension - Blob from Background to Context script 在 chrome 扩展中,无法从内容脚本向后台脚本发送消息并获得响应 - In a chrome extension, can't send a message from the content script to the background script and get a response 如何在Chrome扩展程序中将参数从后台脚本传递到内容脚本? - How to pass parameter from a background script to content script in chrome extension? 从内容脚本发送消息到后台脚本会破坏chrome扩展 - Sending message from content script to background script breaks chrome extension 将消息从后台脚本发送到内容脚本,然后在Chrome扩展程序中进行响应 - Send message from background script to content script then response in chrome extension Chrome 扩展从执行的脚本安全地通信到后台脚本 - Chrome extension safely communicate from executed script to background script 无法从Chrome扩展程序中的后台脚本向内容脚本发送消息 - unable to send message from background script to content script in chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM