-
-
Save orther/ce03a7dd9f05415f4fa60d883d6b62b9 to your computer and use it in GitHub Desktop.
all-in-one org-protocols user script for Chrome based browsers
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 org-protocols | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Send links or/and selected content into your Emacs via various protocols | |
// @match *://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var keys = {}; | |
var openedWindow; | |
// By pressing Ctrl + Alt + 'key': | |
// stores link via `org-protocol://store-link`, which can be inserted by `org-insert-link` | |
keys.link = 'K'; | |
// stores link + title via `org-protocol://store-link`, which can be inserted by `org-insert-link` | |
keys.linkTitle = 'G'; | |
// captures selected text as an org markup via `org-protocol:///capture-html`, needs github.com/alphapapa/org-protocol-capture-html | |
keys.html = 'H'; | |
// captures link + title via `org-protocol://capture` | |
keys.bookmarkTitle = 'B'; | |
// captures link + title + selected text as a plain text via `org-protocol://capture` | |
keys.bookmarkTitleSelected = 'S'; | |
// I need some form of sleep | |
function wait(ms){ | |
var start = new Date().getTime(); | |
var end = start; | |
while(end < start + ms) { | |
end = new Date().getTime(); | |
} | |
} | |
// deal with parens | |
function replace_all(str, find, replace) { | |
return str.replace(new RegExp(find, 'g'), replace); | |
} | |
function escapeIt(text) { | |
return replace_all(replace_all(replace_all(encodeURIComponent(text), "[(]", escape("(")), | |
"[)]", escape(")")), | |
"[']" ,escape("'")); | |
} | |
// note: it can be hard to create association for protocol in chrome when popup is there just one second | |
// for the first time and then after everytime you remove chrome's history, association needs to be confirmed | |
// consider to temporarly increase valu passed to wait() function | |
window.addEventListener('keyup', function() { | |
if (event.ctrlKey && event.altKey && !event.shiftKey) { | |
if (keys.link == String.fromCharCode(event.keyCode)) { | |
openedWindow = window.open('org-protocol://store-link?url='+ encodeURIComponent(location.href)); | |
wait(500); | |
openedWindow.close(); | |
} else if (keys.linkTitle == String.fromCharCode(event.keyCode)) { | |
openedWindow = window.open('org-protocol://store-link?url=' + encodeURIComponent(location.href) + '\\&title=' + encodeURIComponent(document.title)); | |
wait(500); | |
openedWindow.close(); | |
} else if (keys.html == String.fromCharCode(event.keyCode)) { | |
// you need capture template "w" in your emacs | |
openedWindow = window.open('org-protocol:///capture-html?template=w&url=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title || "[untitled page]") + '&body=' + encodeURIComponent(function() { | |
var html = ""; | |
if (typeof window.getSelection != "undefined") { | |
var sel = window.getSelection(); | |
if (sel.rangeCount) { | |
var container = document.createElement("div"); | |
for (var i = 0, len = sel.rangeCount; i < len; ++i) { | |
container.appendChild(sel.getRangeAt(i).cloneContents()); | |
} | |
html = container.innerHTML; | |
} | |
} else if (typeof document.selection != "undefined") { | |
if (document.selection.type == "Text") { | |
html = document.selection.createRange().htmlText; | |
} | |
} | |
var relToAbs = function(href) { | |
var a = document.createElement("a"); | |
a.href = href; | |
var abs = a.protocol + "//" + a.host + a.pathname + a.search + a.hash; | |
a.remove(); | |
return abs; | |
}; | |
var elementTypes = [ | |
['a', 'href'], | |
['img', 'src'] | |
]; | |
var div = document.createElement('div'); | |
div.innerHTML = html; | |
elementTypes.map(function(elementType) { | |
var elements = div.getElementsByTagName(elementType[0]); | |
for (var i = 0; i < elements.length; i++) { | |
elements[i].setAttribute(elementType[1], relToAbs(elements[i].getAttribute(elementType[1]))); | |
} | |
}); | |
return div.innerHTML; | |
}())); | |
wait(500); | |
openedWindow.close(); | |
} else if (keys.bookmarkTitle == String.fromCharCode(event.keyCode)) { | |
openedWindow = window.open('org-protocol://capture?template=L&url=' + encodeURIComponent(location.href) + '\\&title=' + encodeURIComponent(document.title)); | |
wait(500); | |
openedWindow.close(); | |
} else if (keys.bookmarkTitleSelected == String.fromCharCode(event.keyCode)) { | |
openedWindow = window.open('org-protocol://capture?template=p&url=' + encodeURIComponent(location.href) + '\\&title=' + encodeURIComponent(document.title) + '\\&body=' + escapeIt(window.getSelection().toString())); | |
wait(500); | |
openedWindow.close(); | |
} | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment