Leitgedanken/copy.js
2022-11-21 09:47:28 +01:00

107 lines
3.6 KiB
JavaScript

//funzione per aggiungere il riferimento all'articolo originale quando viene effettuata una copia
function copy(lingua){
var urlLocation = document.location.href;
//recupero il testo in lingua da inserire
var j=getTestoCopyByLingua(lingua,urlLocation);
m = document.getElementsByTagName("body")[0];
var f = document.createElement("div");
f.style.overflow = "hidden";
f.style.color = "#000000";
f.style.backgroundColor = "transparent";
f.style.textAlign = "left";
f.style.textDecoration = "none";
f.style.border = "none";
if (typeof window.getSelection != "undefined") {
var n = window.getSelection();
if (n.toString()) if (typeof n.setBaseAndExtent != "undefined") {
var p = n.getRangeAt(0);
f.style.width = 0.1;
f.style.height = 0.1;
f.style.position = "absolute";
f.style.top = "-1000px";
f.style.left = "-1001px";
f.appendChild(p.cloneContents());
f.appendChild(document.createElement("br"));
f.appendChild(document.createElement("br"));
var A = document.createElement("span");
A.innerHTML = j;
f.appendChild(A);
f.appendChild(document.createElement("br"));
j = f.innerText.length;
m.appendChild(f);
if (f.innerText.length != j) f.style.overflow = "";
n.selectAllChildren(f);
window.setTimeout(function () {
f.parentNode.removeChild(f);
window.getSelection().setBaseAndExtent(p.startContainer, p.startOffset, p.endContainer, p.endOffset)
}, 0)
}
else {
h = document.createElement("div");
//h.style.display = "none";
m.appendChild(h);
f.innerHTML = "<br>" + j + "<br>";
h.appendChild(document.createElement("br"));
h.appendChild(f);
h.appendChild(document.createElement("br"));
m = document.createRange();
m.selectNode(f);
n.addRange(m);
window.setTimeout(function () {
h.parentNode.removeChild(h)
}, 0)
}
}
else {
var t = document.documentElement.scrollLeft || document.body.scrollLeft,
u = document.documentElement.scrollTop || document.body.scrollTop;
h = document.createElement("div");
h.style.visibility = "hidden";
h.style.overflow = "hidden";
h.style.position = "absolute";
h.style.left = t + 20 + "px";
h.style.top = u + 20 + "px";
h.style.width = "1px";
h.style.height = "1px";
m.appendChild(h);
var v = document.selection.createRange();
f.innerHTML = v.htmlText + "<br><br>" + j + "<br>";
h.appendChild(document.createElement("br"));
h.appendChild(f);
h.appendChild(document.createElement("br"));
n = m.createTextRange();
n.moveToElementText(f);
n.select();
window.setTimeout(function () {
h.parentNode.removeChild(h);
if (v.text != "") {
v.select();
document.documentElement.scrollLeft = t;
document.body.scrollLeft = t;
document.documentElement.scrollTop = u;
document.body.scrollTop = u
}
}, 0)
}
}
function getTestoCopyByLingua(lingua,urlLocation){
var j="";
if(lingua!=null){
switch (lingua)
{
case "it":
j=" <br/> Testo proveniente dalla pagina <a href='"+urlLocation+"'>"+urlLocation+"</a>";
break;
case "de":
j=" <br/> Dieser Text stammt von der Webseite <a href='http://www.lg-on.de'>http://www.lg-on.de</a>";
break;
}
}else{
j=" <br/> Text from page <a href='"+urlLocation+"'>"+urlLocation+"</a>";
}
return j;
}