Commit 4a4f46d1 authored by Vincent Tam's avatar Vincent Tam

Math.SE: fixed unstable selection range

parent faa152a4
Pipeline #58664639 passed with stages
in 40 seconds
This diff is collapsed.
// https://davidwalsh.name/event-delegate
document.getElementsByTagName("article")[0].addEventListener("click", function(e) {
if (e.target && e.target.nodeName == 'BUTTON') { // only act on buttons
if (e.target && e.target.nodeName == 'BUTTON') {
doCopy(e.target.previousElementSibling.firstElementChild);
}
});
// https://stackoverflow.com/a/33713926/3184351
function doCopy(target) {
var range = document.createRange();
range.selectNodeContents(target);
s = window.getSelection();
s.addRange(range);
document.execCommand('copy');
// Reset selection
// https://stackoverflow.com/a/3169849/3184351
if (window.getSelection) {
if (window.getSelection().empty) { // Chrome
s.empty();
} else if (window.getSelection().removeAllRanges) { // Firefox
s.removeAllRanges();
}
} else if (document.selection) { // IE?
document.selection.empty();
}
target.select();
document.execCommand("copy");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment