Markdown Link

One click to copy a markdown link of current page.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Markdown Link
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  One click to copy a markdown link of current page.
// @author       YourName
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create and show a hovering, styled div with the page title
    function displayHoveringTitle() {
        const pageTitle = document.title; // Get the current page title
        const pageUrl = new URL(window.location.href);

        // Optionally, clean the URL by removing search parameters and hash
        pageUrl.search = ""; // Remove query strings
        pageUrl.hash = ""; // Remove fragment identifier

        // Create the markdown link for copy purpose only
        const markdownLink = `[${pageTitle}](${pageUrl.href})`;

        // Create a div element for displaying the title
        const floatingDiv = document.createElement('div');
        floatingDiv.textContent = pageTitle; // Set the content to page title only
        floatingDiv.style.position = 'fixed';
        floatingDiv.style.bottom = '10px';
        floatingDiv.style.right = '10px';
        floatingDiv.style.backgroundColor = 'rgba(0,0,0,0.5)'; // Semi-transparent black background
        floatingDiv.style.color = 'white';
        floatingDiv.style.padding = '10px';
        floatingDiv.style.borderRadius = '5px';
        floatingDiv.style.zIndex = '1000';
        floatingDiv.style.cursor = 'pointer'; // Change cursor to pointer to indicate clickable

        // Append the div to the body of the page
        document.body.appendChild(floatingDiv);

        // Add click event listener to copy markdown link to clipboard
        floatingDiv.addEventListener('click', function() {
            navigator.clipboard.writeText(markdownLink).then(() => {
                console.log('Markdown link copied to clipboard!');
            }).catch(err => {
                console.error('Failed to copy markdown link: ', err);
            });
        });
    }

    // Add an event listener to call the function when the userscript is loaded
    window.addEventListener('load', displayHoveringTitle);
})();