Last active
July 7, 2022 21:02
-
-
Save edgimar/44522bfdde53a3e128288fb00056360e to your computer and use it in GitHub Desktop.
User-script to copy the currently browsed web-page and selected content to the clipboard for pasting into org-mode
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
// Copyright 2018, M.A.E. | |
// | |
// Add the following as a user-script (via an extension like https://github.com/violentmonkey/violentmonkey) in order to copy the | |
// current webpage and selected text to the clipboard in a format suitable for pasting into an org-mode document. | |
// To execute the action, you need to press Alt-L on a webpage, though this can be modified by changing the keycode | |
// used in the onkeyup function. | |
// ==UserScript== | |
// @name Copy Org-mode Link | |
// @namespace Violentmonkey Scripts | |
// @match *://*/* | |
// @grant clipboardWrite | |
// ==/UserScript== | |
function main() { | |
function copyTextToClipboard(text) | |
{ | |
var textArea = document.createElement("textarea"); | |
textArea.style.position = 'fixed'; | |
textArea.style.top = 0; | |
textArea.style.left = 0; | |
textArea.style.width = '2em'; | |
textArea.style.height = '2em'; | |
textArea.style.padding = 0; | |
textArea.style.border = 'none'; | |
textArea.style.outline = 'none'; | |
textArea.style.boxShadow = 'none'; | |
textArea.style.background = 'transparent'; | |
textArea.value = text; | |
document.body.appendChild(textArea); | |
textArea.select(); | |
try | |
{ | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
console.log('Copying text command was ' + msg); | |
} | |
catch (err) | |
{ | |
console.log('Oops, unable to copy'); | |
} | |
document.body.removeChild(textArea); | |
}; | |
var url = encodeURIComponent(location.href); | |
url = url.replace(/%3A/g, ':').replace(/%2F/g, '/'); | |
var title = document.title; | |
title = title.replace(/\[/g, '{'); title = title.replace(/\]/g, '}'); | |
// see https://unicodelookup.com for unicode values | |
title = title.replace(/[\u2012]/g, "-"); // figure dash | |
title = title.replace(/[\u2013]/g, "-"); // en dash | |
title = title.replace(/[\u2014]/g, "--"); // em dash | |
title = title.replace(/[\u00B7]/g, "-"); // dot | |
title = title.replace(/[\u2019]/g, "'"); // apostrophe | |
var sel_text = window.getSelection(); | |
copyTextToClipboard('[['+url+']['+title+']]'+'\n\n'+sel_text); | |
} | |
// listen for Alt-C key-combination, and then execute | |
document.onkeyup=function(e){ | |
var e = e || window.event; // for IE to cover IEs window object | |
if(e.altKey && e.which == 67) { | |
main(); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment