Last active
January 15, 2025 07:04
-
-
Save wafe/b2c88189a6b25a241b1142a627e81b32 to your computer and use it in GitHub Desktop.
Browser UserScript, Copy the URL and title of the current tab as a Markdown link
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 Copy URL and Title as Markdown | |
// @namespace Violentmonkey Scripts | |
// @match *://*/* | |
// @version 1.1 | |
// @author Heejoon Lee | |
// @description Copy the URL and title of the current tab as a Markdown link | |
// @grant GM_setClipboard | |
// @grant GM_registerMenuCommand | |
// @grant GM_addStyle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Add styles for the toast | |
GM_addStyle(` | |
#markdown-toast { | |
visibility: hidden; | |
min-width: 250px; | |
margin-left: -125px; | |
background-color: #333; | |
color: #fff; | |
text-align: center; | |
border-radius: 2px; | |
padding: 16px; | |
position: fixed; | |
z-index: 9999; | |
left: 50%; | |
top: -50px; | |
font-size: 14px; | |
transition: top 0.5s, visibility 0.5s; | |
} | |
#markdown-toast.show { | |
visibility: visible; | |
top: 30px; | |
} | |
`); | |
// Function to show toast | |
function showToast(message) { | |
let toast = document.getElementById("markdown-toast"); | |
if (!toast) { | |
toast = document.createElement("div"); | |
toast.id = "markdown-toast"; | |
document.body.appendChild(toast); | |
} | |
toast.textContent = message; | |
toast.classList.add("show"); | |
setTimeout(() => { | |
toast.classList.remove("show"); | |
}, 1500); | |
} | |
// Function to copy the URL and title as a Markdown link | |
function copyMarkdownLink() { | |
const title = document.title; | |
const url = window.location.href; | |
const markdownLink = `[${title}](${url})`; | |
// Use GM_setClipboard to copy the text to the clipboard | |
GM_setClipboard(markdownLink); | |
//alert('Markdown link copied to clipboard!'); | |
showToast('Markdown 링크가 클립보드에 복사되었습니다!'); | |
} | |
// Function to copy title and URL in two lines | |
function copyTitleAndUrl() { | |
const title = document.title; | |
const url = window.location.href; | |
const text = `${title}\n${url}`; // 제목 + 줄바꿈 + URL | |
GM_setClipboard(text); | |
showToast('제목 + URL이 클립보드에 복사되었습니다!'); | |
} | |
// Register the menu command to add a button to the userscript manager's toolbar | |
GM_registerMenuCommand("Markdown 링크 복사", copyMarkdownLink); | |
GM_registerMenuCommand("제목 + URL 복사", copyTitleAndUrl); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment