Created
October 10, 2015 05:38
-
-
Save AutoSponge/4d7323a156d9ad8b8341 to your computer and use it in GitHub Desktop.
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
(function () { | |
var textarea; | |
function createTextArea() { | |
if (textarea) { | |
return textarea; | |
} | |
textarea = document.createElement('textarea'); | |
textarea.style.position = 'absolute'; | |
textarea.style.left = '-9999px'; | |
textarea.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px'; | |
textarea.setAttribute('readonly', ''); | |
document.body.appendChild(textarea); | |
} | |
function getParent(a, b) { | |
var parent = a; | |
while (parent !== document.body && !parent.contains(b)) { | |
parent = parent.parentNode; | |
} | |
return parent; | |
} | |
document.body.addEventListener('copy', function () { | |
createTextArea(); | |
var selection = window.getSelection(); | |
var parent = getParent(selection.focusNode, selection.anchorNode); | |
var range = document.createRange(); | |
range.selectNodeContents(parent); | |
selection.addRange(range); | |
textarea.value = selection; | |
var win = window.open('', 'Tonicify-' + Date.now(), ''); | |
var doc = win.document; | |
var div = doc.createElement('div'); | |
div.id = 'tonic'; | |
div.innerHTML = 'var _ = require(\'lodash\');\n\n' + textarea.value; | |
var tonic = doc.createElement('script'); | |
tonic.src = 'https://embed.tonicdev.com/'; | |
tonic.setAttribute('data-element-id', 'tonic'); | |
doc.body.appendChild(div); | |
doc.body.appendChild(tonic); | |
win.document.close(); | |
win.focus(); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment