Douban Username Alias Modifier

Modify Douban usernames in-script CSV format data. Limitations: (1) Only works on PC Douban (https://www.douban.com), and (2) Supports in-script CSV format data only, as Tampermonkey cannot access local files due to security restrictions.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Douban Username Alias Modifier
// @namespace    http://tampermonkey.net/
// @version      1.0
// @author       momobitesthedust
// @match        https://www.douban.com/*
// @description  Modify Douban usernames in-script CSV format data. Limitations: (1) Only works on PC Douban (https://www.douban.com), and (2) Supports in-script CSV format data only, as Tampermonkey cannot access local files due to security restrictions.
// @license      MIT
// ==/UserScript==


(function() {
    'use strict';

    const userCsvData = `
user_id,alias
123,abc
// Add more user ids and aliases as needed
`;

    const userAliasMap = parseCSV(userCsvData);
    modifyUsernameAlias(userAliasMap);

    function parseCSV(data) {
        const lines = data.trim().split('\n');
        const result = {};
        lines.slice(1).forEach(line => {
            const [user_id, alias] = line.split(',');
            if (user_id && alias) {
                result[user_id.trim()] = alias.trim();
            }
        });
        return result;
    }

    function modifyUsernameAlias(userAliasMap) {
        const anchors = document.querySelectorAll('a[href*="https://www.douban.com/people/"]');
        anchors.forEach(anchor => {
            if (anchor.querySelector('img')) {
                return;
            }
            const userIdMatch = anchor.href.match(/people\/([^\/]+)\//);
            if (userIdMatch && userAliasMap[userIdMatch[1]]) {
                const alias = userAliasMap[userIdMatch[1]];
                anchor.textContent = `${anchor.textContent} [${alias}]`;
            }
        });
    }

})();