Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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
You can’t perform that action at this time.