Greasy Fork

VoidPaste

Auto transform pasted image links.

目前为 2023-10-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         VoidPaste
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Auto transform pasted image links.
// @author       voidnyan
// @match        https://anilist.co/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const imageUrls = [
        "https://i.ibb.co"
    ];
    const imageWidth = "320";

    let isShiftPressed = false;

    window.addEventListener("keydown", (event) => {
        if (event.key !== "Shift") {
            return;
        }

        isShiftPressed = true;
    });

    window.addEventListener("keyup", (event) => {
        if (event.key !== "Shift") {
            return;
        }

        isShiftPressed = false;
    });

    window.addEventListener("paste", (event) => {
        const clipboard = event.clipboardData.getData("text/plain").trim();

        if (!clipboard.startsWith(imageUrls) || !isShiftPressed){
            return;
        }

        event.preventDefault();
        let transformedClipboard = "";
        const urlList = clipboard.split("\n");

        for (const url of urlList){
            transformedClipboard += `[ img${imageWidth}(${url}) ](${url})\n\n`;
        }

        window.document.execCommand('insertText', false, transformedClipboard);
    });

})();