豆瓣用户主页记录

为每个豆瓣用户创建独立的记录,存储用户名、用户ID和网址到Tampermonkey的后台,双击进入编辑模式

目前為 2024-10-24 提交的版本,檢視 最新版本

// ==UserScript==
// @name         豆瓣用户主页记录
// @version      0.0.1
// @description  为每个豆瓣用户创建独立的记录,存储用户名、用户ID和网址到Tampermonkey的后台,双击进入编辑模式
// @author       ✌
// @match        https://www.douban.com/people/*
// @grant        GM_setValue
// @grant        GM_getValue
// @namespace https://gf.qytechs.cn/users/1384897
// ==/UserScript==

(function() {
    'use strict';

    const userIDElement = document.querySelector('#profile > div > div.bd > div.basic-info > div > div');

    if (!userIDElement) {
        console.error('未找到用户ID元素');
        return;
    }

    const userID = userIDElement.innerText.split('\n')[0].trim(); // 取出第一行作为userID

    const userNameElement = document.querySelector('#db-usr-profile h1');
    const userName = userNameElement.innerText.trim();

    const userURL = window.location.href;

    const profileHeader = document.querySelector('#db-usr-profile h1');

    // 创建新的div容器,用于放置输入框和保存按钮
    const noteContainer = document.createElement('div');
    noteContainer.style.marginTop = '10px';
    noteContainer.style.display = 'flex';

    // 创建输入框,输入关于用户的记录
    const noteInput = document.createElement('textarea');
    noteInput.rows = 4;
    noteInput.cols = 50;
    noteInput.placeholder = '记录关于该用户...';
    noteInput.style.flexGrow = '1';
    noteInput.style.fontSize = '14px';

    // 使用Tampermonkey的后台存储来加载用户记录
    const savedData = GM_getValue(`user_data_${userID}`, { note: '', name: userName, url: userURL });
    noteInput.value = savedData.note; // 将已保存的记录填入输入框

    // 判断是否有记录,如果有记录,默认不可编辑
    if (savedData.note) {
        noteInput.readOnly = true;
        noteInput.style.backgroundColor = '#f0f0f0';
    }

    // 创建保存按钮
    const saveButton = document.createElement('button');
    saveButton.innerText = '保存';
    saveButton.style.marginLeft = '5px';
    saveButton.style.padding = '2px 5px';
    saveButton.style.fontSize = '11px';
    saveButton.style.height = '30px';

    // 保存按钮点击事件
    saveButton.addEventListener('click', function() {
        const noteContent = noteInput.value;
        if (noteContent) {
            // 保存用户记录,包含ID、用户名、URL、记录
            GM_setValue(`user_data_${userID}`, {
                note: noteContent,
                name: userName,
                url: userURL,
                id: userID
            });

            // 保存后将文本框设置为不可编辑状态,并变灰
            noteInput.readOnly = true;
            noteInput.style.backgroundColor = '#f0f0f0';
        } else {
            alert('请输入内容后再保存');
        }
    });

    // 双击事件:双击文本框可进入编辑模式
    noteInput.addEventListener('dblclick', function() {
        noteInput.readOnly = false;
        noteInput.style.backgroundColor = '#ffffff';
    });

    noteContainer.appendChild(noteInput);
    noteContainer.appendChild(saveButton);

    profileHeader.appendChild(noteContainer);

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址