Copy Link!

Copy the link to current web page, in Markdown or HTML, have the URL decoded to Unicode string, or have the protocol removed.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Copy Link!
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Copy the link to current web page, in Markdown or HTML, have the URL decoded to Unicode string, or have the protocol removed.
// @author       firetree
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    let href

    /**
     * @param {function(string):string} textProvider
     * @param {boolean?} doChain
     */
    function setClipboard(textProvider, doChain) {
        if (typeof href === 'undefined') href = location.href

        let result = textProvider(href)

        GM_setClipboard(result)

        if (doChain) href = result
    }

    function strip(href) {
        return href.replace(/^https?:\/\//, '').replace(/\/$/, '')
    }

    const commands = [
        ['Copy Link', () => setClipboard(href => href)],
        ['Copy Title', () => GM_setClipboard(document.title)],
        ['Decode URL', () => setClipboard(href => decodeURIComponent(href), true)],
        ['Strip', () => setClipboard(href => strip(href))],
        ['Markdown', () => setClipboard(href => `[${document.title}](${href})`)],
        ['Markdown Strip', () => setClipboard(href => `[${strip(href)}](${href})`)],
        ['HTML', () => setClipboard(href => `<a href="${href}">${document.title}</a>`)],
        ['HTML Strip', () => setClipboard(href => `<a href="${href}">${strip(href)}</a>`)],
    ]

    for (let [name, func] of commands) {
        GM_registerMenuCommand(name, func)
    }
})();