Javascript 如何使用 Google Chrome 扩展和内容脚本重定向到 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4859689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I redirect to a URL using a Google Chrome Extension & Content Script?
提问by Sathyajith Bhat
I'm currently building a Google Chrome extension which tests for certain patterns and if found, redirects them to a new URL.
我目前正在构建一个 Google Chrome 扩展程序,它测试某些模式,如果找到,则将它们重定向到一个新的 URL。
I've gotten the pattern checking done via a content script, and now I'm not sure how can I proceed with getting the redirect done. Any suggestions ?
我已经通过内容脚本完成了模式检查,现在我不确定如何继续完成重定向。有什么建议 ?
回答by serg
Send redirect url from a content script to a background page:
将重定向 url 从内容脚本发送到后台页面:
chrome.runtime.sendMessage({redirect: "http://redirect"});
In a background page update tab's url which would cause redirect:
在会导致重定向的后台页面更新选项卡的 url 中:
chrome.runtime.onMessage.addListener(function(request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});
回答by serv-inc
If you want to access a file inside your WebExtension, you can add the file and its prerequisites to web_accessible_resources
in manifest.json
, as in
如果要访问 WebExtension 中的文件,可以将文件及其先决条件添加到web_accessible_resources
in 中manifest.json
,如
{ ... "web_accessible_resources": [ "images/*.png", "style/double-rainbow.css", "script/double-rainbow.js", "script/main.js", "templates/*" ], ... }
{ ... "web_accessible_resources": [ "images/*.png", "style/double-rainbow.css", "script/double-rainbow.js", "script/main.js", "templates/*" ], ... }
回答by sajal
Ive not worked with Google Chrome extensions... but you may be able to use one of these ways.
我没有使用过 Google Chrome 扩展程序……但是您可以使用其中一种方式。
As I understand, these extension APIs allow you to inject javascript into the page... after that its simple manipulation of window.location ...
据我了解,这些扩展 API 允许您将 javascript 注入到页面中......之后它对 window.location 的简单操作......