Firefox扩展根据请求从一个页面重定向到另一个页面

时间:2017-12-16 18:27:57

标签: javascript firefox-addon

所以我有网站不安全://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/*"
]

1 个答案:

答案 0 :(得分:0)

您可以检查window.location.protocol并据此采取相应行动:

if (window.location.protocol === 'http:') { // Insecure
  window.location.href = "https://www.example.com"; // Redirect to website
}