Chrome扩展,可打开和关闭
Chrome extension which toggles On and Off
我正在尝试制作一个扩展,它可以打开和关闭另一个特定的扩展。然而,我反复尝试,没有找到让它工作的方法。
基本上,我的扩展带来了一个带有开/关开关(复选框)的弹出窗口,我需要让它切换另一个扩展的开和关…
这是我的popup.html
<!DOCTYPE html>
<html>
<body>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,700,300' rel='stylesheet' type='text/css'>
<p class="bb">Liga/Desliga do Banco do Brasil</p>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
和popup.js
var bankId = "mkeabchhfifpaaoefpockjhaphjmoapp";
if(document.getElementById("myonoffswitch").checked != true) {
chrome.management.setEnabled(bankId, false);
} else if(document.getElementById("myonoffswitch").checked == true)
{
chrome.management.setEnabled(bankId, true);
}
和manifest.json
{
"browser_action": {
"default_icon": "48.png",
"default_title": "Liga/Desliga o Bando Do Brasil",
"default_popup": "popup.html"
},
"description": "Clique para ativar/desativar a extensão do BB",
"icons": {
"128": "128.png",
"16": "16.png",
"48": "48.png"
},
"name": "Toggle Banco Brasil On/Off",
"permissions": [ "tabs", "management" ],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "1.0",
"manifest_version": 2
}
您可以在此处查看扩展"演示":http://liveweave.com/8M4IvJ
我感谢任何帮助!
让我们看看您的扩展目前是如何工作的。
这是您的弹出页面,加载时默认不选中复选框。
然后,您的代码运行,检查复选框是否选中(未选中),并禁用您指定的扩展。
然后什么都没有。你从未告诉Chrome做任何其他事情。您的代码已经运行完毕,没有事件侦听器,所以现在它是一个静态页面。可能不是你想要的。
现在,让我们来修复它。首先,我想您想将复选框的初始状态设置为扩展的当前状态。很简单:
chrome.management.get(bankId, function(info) {
// Gotta check if we got the info:
if(chrome.runtime.lastError) {
// Something's not right; probably extension is not installed.
// Warn the user somehow? (but no alert(), it can break the popup)
}
document.getElementById("myonoffswitch").checked = info.enabled;
});
接下来,我们不希望在打开弹出窗口时检查状态。相反,我们希望挂接到一个事件,特别是当复选框的值发生变化时。
同样,很简单:
document.getElementById("myonoffswitch").addEventListener("change", function(e) {
// You don't need the conditional if() {} else {}, just use the binary value
chrome.management.setEnabled(bankId, this.checked);
});
相关文章:
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- Chrome扩展 chrome.storage 如何同时进行大量获取和设置并避免竞争条件
- Chrome扩展:Chrome.tabs.executeScript不工作
- 中的访问扩展chrome://extensions页
- 如何在扩展Chrome DevTool时检索请求的启动器
- Chrome扩展-Chrome报警API为创建的每个新报警重新安排所有报警
- 是否有任何方法来扩展chrome's __commandLineAPI
- 谷歌Chrome扩展Chrome .tab . onupdate . addlistener
- 创建在页面上注入脚本的扩展chrome内容
- Chrome扩展:Chrome扩展中未定义的变量,即使它存在于控制台中