使用 Google Chrome webRequest API 进行简单转发
我正在尝试创建一个 Chrome 扩展程序来帮助我了解 Chrome 的 webRequest API 通过复制 此命令完成的操作答案,但没有命名空间的实验部分,由于 webRequest 现在位于 Chrome 的主干中,因此不再需要该部分。
在我的背景页面中,我有代码:
<!DOCTYPE html>
<html>
<head>
<script>
function interceptRequest(request) {
return { redirectUrl: 'http://www.yahoo.com' }
}
chrome.webRequest.onBeforeRequest.addListener(interceptRequest, { urls: ['*://*.google.com/*'] }, ['blocking']);
</script>
</head><body></body>
</html>
在我的清单文件中,我有:
{
"name": "My Extension",
"version": "0.1",
"background_page": "background.html",
"permissions" : [
"webRequest",
"webRequestBlocking",
"*://*/*"
]
}
根据之前提出的问题,我应该被转发到 http://www.yahoo.com 当我访问 https://www.google.com 并且我的扩展已加载,但是 https://www.google.com 加载时没有任何转发效果。我需要做什么才能使用 Chrome 中的 webRequest API 将用户从一个 URI 转发到另一个 URI?
I am attempting to create a Chrome extension to help me understand Chrome's webRequest API by replicating what is done with this answer but without the experimental portion of the namespace, which is no longer needed since webRequest is now in Chrome's trunk.
In my background page I have the code:
<!DOCTYPE html>
<html>
<head>
<script>
function interceptRequest(request) {
return { redirectUrl: 'http://www.yahoo.com' }
}
chrome.webRequest.onBeforeRequest.addListener(interceptRequest, { urls: ['*://*.google.com/*'] }, ['blocking']);
</script>
</head><body></body>
</html>
In my manifest file I have:
{
"name": "My Extension",
"version": "0.1",
"background_page": "background.html",
"permissions" : [
"webRequest",
"webRequestBlocking",
"*://*/*"
]
}
Based on the previously asked question, I should be forwarded to http://www.yahoo.com when I access https://www.google.com and my extension is loaded, however, https://www.google.com loads without any forwarding effect. What do I need to do in order to simply forward a user from one URI to another using the webRequest API in Chrome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的网址匹配模式不正确。您只需添加尾部斜杠:
或者,您可以使用:
'*://www.google.com/' 匹配所有协议,'*://www.google.com/*' 匹配域之外的协议和 '*://*.google.com/*' 来匹配所有子域、协议和路径等。
请参阅:
http://code.google.com/chrome/extensions/trunk/match_patterns.html
Your url matching pattern isn't correct. You simply need to add a trailing slash:
Alternatively, you could use:
'*://www.google.com/' to match all protocols, '*://www.google.com/*' to match beyond just the domain, and '*://*.google.com/*' to match all subdomains, protocols and paths, etc.
See:
http://code.google.com/chrome/extensions/trunk/match_patterns.html