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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         快捷键复制 MarkDown 格式的超链接或标题
// @namespace    https://greasyfork.org/users/518374
// @version      0.3
// @description  用快捷键复制 MarkDown 格式的超链接或标题到剪贴板
// @author       InMirrors
// @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.cssText = `
        position : fixed;
        left : 50%;
        bottom : 30px;
        padding : 10px;
        background : lightgreen;
        opacity : 0.8;
        border-radius : 20px;
        box-shadow: 0px 0px 3px teal;
        font-weight : bold;
        font-size:15px;
        z-index : 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 条件实现自定义快捷键,键值请参见:https://keycode.info/
        if (event.altKey && event.keyCode == 82) {
            copyWithConfirmation(document.title);
        }
        if (event.shiftKey && event.altKey && event.keyCode == 82) {
            copyWithConfirmation(`[${document.title}](${document.URL})`);
        }
        if (event.altKey && event.keyCode == 72) {
            copyWithConfirmation(`<a href="${document.URL}">${document.title}</a>`);
        }
    }
})();