如何创建自动重定向到特定链接的 Chrome 扩展?
您好,我想创建一个 chrome 扩展程序,以便当用户位于 example1.com 页面上时,即使在页面加载之前,他也会自动重定向到 example2.com 。无论用户是否输入网址或通过链接访问该网址,这都应该有效。
请帮助我!
谢谢。
Hi I want to create a chrome extension so that when the user is on the page example1.com he would automatically be redirected to example2.com even before the page loads. this should work whether or not the user types in the url or goes there through a link.
Please help me in this!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在后台页面:
In background page:
您可以使用 Chrome 的 webRequest API 在您的请求与特定网址匹配时重定向您的请求。以下是示例代码:
以下是有关使用 onBeforeRequest 方法的一些详细信息:
第一个参数是回调处理程序,每当拦截与作为第二个参数提供的 Url 模式匹配的请求时就会调用该回调处理程序
第二个参数,一组 URL 或 url 模式或正则表达式,告诉 chrome 拦截这些 URL
第三个参数,requestOptions 可以是
blocking
或requestBody
。您可以在此处阅读有关 webRequest API 的信息,
以防您正在寻找现成的解决方案可以帮您解决这个问题,您可以查看 Requestly 为您做同样的工作。它甚至允许您将 url 与三个运算符进行匹配
请查看此屏幕截图以更好地理解。
希望它有帮助!
You can use Chrome's webRequest API to redirect your request when it matches a particular Url. Here is a sample code:
Here are some details about using onBeforeRequest method:
first arugument, is your callback handler which is invoked whenever a request is intercepted which matches your Url patterns provided as second argument
second argument, A set of Urls or url patterns or Regex which tells chrome to intercept those Urls
third argument, requestOptions which can be
blocking
orrequestBody
.You can read about webRequest API here
Just in case you are looking for an already-made solution that can do the trick for you, you can checkout Requestly which does the same work for you. It even allows you to match your url with three operators
Have a look at this screenshot for better understanding.
Hope it helps!!
您不需要为此扩展。只需将
example1.com
重定向到example2.com
的 IP,然后在 example2 的服务器上创建一个虚拟主机,将请求反向代理到实时主机。You don't need an extension for this. Just redirect
example1.com
to the IP ofexample2.com
, then create a virtualhost on example2's server wich will reverse proxy the requests to the live host.