Chrome 打开和关闭的扩展
Chrome extension which toggles On and Off
我正在尝试制作一个可以打开和关闭另一个特定扩展程序的扩展程序。然而,我反复尝试并没有找到使它起作用的方法。
基本上,我的扩展带来了一个带有 on/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
}
您可以在此处查看扩展名 "demo":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);
});
我正在尝试制作一个可以打开和关闭另一个特定扩展程序的扩展程序。然而,我反复尝试并没有找到使它起作用的方法。
基本上,我的扩展带来了一个带有 on/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
}
您可以在此处查看扩展名 "demo":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);
});