带有facebook块的Chrome扩展程序

时间:2015-07-09 19:59:33

标签: javascript google-chrome-extension

我希望您能帮助我理解为什么我的代码无效。 我想制作一个扩展名,当我加入m.facebook.com时,它会将我重定向到我想要的另一个页面。

让我告诉你我的代码。

var host = "http://www.google.com";          // here you put the URL that you want to be redirected 



chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
         return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};              //where it says "host" you put the name of the var that you have set above to the URL that you want to be redirected


    },
    {
        urls: [
            "*://www.facebook.com/*"                    // here you put the URL that you want to block.
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
    ["blocking"]
);

清单

{
    "name": "Facebook Notification Block",
    "description": "Block Notification",
    "version": "1.0",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "webRequest",
                    "*://facebook.com/*",
            "*://www.facebook.com/*",
        "webRequestBlocking"
    ]
}

此代码工作正常但仅适用于www.facebook.com (当我尝试加入www.facebook.com时,它会将我重定向到google.com) 当我替换为

urls: [
            "*://m.facebook.com/*"                    // here you put the URL that you want to block.
        ],

它不起作用,它加入了m.facebook.com

任何人都可以帮助我吗?

我也试过把

"m.facebook.com/"

但仍无效。 感谢您抽出宝贵的时间阅读本文!

1 个答案:

答案 0 :(得分:1)

{
    "name": "Facebook Notification Block",
    "description": "Block Notification",
    "version": "1.0",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "webRequest",
                    "*://facebook.com/*",
                     "*://m.facebook.com/*",
            "*://www.facebook.com/*",
        "webRequestBlocking"
    ]
}

我多么愚蠢...忘了将m.facebook.com添加到权限中。 现在工作正常。