这个脚本费事作者一周,作者不断思考 \(\texttt{UI}\) 设计和用户体验,最后写成这样。如果有想做贡献的小朋友也可以在 link 中提建议、改代码,谢谢。
// ==UserScript==
// @name NoFishing
// @namespace http://tampermonkey.net/
// @version 2.5
// @description 禁止颓废。
// @author Kimi
// @match *://*/*
// @grant GM_setValue
// @grant GM_getValue
// @updateURL https://github.com/zjx-kimi/NoFishing/raw/master/NoFishing.meta.js
// @downloadURL https://github.com/zjx-kimi/NoFishing/raw/master/NoFishing.js
// ==/UserScript==
(function() {
'use strict';
// 白名单
let whitelist = GM_getValue("whitelist", []);
// 当前访问的网站
const currentSite = window.location.hostname;
// 增加访问次数
function increaseVisitCount() {
let visitCount = GM_getValue("visitCount", 0);
visitCount += 1;
GM_setValue("visitCount", visitCount);
return visitCount;
}
// 检查当前网站是否在白名单中
function isWhitelisted(site) {
return whitelist.includes(site);
}
// 显示气泡提示
// 显示气泡提示
function showToast(message, duration = 3000) {
let toast = document.createElement('div');
toast.style.position = 'fixed';
toast.style.bottom = '20px';
toast.style.left = '50%';
toast.style.transform = 'translateX(-50%)';
toast.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
toast.style.color = 'white';
toast.style.padding = '10px 20px';
toast.style.borderRadius = '4px';
toast.style.zIndex = 10001;
toast.style.fontSize = '16px';
toast.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.3)';
toast.style.whiteSpace = 'pre-wrap'; // 保持换行符
toast.style.textAlign = 'center'; // 中央对齐
toast.style.maxWidth = '300px'; // 限制宽度
toast.style.wordWrap = 'break-word'; // 自动换行
// 使用 HTML 内容来支持换行
toast.innerHTML = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.style.transition = 'opacity 0.5s';
toast.style.opacity = '0';
setTimeout(() => {
document.body.removeChild(toast);
}, 500);
}, duration);
}
// 显示自定义确认弹窗
function showConfirmationDialog(message, onConfirm) {
let overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = 0;
overlay.style.left = 0;
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
overlay.style.zIndex = 10002;
overlay.style.display = 'flex';
overlay.style.alignItems = 'center';
overlay.style.justifyContent = 'center';
let dialog = document.createElement('div');
dialog.style.backgroundColor = 'white';
dialog.style.borderRadius = '8px';
dialog.style.padding = '20px';
dialog.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.3)';
dialog.style.maxWidth = '400px';
dialog.style.width = '100%';
dialog.style.textAlign = 'center';
let text = document.createElement('p');
text.textContent = message;
text.style.marginBottom = '20px';
let confirmButton = document.createElement('button');
confirmButton.textContent = '确认';
confirmButton.style.padding = '10px 20px';
confirmButton.style.backgroundColor = '#007bff';
confirmButton.style.color = 'white';
confirmButton.style.border = 'none';
confirmButton.style.borderRadius = '4px';
confirmButton.style.cursor = 'pointer';
confirmButton.onclick = () => {
document.body.removeChild(overlay);
onConfirm();
};
let cancelButton = document.createElement('button');
cancelButton.textContent = '取消';
cancelButton.style.padding = '10px 20px';
cancelButton.style.backgroundColor = '#6c757d';
cancelButton.style.color = 'white';
cancelButton.style.border = 'none';
cancelButton.style.borderRadius = '4px';
cancelButton.style.cursor = 'pointer';
cancelButton.style.marginLeft = '10px';
cancelButton.onclick = () => {
document.body.removeChild(overlay);
};
dialog.appendChild(text);
dialog.appendChild(confirmButton);
dialog.appendChild(cancelButton);
overlay.appendChild(dialog);
document.body.appendChild(overlay);
}
// 显示控制面板按钮
function showControlPanelButton() {
let panel = document.createElement('div');
panel.style.position = 'fixed';
panel.style.bottom = '10px';
panel.style.right = '10px';
panel.style.backgroundColor = '#f9f9f9';
panel.style.border = '1px solid #ccc';
panel.style.borderRadius = '4px';
panel.style.padding = '5px';
panel.style.zIndex = 10000;
panel.style.cursor = 'pointer';
panel.style.display = 'grid';
panel.style.gridTemplateColumns = '1fr 1fr'; // 2x2 布局
const iconSize = '18px'; // 调整后的图标大小
// ⚙ 控制面板按钮
let controlButton = document.createElement('span');
controlButton.textContent = '⚙';
controlButton.style.cursor = 'pointer';
controlButton.title = '打开控制面板';
controlButton.style.textAlign = 'center';
controlButton.style.padding = '5px'; // 缩小内边距
controlButton.style.backgroundColor = '#007bff';
controlButton.style.color = 'white';
controlButton.style.borderRadius = '4px';
controlButton.style.border = '2px solid #007bff'; // 缩小边框
controlButton.style.display = 'flex';
controlButton.style.alignItems = 'center';
controlButton.style.justifyContent = 'center';
controlButton.style.fontSize = iconSize;
controlButton.onclick = function() {
openControlPanel();
};
// ➕ 添加到白名单按钮
let addButton = document.createElement('span');
addButton.textContent = '➕';
addButton.style.cursor = 'pointer';
addButton.title = '将当前网站加入白名单';
addButton.style.textAlign = 'center';
addButton.style.padding = '5px'; // 缩小内边距
addButton.style.backgroundColor = '#28a745';
addButton.style.color = 'white';
addButton.style.borderRadius = '4px';
addButton.style.border = '2px solid #28a745'; // 缩小边框
addButton.style.display = 'flex';
addButton.style.alignItems = 'center';
addButton.style.justifyContent = 'center';
addButton.style.fontSize = iconSize;
addButton.onclick = function() {
if (!isWhitelisted(currentSite)) {
whitelist.push(currentSite);
GM_setValue("whitelist", whitelist);
showToast("已加入白名单: " + currentSite);
} else {
showToast("该网站已在白名单中。");
}
};
// ❌ 删除当前网站按钮
let deleteButton = document.createElement('span');
deleteButton.textContent = '❌';
deleteButton.style.cursor = 'pointer';
deleteButton.title = '删除当前网站';
deleteButton.style.textAlign = 'center';
deleteButton.style.padding = '5px'; // 缩小内边距
deleteButton.style.backgroundColor = '#ffffff';
deleteButton.style.color = '#dc3545';
deleteButton.style.borderRadius = '4px';
deleteButton.style.border = '2px solid #dc3545'; // 缩小边框
deleteButton.style.display = 'flex';
deleteButton.style.alignItems = 'center';
deleteButton.style.justifyContent = 'center';
deleteButton.style.fontSize = iconSize;
deleteButton.onclick = function() {
if (isWhitelisted(currentSite)) {
whitelist = whitelist.filter(site => site !== currentSite);
GM_setValue("whitelist", whitelist);
showToast("已删除: " + currentSite);
window.location.href = 'https://codeforces.com/problemset/status?friends=on';
} else {
showToast("该网站不在白名单中。");
}
};
//
标签:NoFishing,脚本,style,appendChild,宣传,let,createElement,document,panel
From: https://www.cnblogs.com/kimi0705/p/18366759/NoFishing