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

From 清冽之泉
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 12: Line 12:
<syntaxhighlight lang="JavaScript" line>
<syntaxhighlight lang="JavaScript" line>
// ==UserScript==
// ==UserScript==
// @name        Ban sites to be brave to focus on real life
// @name        Minimal instant homepage blocker
// @namespace    https://qingliezhiquan.com/blog/Main_Page
// @version      0.4
// @version      0.3
// @description  在主页拦截指定站点(只需在 TARGET_SITES 一行写站点名,如 zhihu v2ex bilibili)
// @author      Duanyll & 清冽之泉 & DeepSeek & (refactor)
// @match        *://*/*
// @match        *://*/*
// @run-at      document-start
// @grant        none
// @grant        none
// @license      MIT
// @downloadURL  https://qingliezhiquan.com/blog/Main_Page
// @updateURL    https://qingliezhiquan.com/blog/Main_Page
// ==/UserScript==
// ==/UserScript==


Line 27: Line 22:
   'use strict';
   'use strict';


   // ======= 只需在这一行写上你要屏蔽的站点(用逗号或空格分隔) =======
   // ===== 在这里编辑你要屏蔽的站点“肢干”(只写关键字,不写协议或域名后缀) =====
   // 示例: const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
   // 例如: ['zhihu', 'bilibili', 'v2ex', 'xiaohongshu']
   const TARGET_SITES = ['zhihu', 'v2ex', 'bilibili'];
   const TARGET_STEMS = ['zhihu', 'bilibili', 'v2ex', '52pojie'];
   // =================================================================
   // ========================================================================


   // 常见“主页”路径:如果当前页面路径匹配这些,我们认为是首页
   // 把常见的主页路径列出来(去除尾部斜杠后对比)
   const HOME_PATHS = new Set([
   const HOME_PATHS = new Set(['', '/', '/index', '/index.html', '/index.htm', '/home', '/hot', '/recent']);
    '', '/', '/index', '/index.html', '/index.htm',
    '/hot', '/recent', '/home', '/#', '/#!'
  ]);


   const hostname = window.location.hostname.toLowerCase();
   try {
  const pathname = window.location.pathname || '';
    const host = location.hostname.toLowerCase(); // e.g. "www.zhihu.com"
    const pathRaw = location.pathname || '/';
    // 规范化 pathname:去掉尾部斜杠(除非仅为根)
    let path = pathRaw.replace(/\/+$/, '');
    if (path === '') path = '/';


  // 规范化并拆分用户输入的 target,返回一个 domain 部分(去掉后面的路径)
    // 判断 hostname 的 label(去除端口)中是否包含任一肢干(精确 label 匹配)
  function normalizeTarget(t) {
     const hostLabels = host.split('.').map(s => s.replace(/:\d+$/, '')); // ["www","zhihu","com"]
     t = (t || '').toLowerCase().trim();
     const hostMatches = TARGET_STEMS.some(stem => hostLabels.includes(stem.toLowerCase()));
    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) {
    function isHomeLike(p) {
    const target = normalizeTarget(rawTarget);
      // 直接匹配常见项
    if (!target) return false;
      if (HOME_PATHS.has(p)) return true;
      // 有些站点会把首页放在 "/#/" 或仅通过 hash 控制路由 —— 如果 pathname 为根且存在 hash 或 search,也认为是首页
      if ((p === '/' || p === '') && (location.hash || location.search)) return true;
      return false;
    }


     // 如果 target 是完整域名(含点),直接做结尾匹配: hostname === target 或 hostname.endsWith('.' + target)
     if (hostMatches && isHomeLike(path)) {
    if (target.includes('.')) {
      // 立刻重写文档流 —— 极简内容,避免任何闪现
       return hostname === target || hostname.endsWith('.' + target);
      document.open();
      document.write('<!doctype html><meta charset="utf-8"><title>Act like someone who will succeed.</title><body>已屏蔽 — 专注现实生活更重要</body>');
       document.close();
      return;
     }
     }
 
  } catch (e) {
     // 否则 target 只是一个标签(例如 'zhihu' 或 'v2ex'),检测 hostname 的 label 中是否包含该标签(精确 label 匹配)
     // 出错时不影响页面加载(保险回退)
     const hostLabels = hostname.split('.');
     console.error('minimal-homepage-blocker error:', e);
    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}">
        <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('Site homepage not blocked (not matched or not homepage).');
})();
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 21:46, 8 February 2026

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

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

安装 Tampermonkey

安装方法

在 Tampermonkey 中新建脚本

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

// ==UserScript==
// @name         Minimal instant homepage blocker
// @version      0.4
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // ===== 在这里编辑你要屏蔽的站点“肢干”(只写关键字,不写协议或域名后缀) =====
  // 例如: ['zhihu', 'bilibili', 'v2ex', 'xiaohongshu']
  const TARGET_STEMS = ['zhihu', 'bilibili', 'v2ex', '52pojie'];
  // ========================================================================

  // 把常见的主页路径列出来(去除尾部斜杠后对比)
  const HOME_PATHS = new Set(['', '/', '/index', '/index.html', '/index.htm', '/home', '/hot', '/recent']);

  try {
    const host = location.hostname.toLowerCase(); // e.g. "www.zhihu.com"
    const pathRaw = location.pathname || '/';
    // 规范化 pathname:去掉尾部斜杠(除非仅为根)
    let path = pathRaw.replace(/\/+$/, '');
    if (path === '') path = '/';

    // 判断 hostname 的 label(去除端口)中是否包含任一肢干(精确 label 匹配)
    const hostLabels = host.split('.').map(s => s.replace(/:\d+$/, '')); // ["www","zhihu","com"]
    const hostMatches = TARGET_STEMS.some(stem => hostLabels.includes(stem.toLowerCase()));

    // 判断是否为我们要视为“首页”的路径
    function isHomeLike(p) {
      // 直接匹配常见项
      if (HOME_PATHS.has(p)) return true;
      // 有些站点会把首页放在 "/#/" 或仅通过 hash 控制路由 —— 如果 pathname 为根且存在 hash 或 search,也认为是首页
      if ((p === '/' || p === '') && (location.hash || location.search)) return true;
      return false;
    }

    if (hostMatches && isHomeLike(path)) {
      // 立刻重写文档流 —— 极简内容,避免任何闪现
      document.open();
      document.write('<!doctype html><meta charset="utf-8"><title>Act like someone who will succeed.</title><body>已屏蔽 — 专注现实生活更重要</body>');
      document.close();
      return;
    }
  } catch (e) {
    // 出错时不影响页面加载(保险回退)
    console.error('minimal-homepage-blocker error:', e);
  }
})();