v2ex user tag

v2ex用户标记

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         v2ex user tag
// @namespace    https://greasyfork.org/zh-CN/scripts/437891-v2ex-user-tag
// @version      0.2
// @description  v2ex用户标记
// @author       yatzh
// @match        *://*.v2ex.com/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @license MIT
// ==/UserScript==
 
(function () {
    'use strict';
 
    // store key | 加上版本号,方便后续更改存储格式
    const TAG_JSON_STR_STORE_KEY = 'plugin.user_tag.tag_json_str.v0.1';
    const TAG_STYLE = 'border: 1px solid #FF1744;color:#FF1744;border-radius:3px;padding:1px 3px;margin:1px 3px';
    const TAG_EDIT_CONTAINER_STYLE = 'background:#7e57c2; padding:5px; border-radius:10px';
    const TAG_EDIT_LABEL_STYLE = 'color:white;font-size:16px;margin:5px;';
    
    function getTagMap() {
        let tagJsonStr = GM_getValue(TAG_JSON_STR_STORE_KEY, "{}");
        return JSON.parse(tagJsonStr);
        // return {"zw1one" : "标签A,标签B", "Jooooooooo" : "", "Illusionary": "112"};
    }
 
    function saveTagMap(tagMap) {
        GM_setValue(TAG_JSON_STR_STORE_KEY, JSON.stringify(tagMap));
    }
 
    function tagUserName(username, tagMap) {
        if (tagMap[username.innerText]) {
            let tagValue = tagMap[username.innerText];
            let userTags = tagValue.split(',');
            for (let userTag of userTags) {
                let oneTag = document.createElement('span');
                oneTag.textContent = userTag;
                oneTag.setAttribute('style', TAG_STYLE);
                username.parentElement.insertBefore(oneTag, username)
            }
        }
    }
 
    // let url = document.URL;
    let path = location.pathname
    let tagMap = getTagMap();
 
    if (path == '/' || path.startsWith('/go/') || path.startsWith('/tag/')) {
        // 首页及类首页
        let home_list = document.getElementsByClassName('topic_info');
        let len = home_list.length;
        for (let i = 0; i < len; i++) {
            let username = path === '/' || path.startsWith('/tag/') ? home_list[i].children[2] : home_list[i].children[0];
            tagUserName(username, tagMap);
        }
    } else if (path.startsWith('/t/')) {
        // 帖子详情页
 
        // 主题
        let opUsername = document.getElementsByTagName('small')[0].children[0];
        tagUserName(opUsername, tagMap);
        // 回复
        let comments = document.getElementsByClassName('cell');
        for (let i = 0; i < comments.length; i++) {
            if (comments[i].id.substring(0, 2) != 'r_') {
                continue;
            }
            let username = comments[i].getElementsByTagName('strong')[0];
            tagUserName(username, tagMap);
        }
    } else if (path.startsWith('/member/')) {
        // 个人主页
        let username = document.getElementsByTagName('h1')[0];
        tagUserName(username, tagMap);
 
        // 标签编辑
        let usernameStr = username.innerText;
        let editContainer = document.createElement('div');
        editContainer.setAttribute('style', TAG_EDIT_CONTAINER_STYLE);
 
        let editLabel = document.createElement('span');
        editLabel.setAttribute('style', TAG_EDIT_LABEL_STYLE);
        editLabel.innerText = "标签编辑:";
        editContainer.appendChild(editLabel);
 
        let editTagContent = document.createElement('textarea');
        editTagContent.setAttribute('id', 'editTagContent');
        editTagContent.setAttribute('style', 'width:90%; height:50px');
        editTagContent.setAttribute('placeholder', '标签,英文逗号分割,如: sb,zz');
        editTagContent.value = tagMap[usernameStr] ? tagMap[usernameStr] : "";
        editContainer.appendChild(editTagContent);
        
        let saveBtn = document.createElement('input');
        saveBtn.setAttribute('type', 'button');
        saveBtn.setAttribute('id', 'saveBtn');
        saveBtn.setAttribute('value', '保存标签');
        saveBtn.setAttribute('class', 'normal button');
        editContainer.appendChild(saveBtn);
 
        document.getElementsByTagName('h1')[0].parentElement.appendChild(editContainer);
        document.getElementById('saveBtn').onclick = function() {
            let newTagContent = editTagContent.value;
            if (confirm('确认要保存?[' + usernameStr + ']的标签[' + newTagContent + ']?')) {
                tagMap[usernameStr] = newTagContent;
                saveTagMap(tagMap);
                location.reload();
            }
        };
    }
})();