Skip to content

Instantly share code, notes, and snippets.

@greatghoul
Created March 30, 2025 12:57
Show Gist options
  • Save greatghoul/c1d5562cd638c1f023335a2dfdcff228 to your computer and use it in GitHub Desktop.
Save greatghoul/c1d5562cd638c1f023335a2dfdcff228 to your computer and use it in GitHub Desktop.
豆瓣小组管理助手
// ==UserScript==
// @name 豆瓣小组管理助手
// @namespace https://anl.gg/
// @version 2025-03-26
// @description 改进豆瓣小组管理功能,保存用户申请加入群组的备注
// @author greatghoul
// @match https://www.douban.com/group/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=douban.com
// @require https://code.jquery.com/jquery-3.7.1.min.js
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_getValues
// @grant GM_listValues
// ==/UserScript==
/* global $ */
(function() {
'use strict';
const cache = {};
const PAGE_PATTERNS = {
GROUP_REQUEST: /^\/group\/(.*?)\/requests\/$/,
GROUP_SUB_PAGE: /^\/group\/(.*?)\/.*$/,
};
function log(...args) {
console.log('[豆瓣小组助手]', ...args);
}
function matchPage(pathPattern) {
const pathname = window.location.pathname;
return pathname.match(pathPattern);
}
function parseUserId(url) {
return url.match(/people\/([^/]+)\/?/)[1];
}
function saveUserNote(userId, groupId, userNote) {
log('Saving user note:', { userId, groupId, userNote });
const userKey = `users.${userId}`;
const user = GM_getValue(userKey, {})
user.groupRequests = user.groupRequests || {};
user.groupRequests[groupId] = userNote;
GM_setValue(userKey, user);
}
function handleGroupRequestBackup(groupId) {
$('.group-request-list > li').has(':checked').each((i, request) => {
const $request = $(request);
const userLink = $request.find('.group-request-userface > a').attr('href');
const userId = parseUserId(userLink);
const userNote = $request.find('.group-request-content').text().trim();
saveUserNote(userId, groupId, userNote);
});
initGroupRequestReader();
}
function initGroupReqeustSaver() {
const match = matchPage(PAGE_PATTERNS.GROUP_REQUEST);
if (!match) return;
const groupId = match[1];
$('<button />', {
type: 'button',
text: '保存备注',
title: '将入群申请备注到用户信息,方便日后查看',
click: () => handleGroupRequestBackup(groupId)
}).appendTo('.list-opt');
}
function prepareUserNote(groupId, userLink) {
const $userLink = $(userLink);
const userId = parseUserId($userLink.attr('href'));
const userKey = `users.${userId}`;
const user = cache[userKey] || GM_getValue(userKey, {});
const { groupRequests = {} } = user;
const requestNote = groupRequests[groupId];
if (requestNote) {
$userLink.attr('title', requestNote);
$userLink.toggleClass('anl__user_note', true);
}
}
function initGroupRequestReader() {
const match = matchPage(PAGE_PATTERNS.GROUP_SUB_PAGE);
if (!match) return;
const groupId = match[1];
$('a[href^="https://www.douban.com/people"]:not(.anl__user_note)').each((i, userLink) => {
prepareUserNote(groupId, userLink);
});
}
function init() {
initGroupReqeustSaver();
initGroupRequestReader();
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment