简体   繁体   English

如何为阻止特定网站的Chrome扩展程序编写js

[英]How to write the js for a chrome extension that blocks a specific site

I've spent a good few hours trying to crack this myself, having analysed a bunch of other people's work (this is the latest one I've tried to hack Block URL with a specific word somewhere in the subdomain ), and have come up none the wiser. 我花了好几个小时来试图破解自己,分析了很多其他人的作品(这是我尝试用子域中某个特定单词破解Block URL的最新作品),并且提出了建议没有一个更明智。

I feel like the js shouldn't really be that complex, I'm just trying to block a webpage and surface an error message in its place. 我觉得js其实不应该那么复杂,我只是想阻止网页并在其位置显示错误消息。

This is where I've netted out atm: 这是我清除atm的地方:

chrome.declarativeWebRequest.onRequest.addRules({
  id: 'some rule id',
  conditions: [
    new chrome.declarativeWebRequest.RequestMatcher({
      url: {
        host: 'www.dailymail.com'
      }
    })
  ],

  actions: [
    new chrome.declarativeWebRequest.CancelRequest()
  ]
});

The answer at Block URL with a specific word somewhere in the subdomain works as expected. 子域中某处带有特定单词的阻止URL上的答案按预期工作。 When it does not work, follow the following steps to troubleshoot the problem: 如果它不起作用,请按照以下步骤解决问题:

  1. Open the background page's console and look for error messages . 打开后台页面的控制台,然后查找错误消息
    Search for the error message on Google, and try to understand the recommended results. 在Google上搜索错误消息,然后尝试了解建议的结果。 Trustworthy sources include Stack Overflow , the extension documentation , Chromium's bug tracker , the chromium-extensions and chromium-apps mailing lists. 值得信赖的资源包括Stack Overflow扩展文档Chromium的错误跟踪器chromium扩展chrome-apps邮件列表。

  2. Check whether you have added the correct permissions to your manifest file . 检查是否已为清单文件添加了正确的权限。

  3. If you have just added the new permissions, make sure that you have reloaded the extension (eg by clicking on Reload at chrome://extensions/ ). 如果您刚刚添加了新权限,请确保您已经重新加载了扩展名(例如,通过单击chrome://extensions/上的Reload)。

With the information you've provided, I can think of three reasons for failure: 利用您提供的信息,我可以想到失败的三个原因:

  1. Are you using Chromium beta , Chromium dev (or even Canary)? 您正在使用Chromium betaChromium开发人员 (甚至Canary)吗? If not, then either install Chrome from one of these channels, or use the webRequest API instead. 如果不是,请从以下任一渠道安装Chrome,或改用webRequest API。 The declarativeWebRequest API is currently not available on the stable channel, only on the beta and developer channels. 目前, declarativeWebRequest API在稳定通道上不可用,仅在beta和开发人员通道上可用。
  2. url is an object of the type UrlFilter . urlUrlFilter类型的对象。 There is no property called "host", if you want to match an exact host, use hostEquals , like this: 没有称为“ host”的属性,如果要匹配确切的主机,请使用hostEquals ,如下所示:

     url: { hostEquals: 'www.dailymail.com' } 
  3. You have not added the correct permissions. 您尚未添加正确的权限。 To get your demo to work, you need to declare the declarativeWebRequest and *://www.dailymail.com/* permissions in the manifest file. 为了使您的演示正常工作,您需要在清单文件中declarativeWebRequest*://www.dailymail.com/*权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM