Chrome启动时未启动Chrome webRequest事件

时间:2014-07-17 15:11:13

标签: javascript google-chrome google-chrome-extension

我正在尝试制作Chrome扩展程序来解决Steam恼人的网址警告页面。请参阅此示例:https://steamcommunity.com/linkfilter/?url=http://67.69.104.76:84/marville/photos/planes/comet-162a.jpg

到目前为止我所使用的功能除非点击链接时启动Chrome,否则会显示Steam通知页面。 以下是webrequest上的页面:https://developer.chrome.com/extensions/webRequest

知道如何让我在发布时也能使用它吗?

background.js

chrome.webRequest.onBeforeRequest.addListener(

    function (details) {
        var url = details.url;
        var host = url.substring(43);

        return { redirectUrl: host };
    },

    {
        urls: ["*://steamcommunity.com/linkfilter/*"],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },

    ["blocking"]

);

的manifest.json

{
    "manifest_version": 2,

    "name": "Steam Link Filter Redirect",
    "description": "Bypasses Steam link filtering.",
    "version": "1.1",
    "background": {"scripts":["background.js"]},
    "permissions": [
        "webRequest",
        "*://steamcommunity.com/linkfilter/*",
        "webRequestBlocking"
     ],

    "icons": { 
        "16": "icon16.png",
        "19": "icon19.png",
        "48": "icon48.png",
        "128": "icon128.png" 
    }
}

1 个答案:

答案 0 :(得分:0)

当然,问题是,在为开始页面完成onBeforeRequest阶段后,您的代码执行得太晚了。我认为你不能解决这个问题。

您可能需要添加回退机制。我建议添加一个在linkfilter页面中注入的内容脚本,并将页面URL更改为所需的内容,例如:

"content_scripts": [ {
  "matches": ["*://steamcommunity.com/linkfilter/*"],
  "js": ["redirect.js"]
} ],

redirect.js:

location.replace(
  decodeURIComponent( location.href.replace(/^.*?\?url=/, "") )
);

(我使它比.substring(43)更强大)