Last active
October 6, 2022 14:31
-
-
Save diogotito/8523ae0c9cee3db96ada0a6f2378d9d9 to your computer and use it in GitHub Desktop.
Userscript to link to open tab tab page in Markdown or Zimwiki markup syntax, through a menu command or the 'ctrl-alt-c m' and 'ctrl-alt-c z' keyboard shortcuts
This file contains 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 link to this page as Markdown | |
// @icon https://upload.wikimedia.org/wikipedia/commons/4/48/Markdown-mark.svg | |
// @homepageURL https://gist.github.com/diogotito/8523ae0c9cee3db96ada0a6f2378d9d9 | |
// @downloadURL https://gist.github.com/diogotito/8523ae0c9cee3db96ada0a6f2378d9d9/raw/markdown_link.user.js | |
// @namespace https://gist.github.com/diogotito | |
// @match *://*/* | |
// @noframes | |
// @grant GM_notification | |
// @grant GM_setClipboard | |
// @grant GM_registerMenuCommand | |
// @grant GM_getResourceURL | |
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1 | |
// @version 1.0 | |
// @author diogotito | |
// @description 10/5/2022, 10:49:21 AM | |
// @resource markdown_logo https://upload.wikimedia.org/wikipedia/commons/4/48/Markdown-mark.svg | |
// @resource html_logo https://upload.wikimedia.org/wikipedia/commons/6/61/HTML5_logo_and_wordmark.svg | |
// @resource latex_logo https://upload.wikimedia.org/wikipedia/commons/9/92/LaTeX_logo.svg | |
// @resource zimwiki_logo https://zim-wiki.org/images/globe.png | |
// ==/UserScript== | |
const genShortcuts = (key) => | |
['', 'ctrl-', 'alt-', 'ctrl-alt-'] | |
.map(mods => `ctrl-alt-c ${mods}${key}`) | |
//== Linkers ============================================================= | |
registerLinker({ | |
name: "Markdown", | |
formatter: (title, url) => `[${url}](${title})`, | |
imageURL: GM_getResourceURL('markdown_logo'), | |
shortcuts: genShortcuts('m'), | |
}) | |
registerLinker({ | |
name: "HTML", | |
formatter: (title, url) => `<a href="${url}">${title}</a>`, | |
imageURL: GM_getResourceURL('html_logo'), | |
shortcuts: genShortcuts('h'), | |
}) | |
registerLinker({ | |
name: "LaTeX (hyperref)", | |
formatter: (title, url) => String.raw`\href{${url}}{${title}}`, | |
imageURL: GM_getResourceURL('latex_logo'), | |
shortcuts: genShortcuts('l'), | |
}) | |
registerLinker({ | |
name: "Zimwiki", | |
formatter: (title, url) => `[[${url}|${title}]]`, | |
imageURL: GM_getResourceURL('zimwiki_logo'), | |
shortcuts: genShortcuts('z'), | |
}) | |
//======================================================================== | |
function registerLinker({name, imageURL, shortcuts=[], formatter}) { | |
GM_registerMenuCommand(`Link as ${name} (${shortcuts[0]})`, doLink) | |
for (const shortcut of shortcuts) { | |
VM.shortcut.register(shortcut, doLink) | |
} | |
function doLink(_event) { | |
const cb = formatter(document.title, window.location) | |
GM_setClipboard(cb) | |
GM_notification({ | |
title: `Copied ${name} link`, | |
text: cb, | |
image: imageURL, | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment