Greasy Fork is available in English.
屏蔽特定网站,显示励志语录。不依赖任何外部库,轻量快速。
当前为
// ==UserScript==
// @name 简易专注模式 (Block Site Pure)
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 屏蔽特定网站,显示励志语录。不依赖任何外部库,轻量快速。
// @author werflala
// @license MIT
// @match *://*/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// ================= 配置区域 =================
// 默认显示的励志语录
const MOTTO_TITLE = "敢不敢,事上练";
const MOTTO_SUB = "选项: 练字、编程、阅读、运动";
// 样式:设置界面的CSS
const UI_CSS = `
#block-site-settings-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.5); z-index: 999999; display: flex;
justify-content: center; align-items: center; font-family: sans-serif;
}
#block-site-settings-box {
background: white; padding: 20px; border-radius: 8px; width: 400px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.bs-title { font-size: 18px; font-weight: bold; margin-bottom: 10px; border-bottom: 1px solid #eee; padding-bottom: 5px;}
.bs-textarea { width: 100%; height: 150px; margin-bottom: 10px; padding: 5px; box-sizing: border-box; }
.bs-btn { padding: 5px 15px; cursor: pointer; border: none; border-radius: 4px; color: white; margin-right: 5px;}
.bs-save { background: #409EFF; }
.bs-close { background: #909399; }
.bs-tip { font-size: 12px; color: #666; margin-bottom: 10px; }
`;
// 样式:拦截页面的CSS
const BLOCK_CSS = `
body, html { margin: 0; padding: 0; height: 100%; overflow: hidden; background: #fdfdfd; }
#block-focus-container {
display: flex; flex-direction: column; justify-content: center; align-items: center;
height: 100vh; font-family: "Microsoft YaHei", sans-serif;
}
.focus-title { font-size: 48px; color: #333; margin-bottom: 20px; font-weight: bold; letter-spacing: 2px;}
.focus-sub { font-size: 24px; color: green; margin-bottom: 40px; }
.focus-url { color: #999; font-size: 14px; }
.unlock-btn { margin-top: 50px; opacity: 0; transition: opacity 2s; cursor: default; }
.unlock-btn:hover { opacity: 1; cursor: pointer;}
`;
// ================= 逻辑区域 =================
// 1. 读取屏蔽列表 (存储为数组)
function getBlockList() {
return GM_getValue("blockList", []);
}
// 2. 检查当前网址是否在屏蔽列表中
function checkBlock() {
const list = getBlockList();
const currentUrl = window.location.href;
const currentHostname = window.location.hostname;
for (let rule of list) {
if (!rule) continue;
// 简单的包含匹配,或者你可以写更复杂的正则
// 如果规则是 "bilibili.com",那么访问 "www.bilibili.com" 就会被拦截
if (currentHostname.includes(rule) || currentUrl.includes(rule)) {
doBlockAction(rule);
break;
}
}
}
// 3. 执行拦截动作
function doBlockAction(matchedRule) {
console.log(`[Block Site] Blocked by rule: ${matchedRule}`);
// 停止页面加载 (尽力而为)
window.stop();
// 替换 Body 内容
const htmlContent = `
<div id="block-focus-container">
<div class="focus-title">${MOTTO_TITLE}</div>
<div class="focus-sub">${MOTTO_SUB}</div>
<div class="focus-url">正在尝试访问: ${window.location.hostname}</div>
<div class="unlock-btn" onclick="alert('请在油猴菜单中移除此域名以解锁')">解锁说明</div>
</div>
`;
// 必须在 DOM Ready 或者直接覆写
if(document.body) {
document.body.innerHTML = htmlContent;
GM_addStyle(BLOCK_CSS);
} else {
// 如果运行得太早,body还没生成,监听加载
window.addEventListener('DOMContentLoaded', () => {
document.body.innerHTML = htmlContent;
GM_addStyle(BLOCK_CSS);
});
}
}
// 4. 显示设置界面 (UI)
function showSettings() {
if (document.getElementById('block-site-settings-overlay')) return;
GM_addStyle(UI_CSS);
const list = getBlockList();
const listText = list.join('\n'); // 换行显示方便编辑
const div = document.createElement('div');
div.id = 'block-site-settings-overlay';
div.innerHTML = `
<div id="block-site-settings-box">
<div class="bs-title">屏蔽列表设置</div>
<div class="bs-tip">一行一个关键词或域名 (例如: weibo.com)</div>
<textarea class="bs-textarea" id="bs-input">${listText}</textarea>
<div style="text-align: right;">
<button class="bs-btn bs-close" id="bs-btn-close">取消</button>
<button class="bs-btn bs-save" id="bs-btn-save">保存生效</button>
</div>
</div>
`;
document.body.appendChild(div);
// 绑定事件
document.getElementById('bs-btn-close').onclick = () => div.remove();
document.getElementById('bs-btn-save').onclick = () => {
const val = document.getElementById('bs-input').value;
// 分割换行,去空,去重
const newList = [...new Set(val.split('\n').map(s => s.trim()).filter(s => s))];
GM_setValue("blockList", newList);
div.remove();
alert('保存成功!刷新页面生效。');
location.reload();
};
}
// ================= 初始化 =================
// 注册菜单:点击浏览器插件图标 -> Tampermonkey -> 就能看到这个按钮
GM_registerMenuCommand("🚫 设置屏蔽列表", showSettings);
// 开始检查
checkBlock();
})();