v2ex unread

add unread mark for **visited** post of v2ex.com

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         v2ex unread
// @name:zh-CN   v2ex 标记未读 新回复
// @namespace    https://github.com/axipo/v2exUnread
// @version      0.3
// @description  add unread mark for **visited** post of v2ex.com
// @description:zh-CN 给v2ex增加未读标记的油猴脚本
// @author       axipo
// @match        https://*.v2ex.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    if(location.pathname === '/' || location.pathname === '/my/topics'){
        // read visit log
        let containers = document.querySelectorAll("#Main .topic_info");
        containers = Array.prototype.slice.apply(containers);
        let links = document.querySelectorAll("#Main .topic-link");
        links = Array.prototype.slice.apply(links);
        let replies = links.map(link => /#reply(\d+)/.exec(link.href)[1]);
        let postIds = links.map(link => /\/t\/([0-9]+)/.exec(link.href)[1]);
        containers.forEach((container, index) => {
            if(!replies[index]){
                return
            }
            let oldReply = localStorage.getItem('post' + postIds[index])
            oldReply = parseInt(oldReply ? oldReply : 0)
            if(!oldReply){
                return
            }
            let recentReply = replies[index] - oldReply
            if(!recentReply){
                return
            }
            let span = document.createElement('span')
            span.innerText = recentReply + ' new'
            span.style = `
                margin: 0 0.5rem;
                color: white;
                background-color: #f56c6c;
                padding: 0px 6px;
                border-radius: 6px / 50%;
                font-weight: bold;
            `
            container.appendChild(span)
        })
        return
    }

    var regRes = /\/t\/([0-9]+)$/.exec(location.pathname);


    if(regRes){
        let postId = regRes[1]
        let text = document.querySelector("#Main > .box:nth-child(4) > .cell:nth-child(1)").innerText;
        let recentReply = /(\d+) 条回复/.exec(text)[1];
        localStorage.setItem('post' + postId, recentReply);
    }
})();