所以我有网站不安全://site.com,我想做一个扩展,每次我访问不安全时都会将我重定向到不安全://site2.com://site.com; 这是我的background.js:
var host = "insecure://site2.com";
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: host + details.url.substr(16,details.url.length)};
},
{
urls: [
"insecure://site.com"
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlinsecurerequest", "other"]
},
["blocking"]
);
这是manifest.json的一部分:
"background": {"scripts":["background.js"]},
"content_scripts": [
{
"matches": ["<all_urls>"]
}
],
"permissions": [
"contextMenus",
"webRequest",
"webRequestBlocking",
"activeTab",
"tabs",
"insecure://site.com/*",
"secure://site.com/*",
"insecure://site2.com/*",
"secure://site2.com/*"
]
答案 0 :(得分:0)
您可以检查window.location.protocol
并据此采取相应行动:
if (window.location.protocol === 'http:') { // Insecure
window.location.href = "https://www.example.com"; // Redirect to website
}