admin管理员组文章数量:1355111
background.js:
chrome.webRequest.onBeforeRequest.addListener(function (details) {
console.log(details.url);
window.location.href = '/';
}, {urls: ['<all_urls>']}, []);
It shows all requests in the console when I visit a web site, but it doesn't redirect the site to time.
Additional Information:
I got error on background console here:
Error in event handler for webRequest.onHeadersReceived/1: ReferenceError: url is not defined at chrome.webRequest.onHeadersReceived.addListener.urls (chrome-extension://hkiajgmcjicgampfdeiiacbojlmdokob/background.js:5:8)
then... is there a way to see requests with console.log from time in this case?
I like to see request and don't need to redirect on Chrome window. What I need is only request to see in background console.
background.js:
chrome.webRequest.onBeforeRequest.addListener(function (details) {
console.log(details.url);
window.location.href = 'http://time./';
}, {urls: ['<all_urls>']}, []);
It shows all requests in the console when I visit a web site, but it doesn't redirect the site to time..
Additional Information:
I got error on background console here:
Error in event handler for webRequest.onHeadersReceived/1: ReferenceError: url is not defined at chrome.webRequest.onHeadersReceived.addListener.urls (chrome-extension://hkiajgmcjicgampfdeiiacbojlmdokob/background.js:5:8)
then... is there a way to see requests with console.log from time. in this case?
I like to see request and don't need to redirect on Chrome window. What I need is only request to see in background console.
Share Improve this question edited Mar 4, 2017 at 20:12 Makyen♦ 33.4k12 gold badges92 silver badges125 bronze badges asked Mar 4, 2017 at 7:29 KaeusKaeus 411 silver badge3 bronze badges 1-
2
does not work
... what happens? do you get an error? the thing is,background.js
does not have awindow
– Jaromanda X Commented Mar 4, 2017 at 8:00
1 Answer
Reset to default 9webRequest API provides redirection functionality.
Add webRequestBlocking
, webRequest
, and host permissions in manifest.json:
"permissions" : [
"webRequest",
"webRequestBlocking",
"http://www.example./*" /* or <all_urls> */
],
"background": {
"scripts": ["background.js"]
}
Intercept requests for the page itself (main_frame
) and iframes (sub_frame
) on the URLs you want to redirect (those should be declared in "permissions" shown above) in a blocking
listener:
chrome.webRequest.onBeforeRequest.addListener(function(details) {
console.log(details.url);
if (!details.url.startsWith('http://time./')) {
return {redirectUrl: 'http://time.'};
}
}, {
urls: ['http://www.example./*'], // or <all_urls>
types: ['main_frame', 'sub_frame'],
}, [
'blocking'
]);
To view the background console, open it on chrome://extensions page.
Also, make sure to read the extensions architecture article in the documentation: the background page is a totally separate page, not related to the web page, with its own context and its own URL like chrome-extension://blablabla/background.html which cannot be navigated to another URL.
本文标签: javascriptHow to redirect URLs using chromewebRequest APIStack Overflow
版权声明:本文标题:javascript - How to redirect URLs using chrome.webRequest API? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744036294a2579830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论