Commit e7b2575c authored by Vincent Tam's avatar Vincent Tam

MathSE: fix copyBtn.js

parent c2c65b7a
Pipeline #57881640 passed with stages
in 41 seconds
$(document).ready(function() {
$('.copyBtn').click(function() {
copy($(this).prev().children())
});
// https://davidwalsh.name/event-delegate
document.getElementsByTagName("article")[0].addEventListener("click", function(e) {
if (e.target && e.target.nodeName == 'BUTTON') { // only act on buttons
doCopy(e.target.previousElementSibling.firstElementChild);
}
});
function copy(selector) {
var screenTop = $(document).scrollTop();
var $temp = $("<div>");
$("body").append($temp);
$temp.attr("contenteditable", true)
.html($(selector).html()).select()
.on("focus", function() { document.execCommand('selectAll',false,null) })
.focus();
document.execCommand("copy");
$temp.remove();
$('html, body').scrollTop(screenTop);
// 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();
}
}
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