Copy URL as Markdown Link

Right-click on a blank area of the page to copy the current page's title and URL as a Markdown formatted link to the clipboard.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name                Copy URL as Markdown Link
// @name:zh-CN          复制 Markdown 格式超链接
// @version             1.0.1
// @description         Right-click on a blank area of the page to copy the current page's title and URL as a Markdown formatted link to the clipboard.
// @description:zh-CN   在页面中空白位置右键,将当前页面的标题和 URL 以 Markdown 格式复制到剪贴板。
// @namespace           https://github.com/yxzlwz/browser_scripts
// @author              yxzlwz
// @match               *://*/*
// @grant               GM_registerMenuCommand
// ==/UserScript==

GM_registerMenuCommand("Generally", function () {
    copyMarkdownLink(false);
});

GM_registerMenuCommand("Without Query", function () {
    copyMarkdownLink(true);
});

GM_registerMenuCommand("Title Only", function () {
    navigator.clipboard.writeText(document.title);
});

function copyMarkdownLink(ignoreQuery) {
    let url = window.location.href;
    const title = document.title;

    if (ignoreQuery) {
        url = url.split("?")[0];
    }

    const markdownLink = `[${title}](${url})`;

    navigator.clipboard
        .writeText(markdownLink)
        .then(() => {
            console.log("URL copied as markdown link!");
        })
        .catch(error => {
            console.error("Error copying URL as markdown link:", error);
        });
}