Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created November 22, 2017 14:13
Show Gist options
  • Save WebReflection/c14040f2b8c7f7d23ea9a770017269cd to your computer and use it in GitHub Desktop.
Save WebReflection/c14040f2b8c7f7d23ea9a770017269cd to your computer and use it in GitHub Desktop.
Try to execute a Gist pre content and it copies it to clipboard.
document.documentElement.appendChild(
document.createElement('style')
).textContent = `
.executable {
transition: all .3s;
}
.executable:hover {
cursor: pointer;
background: #fff !important;
}`;
document.querySelectorAll('pre').forEach(pre => {
try {
const text = pre.textContent + '\n';
const fn = Function(text);
pre.onclick = () => {
try { fn.call(window); } catch(meh) {}
const ta = document.createElement('textarea');
pre.parentNode.insertBefore(ta, pre);
ta.value = text;
ta.focus();
ta.setSelectionRange(0, text.length);
document.execCommand('copy');
pre.parentNode.removeChild(ta);
};
pre.classList.add('executable');
} finally {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment