Skip to content

Instantly share code, notes, and snippets.

@rf5860
Last active July 10, 2025 15:08
Show Gist options
  • Save rf5860/181f6ef630585d777333a0d79d6dafbb to your computer and use it in GitHub Desktop.
Save rf5860/181f6ef630585d777333a0d79d6dafbb to your computer and use it in GitHub Desktop.
Greasemonkey Extension for creating a Markdown Link to the current page
// ==UserScript==
// @name MarkdownLinker
// @namespace https://github.com/rjf89
// @version 1.0.4
// @description Press a hotkey to copy a Markdown link of the current page to the clipboard.
// @author rjf89
// @match *://*/*
// @grant GM_setClipboard
// @updateURL https://gist.github.com/rf5860/181f6ef630585d777333a0d79d6dafbb/raw/MarkdownLinker.user.js
// @downloadURL https://gist.github.com/rf5860/181f6ef630585d777333a0d79d6dafbb/raw/MarkdownLinker.user.js
// ==/UserScript==
(function () {
'use strict';
const CONFIG = {
MARKDOWN: {
hotkey: 'c',
ctrlKey: true,
shiftKey: false,
altKey: true,
format: '[{linkText}]({pageUrl})',
name: 'Markdown'
},
ASCIIDOC: {
hotkey: 'c',
ctrlKey: false,
shiftKey: true,
altKey: true,
format: '{pageUrl}[{linkText}]',
name: 'AsciiDoc'
}
};
const createAndCopyLink = (format, ty) => {
const pageUrl = window.location.href;
const pageTitle = document.title;
const selectedText = window.getSelection().toString().trim();
const linkText = selectedText || pageTitle;
GM_setClipboard(format.replace('{linkText}', linkText).replace('{pageUrl}', pageUrl));
};
const createAndCopyAsciiDocLink = () => {
createAndCopyLink('{pageUrl}[{linkText}]');
showNotification('Copied AsciiDoc Link!');
};
const createAndCopyMarkdownLink = () => {
createAndCopyLink('[{linkText}]({pageUrl})');
showNotification('Copied Markdown Link!');
};
const showNotification = (message) => {
const notification = document.createElement('div');
notification.textContent = message;
notification.setAttribute('style', `
position: fixed !important;
top: 20px !important;
right: 20px !important;
padding: 12px 20px !important;
background-color: rgba(0, 0, 0, 0.75) !important;
color: white !important;
border-radius: 8px !important;
z-index: 999999 !important;
font-size: 16px !important;
font-family: sans-serif !important;
box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important;
opacity: 1;
transition: opacity 0.5s ease-out !important;
`);
document.body.appendChild(notification);
setTimeout(() => {
notification.style.opacity = '0';
setTimeout(() => { document.body.removeChild(notification); }, 500);
}, 2000);
};
const getHotkey = (configs, event) => Object.values(configs).find(c =>
c.hotkey === event.key.toLowerCase() &&
c.ctrlKey === event.ctrlKey &&
c.shiftKey === event.shiftKey &&
c.altKey === event.altKey
)
document.addEventListener('keydown', (event) => {
const hotkey = getHotkey(CONFIG, event);
if (hotkey) {
event.preventDefault();
event.stopPropagation();
createAndCopyLink(hotkey.format);
Object.keys
showNotification(`Copied ${hotkey.name} Link!`);
return;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment