Greasy Fork

来自缓存

水源签名档

为水源添加签名档功能

安装此脚本?
作者推荐脚本

您可能也喜欢水源助手

安装此脚本
// ==UserScript==
// @name         水源签名档
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  为水源添加签名档功能
// @author       You
// @match        https://shuiyuan.sjtu.edu.cn/*
// @license      GPL-3.0-only
// @grant none
// ==/UserScript==
(async ()=> {
    'use strict';

    // Your code here...
    let checked = false
    await waitForPage()
    spawnObserver(document.querySelector("#reply-control"),(records)=>{
        if(records.find((e)=>e.addedNodes[0]?.className === "d-editor-button-bar")){
            document.querySelector("div.save-or-cancel > button.create").insertAdjacentHTML("afterend",`<div style="margin: 1vh 0vw 1vh 2vw;width:20vw;max-width:20em">
            <input type="checkbox" id="appendSignature" name="appendSignature" /><label for="signature" style = "display:inline;">签名档:</label>
    <textarea id="signature" name="signature" style="width:100%;height:5vh"/>
    </div>`)
            document.querySelector("#signature").value = localStorage.getItem("shuiyuan_signature")??""
            document.querySelector("#appendSignature").addEventListener("click", function(){localStorage.setItem('shuiyuan_doAppendSignature', this.checked?"checked":"")});
            document.querySelector("#appendSignature").checked = localStorage.getItem('shuiyuan_doAppendSignature')==="checked";
        }})
    window.require("discourse/models/rest").default.prototype.beforeCreate = function(props){
        const signature = document.querySelector("#signature").value
        localStorage.setItem("shuiyuan_signature", signature)
        console.log(signature)
        checked =document.querySelector("#appendSignature").checked
        if(checked){
            if(signature!==""){
                props.raw = props.raw + `\n<div data-signature>\n\n---\n${signature}\n</div>`
                props.hasSignature = true
            }
        }
    }
})();
function waitForPage(){
    return new Promise((resolve, reject)=>{
        if(document.querySelector("#main-outlet-wrapper")!==null) resolve()
        else {
            spawnObserver(document.documentElement, (records, observer)=>{
                if(records.find((e)=>e.addedNodes[0]?.id === "main-outlet-wrapper")){
                    observer.disconnect()
                    resolve()
                }})
        }
    })
}
function spawnObserver(element, callback){
    const replyEditorObserver = new MutationObserver((records,observer)=>{callback(records, observer)});
    replyEditorObserver.observe(element,{subtree: true, childList: true, attributes:false });
}