怎样屏蔽某个网站以令自己更专注地面对生活的真实挑战

From 清冽之泉
Jump to navigation Jump to search

我有个坏习惯,动不动就刷知乎,仿佛掌管了天下,回首一看除了浪费了时间,收获了焦虑,脑子混沌,眼睛干涩,别的对真实生活的改善几近于无。很不值得。这不是知乎的错,是我自己漫无目标的错。这个脚本可以让我不再滥刷知乎。这个脚本的用途是彻底屏蔽知乎首页,不过对于通过搜索引擎查找到的问题答案界面放行。这样,满足了用知乎查有用资料的需求,屏蔽了无目标滥刷知乎首页推荐的需求。完美。

但由于知乎的个性化推荐系统实在太完美,还是推荐隔一段时间注销一下账号,清除信息茧房。除非,你用你的该知乎账号赚到了钱。

安装 Tampermonkey

安装方法

在 Tampermonkey 中新建脚本

把以下脚本复制进去保存即可 ==// ==UserScript

// @name Ban sites to be brave to focus on real life // @namespace https://qingliezhiquan.com/blog/Main_Page // @version 0.3 // @description 在主页拦截指定站点(只需在 TARGET_SITES 一行写站点名,如 zhihu v2ex bilibili) // @author Duanyll & 清冽之泉 & DeepSeek & (refactor) // @match *://*/* // @grant none // @license MIT // @downloadURL https://qingliezhiquan.com/blog/Main_Page // @updateURL https://qingliezhiquan.com/blog/Main_Page // ==/UserScript==

(function () {

 'use strict';
 // ======= 只需在这一行写上你要屏蔽的站点(用逗号或空格分隔) =======
 // 示例: const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
 const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
 // =================================================================
 // 常见“主页”路径:如果当前页面路径匹配这些,我们认为是首页
 const HOME_PATHS = new Set([
   , '/', '/index', '/index.html', '/index.htm',
   '/hot', '/recent', '/home', '/#', '/#!'
 ]);
 const hostname = window.location.hostname.toLowerCase();
 const pathname = window.location.pathname || ;
 // 规范化并拆分用户输入的 target,返回一个 domain 部分(去掉后面的路径)
 function normalizeTarget(t) {
   t = (t || ).toLowerCase().trim();
   if (!t) return ;
   // 如果用户写了完整域名或带路径,取第一个片段(域名)
   if (t.includes('/')) t = t.split('/')[0];
   // 去掉可能的协议
   t = t.replace(/^https?:\/\//, );
   // 去掉尾部的端口号(如 example.com:8080)
   t = t.replace(/:\d+$/, );
   return t;
 }
 // 检查当前 hostname 是否匹配用户指定的 target(支持写 'zhihu'、'zhihu.com'、'www.zhihu.com' 等)
 function hostnameMatchesTarget(hostname, rawTarget) {
   const target = normalizeTarget(rawTarget);
   if (!target) return false;
   // 如果 target 是完整域名(含点),直接做结尾匹配: hostname === target 或 hostname.endsWith('.' + target)
   if (target.includes('.')) {
     return hostname === target || hostname.endsWith('.' + target);
   }
   // 否则 target 只是一个标签(例如 'zhihu' 或 'v2ex'),检测 hostname 的 label 中是否包含该标签(精确 label 匹配)
   const hostLabels = hostname.split('.');
   return hostLabels.includes(target);
 }
 // 判断是否当前路径被认为是“首页”
 function isHomePath(pathname) {
   // 去掉尾部斜杠对比
   const p = pathname.replace(/\/+$/, ) || '/';
   if (HOME_PATHS.has(p)) return true;
   // 允许像 "/?..." 这种只带查询的也当作首页
   if ((p === '/' || p === ) && window.location.search) return true;
   return false;
 }
 // 主逻辑:如果 hostname 匹配任一 target 且路径认为是首页,就拦截
 const shouldBlock = TARGET_SITES.some(t => hostnameMatchesTarget(hostname, t)) && isHomePath(pathname);
 if (shouldBlock) {
   // 你可以按需替换下面的拦截提示(支持简单 HTML)
   const style = [
     'display:flex',
     'align-items:center',
     'justify-content:center',
     'height:100vh',
     'margin:0',
     'font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial',
     'background:#111',
     'color:#fff',
     'text-align:center',
     'padding:2rem'
   ].join(';');
   document.documentElement.innerHTML = `
     <body style="${style}">

不要相信阴暗、无力、无希望的那一面

你现在被屏蔽的站点位于首页 — 专注现实生活更重要。要访问具体页面,请从搜索或外链进入。

     </body>
   `;
   console.log('Site homepage blocked by userscript — targets:', TARGET_SITES.join(', '));
   return;
 }
 // 不拦截则继续加载
 console.log('Site homepage not blocked (not matched or not homepage).');

})();