检测URL是否被打开 - Chrome扩展 [英] Detect if URL is opened - Chrome extension

查看:135
本文介绍了检测URL是否被打开 - Chrome扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以检测某个URL是否在Chrome中打开并重定向到另一个页面。我需要它做一个网站拦截器。

解决方案

是的,您现在可以 Chrome 17



为manifest.json添加背景页面和webRequest权限:

  {
background_page:background.html,
permissions:[
webRequest,webRequestBlocking,
http://www.mozilla.org/*
]
}
pre>

和重定向逻辑到background.html:

 << ; HTML><身体GT; 
< script>
chrome.webRequest.onBeforeRequest.addListener(
function(details){
//console.log('before',details);
if(details.url == http://www.mozilla.org/){
return {redirectUrl:https://www.google.com/chrome/};
};
},
{
urls:[http://www.mozilla.org/*],
类型:[main_frame]
},
[blocking ]
);
< / script>
< / body>< / html>


Is there any way to detect if a certain URL is opened in chrome and redirect to another page. I need it to make a site blocker.

解决方案

Yes, you can it now with Chrome 17.

Add the background page and webRequest permissions to manifest.json:

{
  "background_page": "background.html",
  "permissions": [
    "webRequest", "webRequestBlocking",
    "http://www.mozilla.org/*"
  ]
}

and a redirect logic to background.html:

<html><body>
<script>
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    //console.log('before', details);
    if (details.url == "http://www.mozilla.org/") {
      return {redirectUrl: "https://www.google.com/chrome/"};
    };
  },
  {
    urls: ["http://www.mozilla.org/*"],
    types: ["main_frame"]
  },
  ["blocking"]
);
</script>
</body></html>

这篇关于检测URL是否被打开 - Chrome扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆