// ==UserScript== // @name 水源显示回复可见 // @namespace CCCC_David // @version 0.2.0 // @description 可在水源论坛显示仅回复可见的回帖内容(需要手动点击按钮) // @author CCCC_David // @match https://shuiyuan.sjtu.edu.cn/* // @grant none // @downloadURL none // ==/UserScript== (async () => { 'use strict'; // From Font Awesome Free v5.15 by @fontawesome - https://fontawesome.com // License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) // Modified class attribute to fit in. const prevReplyIcon = ''; const nextReplyIcon = ''; const allowedPolicy = window.trustedTypes?.createPolicy?.('allowedPolicy', {createHTML: (x) => x}); const createTrustedHTML = (html) => (allowedPolicy ? allowedPolicy.createHTML(html) : html); const htmlParser = new DOMParser(); const escapeRegExpOutsideCharacterClass = (s) => s.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&'); const discourseFetch = (url, options) => fetch(url, { method: options?.method ?? 'GET', headers: { 'Discourse-Present': 'true', 'Discourse-Logged-In': 'true', 'X-Requested-With': 'XMLHttpRequest', 'X-CSRF-Token': document.querySelector('meta[name=csrf-token]').content, ...options?.headers, }, body: options?.body, mode: 'same-origin', credentials: 'include', }); const fetchReply = async (topicId, replyId) => { const replyURL = `/t/topic/${topicId}/${replyId}`; const response = await discourseFetch(`/onebox?url=${encodeURIComponent(replyURL)}`); return response.text(); }; const fetchTopicInfo = async (topicId) => { const [topicInfo1, topicInfo2] = await Promise.all([ `/t/${topicId}.json`, `/latest.json?topic_ids=${topicId}`, ].map(async (url) => (await discourseFetch(url, {headers: {Accept: 'application/json'}})).json())); return { isPrivateReply: topicInfo1.private_replies, maxReplyId: topicInfo2.topic_list.topics[0].highest_post_number, participants: topicInfo1.details?.participants ?? [], }; }; const fetchUserInfo = async (username) => { if (!username) { return { username: null, name: null, title: null, }; } const response = await discourseFetch(`/u/${encodeURIComponent(username)}/card.json`, {headers: {Accept: 'application/json'}}); const userInfo = await response.json(); return { username: userInfo.user.username, name: userInfo.user.name, title: userInfo.user.title, }; }; const renderReply = (tree) => { const blockquote = tree.querySelector('aside > blockquote'); if (!blockquote) { return '(帖子已被作者删除)'; } const reply = blockquote.innerHTML.trim(); if (!reply) { return ' '; } // eslint-disable-next-line no-undef return require('discourse/lib/text').cookAsync(reply); }; const getAvatarURLFromReply = (tree) => tree.querySelector('aside > div > img')?.src ?? ''; const tryToFindUserByAvatar = async (avatarURL, participants) => { // Case 1: Extract username from URL if it is user avatar. const username = avatarURL.match(/\/user_avatar\/(?:[^/]+)\/([^/]+)\//u)?.[1]; if (username) { return { username: decodeURIComponent(username), name: null, title: null, }; } // Case 2: Try to match letter avatar with top participants of this topic. const letterAvatarMatch = avatarURL.match(/\/letter_avatar_proxy\/(?:[^/]+)\/letter\/([^/]+)\/([^/]+)\//u); // Letter and color if (!letterAvatarMatch) { // Unexpected format. return { username: null, name: null, title: null, }; } const letterAvatarTag = ['letter', letterAvatarMatch[1], letterAvatarMatch[2]].join('/'); for (const participant of participants) { if ((participant.avatar_template ?? '').includes(letterAvatarTag)) { return { username: participant.username, name: participant.name, title: null, }; } } // Case 3: Search for users with the letter and try to match letter avatar. const searchUsersByLetterResult = await (await discourseFetch(`/directory_items?period=all&order=username&name=${encodeURIComponent(decodeURIComponent(letterAvatarMatch[1]))}`)).json(); for (const user of searchUsersByLetterResult.directory_items) { if ((user.user.avatar_template ?? '').includes(letterAvatarTag)) { return { username: user.user.username, name: user.user.name, title: user.user.title, }; } } // We are unable to figure out the username by avatar. return { username: null, name: null, title: null, }; }; const addViewer = async (parentNode) => { if (!parentNode) { return null; } // Do not add the viewer more than once for a page. if (document.getElementById('show-private-reply-div')) { return null; } const topicId = parseInt(window.location.pathname.match(/^\/t\/topic\/(\d+)(?=\/|$)/u)?.[1], 10); if (Number.isNaN(topicId)) { // Unable to parse topic ID, maybe not a topic page, give up. return null; } const {isPrivateReply, maxReplyId, participants} = await fetchTopicInfo(topicId); // Do not add viewer if current topic is not private reply. // Double check for race condition after await. if (!isPrivateReply || document.getElementById('show-private-reply-div')) { return null; } const viewerContainer = document.createElement('div'); viewerContainer.id = 'show-private-reply-div'; viewerContainer.innerHTML = createTrustedHTML(`