在 chrome 扩展中重定向 URL [英] Redirecting URL in a chrome extension
问题描述
如何在访问给定 URL 时重定向扩展程序中的 chrome?
How can I go about redirecting chrome in an extension when visiting a given URL?
例如:当我访问 http://yahoo.com/
我希望它重定向到 http://google.com/
For example: when I visit http://yahoo.com/
I want it to redirect to http://google.com/
注意:此问题的前一个版本询问是否有任何 Google chrome 扩展程序可以在访问某个 URL 时自动重定向选项卡.因此,下面的(目前有两个)答案解决了不同的问题.
NOTE: A former version of this question asked whether there is any Google chrome extension which automatically redirects the tab when it visits a certain URL. Accordingly, the (currently two) answers below address different questions.
推荐答案
有很多选择,一个比另一个更复杂.
There are many options, the one more convoluted than the other.
webRequest
API,特别是onBeforeRequest
事件.(更好的是,即将推出的declarativeWebRequest
API)..莉>- 内容脚本.在页面中注入
location.replace('http://example.com')
. tabs
API.使用onUpdated
事件 来检测何时页面已更改其位置,并且chrome.tabs.update
更改其 URL.避免无限循环!
- 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!
第一个是最好的,因为它在页面被请求之前就被激活了.第二个可以在请求完成后激活,但在页面呈现之前 ("run_at":"document_start"
) 或在页面呈现之后 ("run_at":"document_end"
).为了完整起见,我提到了最后一个,但您不应该使用它,因为其他选项更好.
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.
这是一个使用 webRequest
API 的示例,这是一个简单的扩展程序,它允许我浏览 Pirate bay 上的页面,即使主要主机被我的 ISP 删除(实际的 URL 列表是更长的时间,但为了示例,我省略了它们).
有关 URL 格式的说明,请参阅匹配模式.
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.
{
"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?://[^/]+([Ss]*)/)[1]};
},
{
urls: [
"*://piratebay.se/*",
"*://www.piratebay.se/*"
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
这篇关于在 chrome 扩展中重定向 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!