google-chrome-extension - 如何制作一个基本的 Chrome 扩展来阻止某些网站?

标签 google-chrome-extension

到目前为止我所拥有的:

list .json

{
  "name": "Testing",
  "version": "0.1",
  "manifest_version": 2,
  "description": "Hi there.",
  "background": {
	"scripts": ["background.js"]
  },

  "icons": {
    "128" : "images/test.png"
  },
  "browser_action": {
    "default_icon": "images/test2.png",
    "default_title": "test"
  },
  "permissions": [
	"webRequest",
	"webRequestBlocking",
	"https://www.google.com/*",
	"http://www.dictionary.com/*"
  ]
}

背景.js

chrome.webRequest.onBeforeRequest.addListener(function(details) { 
		return {cancel: true}; 
	},
	{urls: ["https://www.google.com", "http://www.dictionary.com/*"]},
	["blocking"]);

我希望通过加载这个解压的扩展程序,它会“阻止”列出的网站(使用 Google.com 和dictionary.com 进行测试)。我不确定阻止功能实际上是如何工作的,但我认为网站要么无法加载,要么会显示某种一般错误。

但是,似乎没有发生任何事情,所以我猜测我对“阻塞”的理解有缺陷和/或我的代码编写不正确。我的代码基于这些引用文献:

https://developer.chrome.com/extensions/examples/extensions/catblock/manifest.json https://developer.chrome.com/extensions/examples/extensions/catblock/background.js https://developer.chrome.com/extensions/webRequest

“以下示例以更有效的方式实现了相同的目标,因为不针对 www.evil.com 的请求不需要传递给扩展程序:

  chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {cancel: true}; },
    {urls: ["*://www.evil.com/*"]},
    ["blocking"]); "

这是我第一次尝试制作 Chrome 扩展,我对 html 或 javascript 不太熟悉,所以如果我的实现偏离了目标,我深表歉意。

最佳答案

这是 site-blocking extension 的代码我做的。


以下是ma​​nifest.json文件:

{
  "manifest_version": 2,

  "name": "SiteBlocker",
  "version": "1.0.0",
  "description": "Block non-important info from your browsers",

  "content_scripts": [{
    "js": ["content.js"],
    "matches": ["http://*/*", "https://*/*"]
  }],
  "browser_action": {
      "default_icon": "icons8-fire-96.png", //change this icon to your icon file
      "default_popup": "small_win.html"
    }
}

这是包含主要代码的content.js文件:

//BLOCK WORDS
findString = function findText(text) {
  if(window.find(text)){
    document.documentElement.innerHTML = '';
    document.documentElement.innerHTML = 'This site is blocked';
    document.documentElement.scrollTop = 0;
  };
}

findString("WordToBlock");

//BLOCK THE PARTIAL DOMAINS
findURL = function changeURL(text){
  var current = window.location.href;
  if(current === text){
    window.location.replace("https://www.google.co.in");
  }
}

//BLOCK THE ENTIRE DOMAIN WITH THE FOLLOWING FUNCTION
findAllURL = function changeAllURL(text){
  var current = window.location.href;
  if(current.startsWith(text)){
    document.documentElement.innerHTML = '';
    document.documentElement.innerHTML = 'Domain is blocked';
    document.documentElement.scrollTop = 0;
  }
}


findURL("https://www.quora.com/");
findAllURL("https://www.facebook.com/");

这是 small_win.html 文件,当您单击扩展程序的图标时,该文件会打开一个弹出窗口:

<!DOCTYPE html>
<html>
<head>
  <style media="screen">
      body {
          min-width:500px;
      }
  </style>
</head>
<body>
  Add the content of the popup window
</h3>
</body>
</html>

这是我的 github 存储库的链接:https://github.com/sak1sham/LaserFocus其中包含扩展的代码。

谢谢

关于google-chrome-extension - 如何制作一个基本的 Chrome 扩展来阻止某些网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46624554/

相关文章:

google-chrome-extension - Chrome 文本转语音 API 不起作用

jquery - 通过 Chrome 扩展检测网页中的所有 <object>/<embed> 标签

javascript - 以编程方式单击 chrome 扩展程序中 Gmail 的 "show original"按钮?

javascript - 每当加载新页面时,将 chrome 扩展写入 console.log 事件选项卡 URL

url - 匹配模式 Google URL

javascript - Chrome 插件 - 从窗口的一部分生成 PNG/JPG

javascript - 使用 native JavaScript 触发 jQuery 实时事件

javascript - 内容脚本创建的元素在页面上创建 Gmail、Facebook、stackoverflow 等问题

google-chrome - 将 "chrome_url_overrides"添加到 chrome 扩展,禁用现有用户的扩展?

google-chrome - 调试新的 Mozilla WebExtension API