// ==UserScript== // @name 51CTONock // @namespace http://tampermonkey.net/ // @version 0.0.1 // @description BlogNock系列,51CTO 文章的标识优化 // @author Exisi // @license MIT License // @match blog.51cto.com/* // @supportURL https://github.com/Exisi/BlogNock/issues/ // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // ==/UserScript== (function () { "use strict"; const features = { mark: { datetime: { enabled: GM_getValue("datetime", true), selector: [".messbox time", "script[type='application/ld+json']"], icon: ``, }, readtime: { enabled: GM_getValue("readtime", true), selector: [".messbox time", ".article-content-wrap"], icon: ``, }, }, allow_copy: { enabled: GM_getValue("allow_copy", true), selector: [".code-toolbar", ".article-content-wrap"], }, allow_copy_with_btn: { enabled: GM_getValue("allow_copy_with_btn", true), selector: [".copy_btn", "code"], }, hidden: { first_publish_tips: { enabled: GM_getValue("first_publish_tips", true), selector: [".header-content li.first"], }, publish_task_tips: { enabled: GM_getValue("publish_task_tips", true), selector: [".header-content>.task", ".header-right .creative"], }, min_menu_pic: { enabled: GM_getValue("min_menu_pic", true), selector: [".hover-ball"], }, }, special_hidden: { first_publish_tips: { enabled: GM_getValue("first_publish_tips", true), selector: [".detail-content-right section", "section.common-fix"], }, }, }; const setModal = ``; const setStyle = `@keyframes fall { 0% { transform: translate(0%, -100%); opacity: 0; } 100% { transform: translate(0%, 0%); opacity: 1; } } .setting-item input[type=checkbox] { height: 0; width: 0; display: none; } .setting-item label { cursor: pointer; text-indent: -9999px; width: 40px; height: 20px; background: pink; display: block; border-radius: 100px; position: relative; } .setting-item label:after { content: ''; position: absolute; top: 2px; left: 2px; width: 15px; height: 15px; background: #fff; border-radius: 90px; transition: 0.2s; } .setting-item input:checked+label { background: #57a; } .setting-item input:checked+label:after { left: calc(100% - 2px); transform: translateX(-100%); } .setting-item label:active:after { width: 28px; } .modal-dialog { pointer-events: auto !important; display:none; border: none; position: fixed; z-index: 9999; left: 0; top: 0; width: 100%; min-width: 100vw; min-height: 100vh; height: 100%; background-color: rgba(0, 0, 0, 0.4); } .modal-setting { width: 450px; margin: auto; background-color: #ffffff; border-radius: 5px; padding: 20px; margin-top: 40px; position: relative; box-sizing: border-box; animation: fall 0.5s ease-in-out; } .modal-header { border-bottom: 1px solid #000000; } .modal-header h3 { padding: 10px 0; margin: 0; } .modal-header span { font-size: 24px; color: #ccc; position: absolute; right: 5px; top: 0; cursor: pointer; } .setting-item { margin: 10px 0; font-size: 14px; display: flex; justify-content: space-between; }`; const dStyle = document.createElement("style"); dStyle.innerHTML = setStyle; document.head.appendChild(dStyle); const modal = document.createElement("div"); modal.innerHTML = setModal; document.body.appendChild(modal); const checkboxList = document.querySelectorAll(".setting-item input"); Array.from(checkboxList).forEach((checkbox) => { const nock = checkbox.getAttribute("aria-nock"); checkbox.checked = GM_getValue(nock, true); }); const showSetting = () => (document.querySelector(".modal-dialog").style.display = "block"); const closeSetting = () => (document.querySelector(".modal-dialog").style.display = "none"); document.querySelector(".modal-dialog").addEventListener("click", closeSetting); document.querySelector(".modal-setting").addEventListener("click", (e) => e.stopPropagation()); document.querySelector(".btn-dialog-close").addEventListener("click", closeSetting); document.querySelector(".modal-body").addEventListener("click", (e) => { if (e.target.type !== "checkbox") { return; } const nock = e.target.getAttribute("aria-nock"); const flag = GM_getValue(nock) == null ? false : !GM_getValue(nock); GM_setValue(nock, flag); }); GM_registerMenuCommand("功能设置", showSetting); if (features.mark.datetime.enabled) { const datePublished = document.querySelector(features.mark.datetime.selector[0]); const jsonData = JSON.parse( document.querySelector(features.mark.datetime.selector[1])?.innerText ?? "{}" ); const rawPublishedTime = datePublished.getAttribute("pubdate"); const rawModifiedTime = jsonData?.upDate ?? rawPublishedTime; const postTime = new Date(rawPublishedTime).toLocaleString().replaceAll("/", "-"); const updateTime = new Date(rawModifiedTime).toLocaleString().replaceAll("/", "-"); const postTimeAgo = calculateTimeAgo(rawPublishedTime); const updateTimeAgo = calculateTimeAgo(rawModifiedTime); const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`; const formattedUpdateTime = `编辑于 ${updateTime}(${updateTimeAgo})`; const timeBlock = document.createElement("span"); timeBlock.style.textDecoration = "underline"; timeBlock.innerText = formattedPostTime; datePublished.style.display = "flex"; datePublished.style.alignItems = "center"; datePublished.style.cursor = "pointer"; datePublished.innerHTML = features.mark.datetime.icon; datePublished.appendChild(timeBlock); datePublished.addEventListener("click", () => { const text = timeBlock.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime; timeBlock.innerText = text; }); const icon = datePublished.querySelector("svg"); icon.style.width = "14px"; icon.style.height = "14px"; icon.style.marginRight = "3px"; } if (features.mark.readtime.enabled) { const timeItem = document.querySelector(features.mark.readtime.selector[0]); const readbox = document.createElement("span"); const textCount = document.querySelector(features.mark.readtime.selector[1]).innerText.length; const readtime = textCount / 400; if (readtime >= 1440) { const days = Math.floor(readtime / 1440); readbox.innerHTML = `${features.mark.readtime.icon} 预计阅读时长 ${days} 天`; } if (readtime >= 60) { const hours = Math.floor(readtime / 60); const minutes = Math.floor(readtime % 60); readbox.innerHTML = `${features.mark.readtime.icon} 预计阅读时长 ${hours} 小时 ${minutes} 分钟`; } if (readtime >= 1) { const minutes = Math.round(readtime); readbox.innerHTML = `${features.mark.readtime.icon} 预计阅读时长 ${minutes} 分钟`; } if (readtime < 1) { const seconds = Math.round(readtime * 60); readbox.innerHTML = `${features.mark.readtime.icon} 预计阅读时长 ${seconds} 秒`; } readbox.style.textDecoration = "underline"; readbox.style.display = "inline-flex"; readbox.style.alignItems = "center"; readbox.style.cursor = "pointer"; readbox.addEventListener("click", () => document.querySelector(features.mark.readtime.selector[1]).scrollIntoView(false) ); const icon = readbox.querySelector("svg"); icon.style.width = "14px"; icon.style.height = "14px"; icon.style.marginRight = "3px"; timeItem.after(readbox); } if (features.allow_copy.enabled) { const observer = new MutationObserver(() => { const codeToolBar = document.querySelector(features.allow_copy.selector[0]); if (!codeToolBar) { return; } const article = document.querySelector(features.allow_copy.selector[1]); article.querySelectorAll("*").forEach((content) => { content.oncopy = (e) => e.stopPropagation(); content.onkeydown = (e) => e.stopPropagation(); }); observer.disconnect(); }); observer.observe(document.body, { childList: true, subtree: true }); } if (features.allow_copy_with_btn.enabled) { console.log("object"); const observer = new MutationObserver(() => { const copyBtns = document.querySelectorAll(features.allow_copy_with_btn.selector[0]); if (copyBtns.length == 0) { return; } copyBtns.forEach((btn) => { btn.innerText = "复制"; btn.addEventListener("click", (e) => { e.stopPropagation(); const codeBlock = e.target.parentNode.parentNode; const code = codeBlock.querySelector(features.allow_copy_with_btn.selector[1]); navigator.clipboard.writeText(code.innerText); btn.innerText = "已复制"; setTimeout(() => (btn.innerText = "复制"), 1000); }); }); observer.disconnect(); }); observer.observe(document.body, { childList: true, subtree: true }); } const hiddenStyle = document.createElement("style"); document.head.appendChild(hiddenStyle); Object.entries(features.hidden).forEach(([key, option]) => { if (option.enabled) { const cssRule = `${option.selector} { display: none !important; }`; hiddenStyle.sheet.insertRule(cssRule); } }); if (features.special_hidden.first_publish_tips.enabled) { const tipItem = document.querySelector(features.special_hidden.first_publish_tips.selector[1]); tipItem.style.display = "none"; const cssRule = `${features.special_hidden.first_publish_tips.selector[0]}{ visibility: visible !important; }`; hiddenStyle.sheet.insertRule(cssRule); } function calculateTimeAgo(datetime) { const SECOND = 1000; const MINUTE = 60 * SECOND; const HOUR = 60 * MINUTE; const DAY = 24 * HOUR; const MONTH = 30 * DAY; const YEAR = 12 * MONTH; const postDate = new Date(datetime); const currentDate = new Date(); const timeDiff = currentDate - postDate; if (timeDiff >= YEAR) { return Math.floor(timeDiff / YEAR) + " 年前"; } if (timeDiff >= MONTH) { return Math.floor(timeDiff / MONTH) + " 个月前"; } if (timeDiff >= DAY) { return Math.floor(timeDiff / DAY) + " 天前"; } if (timeDiff >= HOUR) { return Math.floor(timeDiff / HOUR) + " 小时前"; } if (timeDiff >= MINUTE) { return Math.floor(timeDiff / MINUTE) + " 分钟前"; } return Math.floor(timeDiff / SECOND) + " 秒前"; } })();