Chrome 用于在弹出窗口中打开和关闭我的扩展的扩展代码
Chrome Extension Code To Toggle My Extension On & Off In Popup
我正在构建我的第一个 chrome 扩展,我快完成了!但是,我陷入了最后一部分,即创建一个简单的 html 切换以暂时禁用扩展。基本上,它的功能类似于广告拦截器,但它不是拦截广告,而是拦截网站并将它们重定向到特定的 URL.
这是我用来尝试执行此操作的代码,但出于某种原因,它会切换启用禁用,但不会关闭重定向。该应用程序的功能现在完美运行,我只想能够打开和关闭它。
manifest.json
{
"manifest_version": 2,
"name": "Purge",
"description": "Why Use Anything But Google?",
"version": "1.0.0",
"icons": {"128":"icon_128.png"},
"browser_action": {"default icon": "icon.png",
"default_popup": "popup.html"},
"permissions": ["webRequest", "webRequestBlocking", "http://*/", "https://*/"],
"background": {"scripts": ["blocked_domains.js", "background.js"]}
}
popup.html
<html>
<head>
<script src="toggle.js"></script>
</head>
<body>
<h3>PURGE!</h3>
<input type="button" id="toggle_button" value="Disable" />
<hr>
</body>
</html>
background.js
var enabled = true;
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
var url = "https://www.google.com/";
return {redirectUrl: url};
},
{urls: blocked_domains},
["blocking"]);
toggle.js
window.onload = function () {
function updateLabel() {
var enabled = chrome.extension.getBackgroundPage().enabled;
document.getElementById('toggle_button').value = enabled ? "Disable" : "Enable";
}
document.getElementById('toggle_button').onclick = function () {
var background = chrome.extension.getBackgroundPage();
background.enabled = !background.enabled;
updateLabel();
};
updateLabel();
}
好吧,您正在切换布尔值 enabled
和按钮,但无论值如何,您仍然会重定向。
在 onBeforeRequest
侦听器中,在决定重定向之前检查 enabled
的值是否为 false
的 true
:
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
if(!enabled) // if the extension is not enabled
return { cancel: false }; // don't cancel or redirect
var url = "https://www.google.com/";
return { redirectUrl: url };
},
{urls: blocked_domains},
["blocking"]);
我正在构建我的第一个 chrome 扩展,我快完成了!但是,我陷入了最后一部分,即创建一个简单的 html 切换以暂时禁用扩展。基本上,它的功能类似于广告拦截器,但它不是拦截广告,而是拦截网站并将它们重定向到特定的 URL.
这是我用来尝试执行此操作的代码,但出于某种原因,它会切换启用禁用,但不会关闭重定向。该应用程序的功能现在完美运行,我只想能够打开和关闭它。
manifest.json
{
"manifest_version": 2,
"name": "Purge",
"description": "Why Use Anything But Google?",
"version": "1.0.0",
"icons": {"128":"icon_128.png"},
"browser_action": {"default icon": "icon.png",
"default_popup": "popup.html"},
"permissions": ["webRequest", "webRequestBlocking", "http://*/", "https://*/"],
"background": {"scripts": ["blocked_domains.js", "background.js"]}
}
popup.html
<html>
<head>
<script src="toggle.js"></script>
</head>
<body>
<h3>PURGE!</h3>
<input type="button" id="toggle_button" value="Disable" />
<hr>
</body>
</html>
background.js
var enabled = true;
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
var url = "https://www.google.com/";
return {redirectUrl: url};
},
{urls: blocked_domains},
["blocking"]);
toggle.js
window.onload = function () {
function updateLabel() {
var enabled = chrome.extension.getBackgroundPage().enabled;
document.getElementById('toggle_button').value = enabled ? "Disable" : "Enable";
}
document.getElementById('toggle_button').onclick = function () {
var background = chrome.extension.getBackgroundPage();
background.enabled = !background.enabled;
updateLabel();
};
updateLabel();
}
好吧,您正在切换布尔值 enabled
和按钮,但无论值如何,您仍然会重定向。
在 onBeforeRequest
侦听器中,在决定重定向之前检查 enabled
的值是否为 false
的 true
:
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
if(!enabled) // if the extension is not enabled
return { cancel: false }; // don't cancel or redirect
var url = "https://www.google.com/";
return { redirectUrl: url };
},
{urls: blocked_domains},
["blocking"]);