// ==UserScript==
// @name SegmentFaultNock
// @namespace http://tampermonkey.net/
// @version 0.0.8
// @description BlogNock系列,思否文章的标识优化
// @author Exisi
// @license MIT License
// @match segmentfault.com/a/*
// @supportURL https://github.com/Exisi/BlogNock/issues/
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function () {
"use strict";
const features = {
mark: {
modified_history: {
enabled: GM_getValue("modified_history", true),
selector: ["time[itemprop='datePublished']"],
icon: ``,
},
adjust_position: {
enabled: GM_getValue("adjust_position", true),
selector: ["time[itemprop='datePublished']"],
icon: ``,
},
datetime: {
enabled: GM_getValue("datetime", true),
selector: ["time[itemprop='datePublished']", "script[type='application/ld+json']"],
icon: ``,
},
readtime: {
enabled: GM_getValue("readtime", true),
selector: ["time[itemprop='datePublished']", "article"],
},
},
hidden_first_login_dialog: {
enabled: GM_getValue("hidden_first_login_dialog", true),
},
};
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; } .nock-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; 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(".nock-modal-dialog").style.display = "block");
const closeSetting = () => (document.querySelector(".nock-modal-dialog").style.display = "none");
document.querySelector(".nock-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.modified_history.enabled) {
const datePublished = document.querySelector(features.mark.datetime.selector[0]);
const datetimeItem = datePublished.parentNode;
const history = document.createElement("a");
history.innerHTML = `${features.mark.modified_history.icon}历史修改`;
history.style.cursor = "pointer";
history.style.marginLeft = "10px";
history.style.color = "rgba(108, 117, 125)";
history.style.display = "flex";
history.style.alignItems = "center";
history.addEventListener("click", () => {
document.querySelectorAll(features.mark.modified_history.selector[0])[1].click();
});
datetimeItem.before(history);
const icon = history.querySelector("svg");
icon.style.width = "17px";
icon.style.height = "17px";
icon.style.marginRight = "3px";
}
if (
features.mark.adjust_position.enabled &&
document.querySelector(features.mark.adjust_position.selector[0]).nextSibling
) {
const time = document.querySelector(features.mark.adjust_position.selector[0]);
const position = time.nextSibling;
const positionBox = document.createElement("a");
positionBox.style.display = "flex";
positionBox.style.alignItems = "center";
positionBox.style.marginLeft = "10px";
positionBox.style.color = "rgba(108, 117, 125)";
positionBox.innerHTML = features.mark.adjust_position.icon;
positionBox.appendChild(position);
const icon = positionBox.querySelector("svg");
icon.style.width = "16px";
icon.style.height = "16px";
time.parentNode.after(positionBox);
}
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("datetime");
const rawUpdatedTime = jsonData.dateModified ?? rawPublishedTime;
const postTimeAgo = calculateTimeAgo(rawPublishedTime);
const updateTimeAgo = calculateTimeAgo(rawUpdatedTime);
const postTime = new Date(rawPublishedTime).toLocaleString().replaceAll("/", "-");
const updateTime = new Date(rawUpdatedTime).toLocaleString().replaceAll("/", "-");
const formattedPostTime = `发布于 ${postTime}(${postTimeAgo})`;
const formattedUpdateTime = `编辑于 ${updateTime}(${updateTimeAgo})`;
const datetimeItem = datePublished.parentNode;
datetimeItem.innerHTML = features.mark.datetime.icon;
datePublished.style.textDecoration = "underline";
datePublished.innerHTML = formattedPostTime;
datetimeItem.style.display = "flex";
datetimeItem.style.alignItems = "center";
datetimeItem.appendChild(datePublished);
datetimeItem.addEventListener("click", (e) => {
e.preventDefault();
const text =
datePublished.innerText == formattedPostTime ? formattedUpdateTime : formattedPostTime;
datePublished.innerText = text;
});
const icon = datetimeItem.querySelector("svg");
icon.style.width = "16px";
icon.style.height = "15px";
icon.style.marginRight = "3px";
datePublished.closest("div").style.flexWrap = "wrap";
}
if (features.mark.readtime.enabled) {
const time = document.querySelector(features.mark.readtime.selector[0]);
const article = document.querySelector(features.mark.readtime.selector[1]);
const readtime = time.closest("div").lastChild;
readtime.lastChild.style.textDecoration = "underline";
readtime.style.cursor = "pointer";
readtime.addEventListener("click", () => article.scrollIntoView(false));
}
if (features.hidden_first_login_dialog.enabled) {
document.cookie = "showLogin=true";
}
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) + " 秒前";
}
})();