Last active
February 10, 2025 17:50
-
-
Save npretto/20735c543e0cc4a4c2e9cae25c6d70c0 to your computer and use it in GitHub Desktop.
user script to copy link to the page as md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Universal Link Copier (GitHub, Notion, and Others) | |
| // @namespace ViolentMonkey Scripts | |
| // @version 1.3 | |
| // @description Copy the link with relevant information from GitHub issues, pull requests, and other pages when nothing is selected and cmd+c is pressed. | |
| // @author You | |
| // @match *://github.com/*/*/* | |
| // @match *://notion.so/* | |
| // @match *://*/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| // Function to copy the link based on the page content | |
| function copyLink() { | |
| const url = new URL(document.location); | |
| let link = ""; | |
| // Emojis for GitHub issues and PRs | |
| const emoji = { | |
| issues: ":github-issue:", | |
| pull: ":pull-request:", | |
| draft: ":draft-pull-request:", | |
| merged: ":merged:", | |
| }; | |
| // If the page is a GitHub issue or pull request | |
| if (url.host === "github.com" && /\/.*\/(issues|pull)\/(\d+)/.test(url.pathname)) { | |
| const m = url.pathname.match(/\/.*\/(issues|pull)\/(\d+)/); | |
| if (m) { | |
| let [_, type, id] = m; | |
| // Try to get the issue title from data-testid="issue-title" first | |
| const titleElement = document.querySelector('[data-testid="issue-title"]'); | |
| const title = titleElement ? titleElement.textContent : (document.querySelector(".js-issue-title")?.textContent || "No Title"); | |
| const isDraft = !!document.querySelector('.gh-header-meta [title="Status: Draft"]'); | |
| const isMerged = !!document.querySelector('.gh-header-meta [title="Status: Merged"]'); | |
| if (isDraft) type = "draft"; | |
| if (isMerged) type = "merged"; | |
| link = `${emoji[type] || emoji.pull} [${title.replaceAll("[", "【").replaceAll("]", "】")}](${url.href})`; | |
| console.log("GitHub link", link); | |
| } | |
| } | |
| // If the page is a Notion page | |
| else if (url.host.includes("notion.so")) { | |
| const title = document.querySelector("h1")?.innerText || "No Title"; | |
| link = `[${title.replaceAll("[", "【").replaceAll("]", "】")}](${url.href})`; | |
| console.log("Notion link", link); | |
| } | |
| // Fallback for other pages (just title and URL) | |
| else { | |
| const title = document.title || "No Title"; | |
| link = `[${title}](${url.href})`; | |
| console.log("Fallback link", link); | |
| } | |
| // Copy the link to the clipboard | |
| navigator.clipboard.writeText(link).then(() => { | |
| console.log("Link copied:", link); | |
| }).catch((err) => { | |
| console.error("Failed to copy text:", err); | |
| }); | |
| } | |
| // Event listener for "cmd+c" when nothing is selected | |
| document.addEventListener('copy', function(e) { | |
| const selection = window.getSelection().toString(); | |
| if (!selection) { // Only trigger if no text is selected | |
| e.preventDefault(); | |
| copyLink(); // Trigger the custom copy function | |
| } | |
| }); | |
| })(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took ispiration from https://gist.github.com/iethree/9e39fd79e3d2e7f55aed39643066be59