在Chrome扩展中重定向URL [英] Redirecting URL in a chrome extension
问题描述
如何在访问给定网址时重定向扩展程序中的chrome? 例如:当我访问 http://时yahoo.com /
我希望它重定向到 http://google.com/
有很多选项,比另一个更复杂。
$ b
- 内容脚本。在页面中注入
location.replace('http://example.com')
。
-
选项卡
API。使用onUpdated
事件来检测页面何时更改其位置,以及chrome.tabs.update
更改其网址。尽管避免了一个无限循环!
< a href =https://developer.chrome.com/extensions/webRequest.html =noreferrer>
webRequest
API,特别是 onBeforeRequest
事件。 (更好的是,即将推出的 declarativeWebRequest
API < a>)。 第一个是最好的,因为它在一个页面被请求之前被激活。第二个可以在请求完成后激活,但是在页面呈现之前(run_at:document_start
)或者渲染之后( run_at: document_end
)。我提到了完整性的最后一个,但不应该使用它,因为其他选项更好。
以下是使用 webRequest
API,这是一个简单的扩展,它允许我浏览海盗湾上的页面,即使主要主机被我的ISP关闭(实际的URL列表更长,但我省略了它们为了举例)。
请参阅匹配模式以了解一个关于URL格式的解释。
manifest.json
{
name:The Pirate Bay,
description:将海盗湾重定向到不同的主机,
版本:1.0,
manifest_version:2,
background:{scripts:[background.js]},
permissions:[
webRequest,
*://thepiratebay.se/*,
*://www.thepiratebay.se/*,
webRequestBlocking
background.js $ c
var host =http://tpb.pirateparty.org.uk; $ c>
var host =http://tpb.pirateparty.org.uk; $ c>
chrome.webRequest.onBeforeRequest.addListener(
function(details){
return {redirectUrl:host + details.url.match(/ ^ https https::\ / \ / [^ \ /] +([\S\s] *)/)[1]};
},
{
网址:[
*:// piratebay.se/*,
*://www.piratebay.se/*
,
类型:[main_frame,sub_frame,stylesheet,脚本,图像,对象,xmlhttprequest,其他]
},
[blocking]
);
How can I go about redirecting chrome in an extension when visiting a given URL?
For example: when I visit http://yahoo.com/
I want it to redirect to http://google.com/
There are many options, the one more convoluted than the other.
- The
webRequest
API, specifically theonBeforeRequest
event. (Even better, the upcomingdeclarativeWebRequest
API). - Content scripts. Inject
location.replace('http://example.com')
in a page. - The
tabs
API. Use theonUpdated
event to detect when a page has changed its location, andchrome.tabs.update
to change its URL. Avoid an infinite loop though!
The first one is the best one, because it is activated before a page is even requested. The second one can be activated after the request has been fulfilled, but before the page is rendered ("run_at":"document_start"
) or after it's rendered ("run_at":"document_end"
). I mentioned the last one for completeness, but you shouldn't use it, because the other options are way better.
Here's an example using the webRequest
API, a simple extension which allows me to browse pages on the Pirate bay, even though the main hosts are taken down by my ISP (the actual list of URLs is much longer, but I have omitted them for the sake of the example).
See match patterns for an explanation on the URL formats.
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"
]
}
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"]
);
这篇关于在Chrome扩展中重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!