新增脚本 tampermonkey/v2ex.com/v2ex-mark-read.js 标记 V2ex 中已读的帖子标题,如果有新回复,帖子标题将会恢复未读状态。

main
greatbody 2023-02-15 11:27:53 +08:00
parent 38519fc9dc
commit b40c59ebe2
No known key found for this signature in database
GPG Key ID: 01CEB6267272A9A5
1 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,103 @@
// ==UserScript==
// @name V2ex Helper - Mark read
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Mark read posts with background color and save state in json.
// @author You
// @match https://v2ex.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=v2ex.com
// @require https://static.sunrui.ink:8888/tampermonkey.js?t=23423367
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-start
// @license MIT
// ==/UserScript==
(function () {
// 'use strict';
GM_addStyle(`
.cell.read {
background-color: antiquewhite;
}
`);
function getWebsite() {
// Get website domain
return window.location.hostname;
}
function getJsonDataObject() {
return JSON.parse(GM_getValue(getWebsite(), '{}'));
}
function setUrlRead(url) {
// Set url as read
var data = getJsonDataObject();
data[url] = true;
GM_setValue(getWebsite(), JSON.stringify(data));
}
function isUrlRead(url) {
// Check if url is read
var data = getJsonDataObject();
return data[url] === true;
}
function getMainPageCells() {
// Get main page cells
return document.querySelectorAll('#Main div.cell.item');
}
function getTopicsPageCells() {
// Get topics page cells
return document.querySelectorAll('#TopicsNode .cell');
}
function getCells() {
const mainCells = getMainPageCells();
const topicsCells = getTopicsPageCells();
if (mainCells.length > 0) {
return mainCells;
}
if (topicsCells.length > 0) {
return topicsCells;
}
return [];
}
function findLinks() {
return Array.from(getCells()).map((cell) => { return cell.querySelector('.item_title a').href; });
}
var pm = new PageManager();
const condition = () => {
return findLinks().length > 0;
}
// record current page as read
setUrlRead(window.location.href);
pm.stage('Add color to viewd topics')
.on(condition)
.logLevel(1)
.act((logger) => {
logger('Add color to viewd topics');
// find all cells
var cells = getCells();
// add color to read topics
Array.from(cells).forEach((cell) => {
const link = cell.querySelector('.item_title a').href;
logger(`${link} is ${isUrlRead(link) ? 'read' : 'unread'}`);
if (isUrlRead(link)) {
cell.classList.add('read');
}
});
logger('Done', 2);
return false;
})
.every(1000)
.run();
})();