快捷键复制 MarkDown 格式的超链接或标题

用快捷键复制 MarkDown 格式的超链接或标题到剪贴板

当前为 2023-08-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         快捷键复制 MarkDown 格式的超链接或标题
// @namespace    https://greasyfork.org/users/518374
// @version      0.2
// @description  用快捷键复制 MarkDown 格式的超链接或标题到剪贴板
// @author       InMirrors
// @note         复制 MarkDown 格式的超链:Shitf + Alt + R,修改第 53 行的代码实现自定义,键值请参见:https://keycode.info/
// @note         复制标题:Alt + R,修改第 50 行的代码实现自定义
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// @icon         https://plugins.jetbrains.com/files/18897/166369/icon/pluginIcon.png
// @license      MIT
// ==/UserScript==

(function() {

    'use strict';

    function copyWithConfirmation(text) {
        GM_setClipboard(text);

        // Show confirmation message
        var confirmation = document.createElement("div");
        confirmation.innerHTML = "Copied";
        confirmation.style.position = "fixed";
        confirmation.style.left = "50%";
        confirmation.style.bottom = "30px";
        confirmation.style.padding = "10px";
        confirmation.style.background = "lightgreen";
        confirmation.style.opacity = 0.8;
        confirmation.style.borderRadius = "20px";
        confirmation.style.zIndex = 999;

        document.body.appendChild(confirmation);

        // Remove after 2 seconds
        setTimeout(function() {
            confirmation.remove();
        }, 2000);
    }

    GM_registerMenuCommand("复制标题及链接", () => copyWithConfirmation(`[${document.title}](${document.URL})`));

    GM_registerMenuCommand("仅复制标题", () => copyWithConfirmation(document.title));

    GM_registerMenuCommand("仅复制链接", () => copyWithConfirmation(document.URL));

    document.onkeydown = function(event) {
        if (event.altKey && event.keyCode == 82) {
            copyWithConfirmation(document.title);
        }
        if (event.shiftKey && event.altKey && event.keyCode == 82) {
            copyWithConfirmation(`[${document.title}](${document.URL})`);
        }
    }
})();