26 google-chrome google-chrome-extension
在访问给定的URL时,如何在扩展中重定向chrome?
例如:当我访问时,http://yahoo.com/
我希望它重定向到http://google.com/
Rob*_*b W 50
有很多选择,一个比另一个更令人费解.
webRequest
API,具体的onBeforeRequest
事件.(甚至更好,即将推出的declarativeWebRequest
API).location.replace('http://example.com')
页面.tabs
API.使用该onUpdated
事件可以检测页面何时更改其位置以及chrome.tabs.update
更改其URL.尽管避免无限循环!第一个是最好的,因为它甚至在页面被请求之前被激活.第二个可以在请求完成后激活,但在页面渲染("run_at":"document_start"
)之前或渲染()之后激活"run_at":"document_end"
.我提到了最后一个完整性,但你不应该使用它,因为其他选项更好.
这是一个使用webRequest
API 的示例,这是一个简单的扩展,允许我浏览Pirate托架上的页面,即使我的ISP取下了主要主机(实际的URL列表要长得多,但我已经省略了它们)例子).
有关URL格式的说明,请参阅匹配模式.
manifest.json
{
"name": "The Pirate Bay",
"description": "Redirect The Pirate Bay to a different host",
"version": "1.0",
"manifest_version": 2,
"background": {"scripts":["background.js"]},
"permissions": [
"webRequest",
"*://thepiratebay.se/*",
"*://www.thepiratebay.se/*",
"webRequestBlocking"
]
}
Run Code Online (Sandbox Code Playgroud)
background.js
var host = "http://tpb.pirateparty.org.uk";
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};
},
{
urls: [
"*://piratebay.se/*",
"*://www.piratebay.se/*"
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
Run Code Online (Sandbox Code Playgroud)
sac*_*024 19
我知道我在游戏中有点迟到回答这个问题我还是想为将来的读者回答这个问题.看一下
目前,您可以为其设置规则
截图了解更多:
路线图中有很多东西需要被覆盖
..还有更多.
PS:我已经创建了这个如果你没有找到这个有用的话你可以怪我:)
归档时间: |
|
查看次数: |
45212 次 |
最近记录: |