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

From 清冽之泉
Jump to navigation Jump to search
Created page with "我有个坏习惯,动不动就刷知乎,仿佛掌管了天下,回首一看除了浪费了时间,收获了焦虑,脑子混沌,眼睛干涩,别的对真实生活的改善几近于无。很不值得。这不是知乎的错,是我自己漫无目标的错。这个脚本可以让我不再滥刷知乎。这个脚本的用途是彻底屏蔽知乎首页,不过对于通过搜索引擎查找到的问题答案界面放行。这样,满足了用知乎查有用资..."
 
Line 9: Line 9:
[[File:Tampermonkey 新建脚本的方法.png|1200px]]
[[File:Tampermonkey 新建脚本的方法.png|1200px]]


== 把以下脚本复制进去保存即可 ==
== 把以下脚本复制进去保存即可 ==// ==UserScript==
<syntaxhighlight lang="JavaScript" line>
// ==UserScript==
// @name        Ban sites to be brave to focus on real life
// @name        Ban sites to be brave to focus on real life
// @namespace    https://qingliezhiquan.com/blog/Main_Page
// @namespace    https://qingliezhiquan.com/blog/Main_Page
// @version      0.2
// @version      0.3
// @description  精准屏蔽知乎、V2EX 首页,但不屏蔽从搜索引擎或其他途径进入的具体页面。本插件思路是 Duanyll Zhihu-Smart-Block https://duanyll.com/ https://duanyll.com/2019/12/14/Zhihu-Smart-Block/ 最先提出并实现的,本插件是在他的思路上用 DeepSeek 改进的。具体改进为:不限制时间,直接限制知乎首页
// @description  在主页拦截指定站点(只需在 TARGET_SITES 一行写站点名,如 zhihu v2ex bilibili)
// @author      Duanyll & 清冽之泉 & DeepSeek
// @author      Duanyll & 清冽之泉 & DeepSeek & (refactor)
// @match        *://zhihu.com/*
// @match        *://*/*
// @match        *://www.zhihu.com/*
// @match        *://v2ex.com/*
// @match        *://www.v2ex.com/*
// @grant        none
// @grant        none
// @license      MIT
// @license      MIT
// @downloadURL https://qingliezhiquan.com/blog/Main_Page
// @downloadURL https://qingliezhiquan.com/blog/Main_Page
// @updateURL https://qingliezhiquan.com/blog/Main_Page
// @updateURL   https://qingliezhiquan.com/blog/Main_Page
// ==/UserScript==
// ==/UserScript==


(function () {
(function () {
    'use strict';
  'use strict';


    // 需要屏蔽的 URL 列表
  // ======= 只需在这一行写上你要屏蔽的站点(用逗号或空格分隔) =======
    const blockedUrls = [
  // 示例: const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
        "https://zhihu.com",
  const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
        "http://zhihu.com",
  // =================================================================
        "https://www.zhihu.com",
        "http://www.zhihu.com",
        "zhihu.com",
        "www.zhihu.com",
        "https://www.zhihu.com/hot",
        "https://www.v2ex.com",
        "https://www.v2ex.com/recent"
    ];


    // 获取当前页面的完整 URL
  // 常见“主页”路径:如果当前页面路径匹配这些,我们认为是首页
    const currentUrl = window.location.href;
  const HOME_PATHS = new Set([
    '', '/', '/index', '/index.html', '/index.htm',
    '/hot', '/recent', '/home', '/#', '/#!'
  ]);


     // 检查当前 URL 是否需要屏蔽
  const hostname = window.location.hostname.toLowerCase();
     function shouldBlock() {
  const pathname = window.location.pathname || '';
        // 如果当前 URL 是完全匹配需要屏蔽的 URL,则返回 true
 
        return blockedUrls.some(url => currentUrl === url || currentUrl === url + "/");
  // 规范化并拆分用户输入的 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 匹配)
     if (shouldBlock()) {
    const hostLabels = hostname.split('.');
        document.body.innerHTML = "<h1>天天瞎看,人会废掉</h1>";
    return hostLabels.includes(target);
         console.log("知乎主页已屏蔽");
  }
        return; // 直接返回,不执行后续逻辑
 
    }
  // 判断是否当前路径被认为是“首页”
  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}">
        <div>
          <h1 style="margin:0 0 0.5rem 0; font-size:1.6rem;">不要相信阴暗、无力、无希望的那一面</h1>
          <p style="margin:0.2rem 0 0 0; opacity:0.85;">你现在被屏蔽的站点位于首页 — 专注现实生活更重要。要访问具体页面,请从搜索或外链进入。</p>
         </div>
      </body>
    `;
    console.log('Site homepage blocked by userscript — targets:', TARGET_SITES.join(', '));
    return;
  }


    // 如果不需要屏蔽,则继续加载页面
  // 不拦截则继续加载
    console.log("当前页面不需要屏蔽,继续加载");
  console.log('Site homepage not blocked (not matched or not homepage).');
})();
})();
</syntaxhighlight>

Revision as of 23:20, 7 February 2026

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

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

安装 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).');

})();