怎样屏蔽某个网站以令自己更专注地面对生活的真实挑战: Difference between revisions
Jump to navigation
Jump to search
| (One intermediate revision by the same user not shown) | |||
| Line 12: | Line 12: | ||
<syntaxhighlight lang="JavaScript" line> | <syntaxhighlight lang="JavaScript" line> | ||
// ==UserScript== | // ==UserScript== | ||
// @name | // @name Minimal instant homepage blocker | ||
// @version 0.4 | |||
// @version 0. | |||
// @match *://*/* | // @match *://*/* | ||
// @run-at document-start | |||
// @grant none | // @grant none | ||
// ==/UserScript== | // ==/UserScript== | ||
| Line 27: | Line 22: | ||
'use strict'; | 'use strict'; | ||
// ===== | // ===== 在这里编辑你要屏蔽的站点“肢干”(只写关键字,不写协议或域名后缀) ===== | ||
// | // 例如: ['zhihu', 'bilibili', 'v2ex', 'xiaohongshu'] | ||
const | 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']); | ||
const | 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); | |||
} | } | ||
})(); | |||
</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);
}
})();