我即将完成扩展程序,我只需要在popup.html中打开/关闭开关即可控制“ chrome:// extensions”标签中的同一开关。有可能吗?
这是我所做的: popup.html:
// on/off switch
<div class="center custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">PAUSE / START</label>
</div>
popup.js:
var ss = document.getElementById("customSwitch1");
$(ss).click(function() {
if($(ss).prop("checked")){
chrome.runtime.sendMessage({isOn: "true"}, function(response) {
console.log(response.farewell);
});
}
else{
chrome.runtime.sendMessage({isOn: "false"}, function(response) {
console.log(response.state);
});
}
});
background.js:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.isOn == "true")
sendResponse({state: "true"});
if (request.isOn == "false")
sendResponse({state: "false"});
});
答案 0 :(得分:1)
您可以在Chrome中启用扩展程序,并让弹出窗口控制扩展程序仅在暂停/开始时起作用,而不禁用整个扩展程序。
答案 1 :(得分:1)
您可以潜在地从代码中禁用自己的扩展名。实际上,您甚至可以将其卸载。
有关如何操作,请参见chrome.management
API。
但是,这将使您无法重新启用它。
如果您想控制另一个扩展,这是一种有效的方法。
您需要获得"management"
权限才能影响其他扩展名。
如果您想控制自己的行为,则需要在扩展逻辑中实现。
/* Pseudocode */
function someEventHandler() {
if (state.enabled) {
// Do stuff
} else {
console.log("Functionality disabled, ignoring SomeEvent");
}
}
请注意,要保留此设置,您可能需要将其保存在扩展扩展重启后的位置。 chrome.storage
API可能是您的最佳选择。