Skip to content

Instantly share code, notes, and snippets.

@wafe
Created August 6, 2024 01:50
Show Gist options
  • Save wafe/b2c88189a6b25a241b1142a627e81b32 to your computer and use it in GitHub Desktop.
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
// ==UserScript==
// @name Copy URL and Title as Markdown
// @namespace Violentmonkey Scripts
// @match *://*/*
// @version 1.0
// @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 링크가 클립보드에 복사되었습니다!');
}
// Register the menu command to add a button to the userscript manager's toolbar
GM_registerMenuCommand("Markdown 링크 복사", copyMarkdownLink);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment