v2ex user tag

v2ex用户标记

当前为 2022-01-01 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         v2ex user tag
// @namespace    https://greasyfork.org/zh-CN/scripts/
// @version      0.1
// @description  v2ex用户标记
// @author       hellothankyou
// @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.getElementsByClassName('fr')[0].parentElement.appendChild(editContainer);
        document.getElementById('saveBtn').onclick = function() {
            let newTagContent = editTagContent.value;
            if (confirm('确认要保存?[' + usernameStr + ']的标签[' + newTagContent + ']?')) {
                tagMap[usernameStr] = newTagContent;
                saveTagMap(tagMap);
                location.reload();
            }
        };
    }
})();