Copy Title and URL as Markdown

Copy current page title and URL in Markdown format using a hotkey or button

目前為 2024-08-19 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Copy Title and URL as Markdown
// @namespace    https://lynelluo.github.io/
// @version      1.03
// @description  Copy current page title and URL in Markdown format using a hotkey or button
// @author       lynel0625
// @match        *://*/*
// @grant        none
// @icon         https://raw.githubusercontent.com/Lynelluo/scriptastic-toolbox/main/scripts/Copy%20Title%20and%20URL%20as%20Markdown.png
// @license MIT
// ==/UserScript==
(function() {
    // Function to copy title and URL as Markdown
    function copyMarkdown() {
        var title = document.title;
        var url = window.location.href;
        var markdown = `[${title}](${url})`;

        var textarea = document.createElement("textarea");
        textarea.value = markdown;
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand("copy");
        document.body.removeChild(textarea);

        showNotification("Copied as Markdown: " + markdown);
    }

    // Function to show a notification
    function showNotification(message) {
        var notification = document.createElement("div");
        notification.innerText = message;
        notification.style.position = "fixed";
        notification.style.bottom = "50px";
        notification.style.left = "10px";
        notification.style.backgroundColor = "#333";
        notification.style.color = "#fff";
        notification.style.padding = "10px";
        notification.style.borderRadius = "5px";
        notification.style.zIndex = 1001;
        notification.style.fontSize = "14px";
        notification.style.opacity = "0.9";
        document.body.appendChild(notification);

        // Automatically remove the notification after 2 seconds
        setTimeout(function() {
            document.body.removeChild(notification);
        }, 2000);
    }

    // Add event listener for 'Ctrl + Shift + y' or 'Command + Shift + y' shortcut
    document.addEventListener('keydown', function(e) {
        // console.log("Key pressed:", e.key, "Ctrl:", e.ctrlKey, "Shift:", e.shiftKey, "Meta:", e.metaKey); // Debugging: log key events
        if ((e.key === 'Y' && e.shiftKey && (e.ctrlKey || e.metaKey))) {
            // console.log("Shortcut detected: Ctrl/Command + Shift + Y"); // Debugging: log when shortcut is detected
            e.preventDefault(); // Uprevent default action
            copyMarkdown();
        }
    });

    // Create a button to trigger the copy action
    var button = document.createElement("button");
    button.innerText = "Copy as Markdown";
    button.style.position = "fixed";
    button.style.bottom = "10px";
    button.style.left = "10px"; // Place the button in the bottom-left corner
    button.style.zIndex = 1000; // Make sure the button is on top of other elements
    document.body.appendChild(button);

    // Add event listener for the button
    button.addEventListener("click", function() {
        copyMarkdown();
    });
})();