raw
mp-wp_add-footnot...    1 <script type="text/javascript">
mp-wp_add-footnot... 2 // Script to allow anchoring of user-selected content on html pages.
mp-wp_add-footnot... 3 // Original idea deployed by http://archive.today
mp-wp_add-footnot... 4 // Packaged for WordPress on http://trilema.com/2015/that-spiffy-selection-thing/
mp-wp_add-footnot... 5
mp-wp_add-footnot... 6 function findPos(obj) {
mp-wp_add-footnot... 7 var curtop = 0;
mp-wp_add-footnot... 8 while (obj && obj.offsetParent) {
mp-wp_add-footnot... 9 curtop += obj.offsetTop; // todo: + webkit-transform
mp-wp_add-footnot... 10 obj = obj.offsetParent;
mp-wp_add-footnot... 11 }
mp-wp_add-footnot... 12 return curtop;
mp-wp_add-footnot... 13 }
mp-wp_add-footnot... 14 var artificial = null;
mp-wp_add-footnot... 15 var prevhash = "";
mp-wp_add-footnot... 16 function scrollToHash() {
mp-wp_add-footnot... 17 if (document.location.hash.replace(/^#/, "")==prevhash.replace(/^#/, ""))
mp-wp_add-footnot... 18 return;
mp-wp_add-footnot... 19 prevhash = document.location.hash;
mp-wp_add-footnot... 20 if (document.location.hash.match(/#[0-9.]+%/)) {
mp-wp_add-footnot... 21 var p = parseFloat(document.location.hash.substring(1));
mp-wp_add-footnot... 22 if (0 < p && p < 100 /*&& p%5 != 0*/) {
mp-wp_add-footnot... 23 var content = document.getElementById("shash-<?php the_ID(); ?>")
mp-wp_add-footnot... 24 var y = findPos(content) + (content.offsetHeight)*p/100;
mp-wp_add-footnot... 25 window.scrollTo(0, y-16);
mp-wp_add-footnot... 26 }
mp-wp_add-footnot... 27 }
mp-wp_add-footnot... 28
mp-wp_add-footnot... 29 var adr = document.location.hash.match(/selection-(\d+).(\d+)-(\d+).(\d+)/);
mp-wp_add-footnot... 30 if (adr) {
mp-wp_add-footnot... 31 var pos=0,begin=null,end=null;
mp-wp_add-footnot... 32 function recur(e) {
mp-wp_add-footnot... 33 if (e.nodeType==1) pos = (pos&~1)+2;
mp-wp_add-footnot... 34 if (e.nodeType==3) pos = pos|1;
mp-wp_add-footnot... 35 if (pos==adr[1]) begin=[e, adr[2]];
mp-wp_add-footnot... 36 if (pos==adr[3]) end =[e, adr[4]];
mp-wp_add-footnot... 37 for (var i=0; i<e.childNodes.length; i++)
mp-wp_add-footnot... 38 recur(e.childNodes[i]);
mp-wp_add-footnot... 39 if (e.childNodes.length>0 && e.lastChild.nodeType==3)
mp-wp_add-footnot... 40 pos = (pos&~1)+2;
mp-wp_add-footnot... 41 }
mp-wp_add-footnot... 42 // remove old "artificial" span if any
mp-wp_add-footnot... 43 if (artificial) {
mp-wp_add-footnot... 44 artificial.previousSibling.data += artificial.childNodes[0].data;
mp-wp_add-footnot... 45 artificial.parentNode.removeChild(artificial);
mp-wp_add-footnot... 46 }
mp-wp_add-footnot... 47 var content = document.getElementById("shash-<?php the_ID(); ?>");
mp-wp_add-footnot... 48 recur(content.childNodes[content.childNodes[0].nodeType==3 ? 1 : 0]);
mp-wp_add-footnot... 49 if (begin!=null && end!=null) {
mp-wp_add-footnot... 50 // scroll to selection
mp-wp_add-footnot... 51 if (begin[0].nodeType==3) {
mp-wp_add-footnot... 52 var text = document.createTextNode(begin[0].data.substr(0, begin[1]));
mp-wp_add-footnot... 53 artificial = document.createElement("SPAN");
mp-wp_add-footnot... 54 artificial.appendChild(document.createTextNode(begin[0].data.substr(begin[1])));
mp-wp_add-footnot... 55
mp-wp_add-footnot... 56 begin[0].parentNode.insertBefore(text, begin[0]);
mp-wp_add-footnot... 57 begin[0].parentNode.replaceChild(artificial, begin[0]);
mp-wp_add-footnot... 58
mp-wp_add-footnot... 59 if (end[0]===begin[0])
mp-wp_add-footnot... 60 end = [artificial.childNodes[0], end[1]-begin[1]];
mp-wp_add-footnot... 61 begin = [artificial.childNodes[0], 0];
mp-wp_add-footnot... 62 /* window.scrollTo(0, findPos(artificial)-8); */ artificial.scrollIntoView(true);
mp-wp_add-footnot... 63 } else if (begin[0].nodeType==1) {
mp-wp_add-footnot... 64 /* window.scrollTo(0, findPos(begin[0])-8); */ begin[0].scrollIntoView(true);
mp-wp_add-footnot... 65 }
mp-wp_add-footnot... 66
mp-wp_add-footnot... 67 if (window.getSelection) {
mp-wp_add-footnot... 68 var sel = window.getSelection();
mp-wp_add-footnot... 69 sel.removeAllRanges();
mp-wp_add-footnot... 70 var range = document.createRange();
mp-wp_add-footnot... 71 range.setStart(begin[0], begin[1]);
mp-wp_add-footnot... 72 range.setEnd ( end[0], end[1]);
mp-wp_add-footnot... 73 sel.addRange(range);
mp-wp_add-footnot... 74 } else if (document.selection) { // IE
mp-wp_add-footnot... 75 }
mp-wp_add-footnot... 76 }
mp-wp_add-footnot... 77 }
mp-wp_add-footnot... 78 }
mp-wp_add-footnot... 79 window.onhashchange = scrollToHash;
mp-wp_add-footnot... 80 var initScrollToHashDone = false;
mp-wp_add-footnot... 81 function initScrollToHash() {
mp-wp_add-footnot... 82 if (!initScrollToHashDone) {
mp-wp_add-footnot... 83 initScrollToHashDone = true;
mp-wp_add-footnot... 84 scrollToHash();
mp-wp_add-footnot... 85 }
mp-wp_add-footnot... 86 }
mp-wp_add-footnot... 87 window.onload = initScrollToHash;
mp-wp_add-footnot... 88 setTimeout(initScrollToHash, 500); /* onload can be delayed by counter code */
mp-wp_add-footnot... 89
mp-wp_add-footnot... 90 //document.onselectionchange = /* only webkit has working document.onselectionchange */
mp-wp_add-footnot... 91 document.onmousedown = document.onmouseup = function(e) {
mp-wp_add-footnot... 92 var newhash = "";
mp-wp_add-footnot... 93 if (window.getSelection) {
mp-wp_add-footnot... 94 var sel=window.getSelection();
mp-wp_add-footnot... 95 if (!sel.isCollapsed) {
mp-wp_add-footnot... 96 var pos=0,begin=[0,0],end=[0,0];
mp-wp_add-footnot... 97 var range=sel.getRangeAt(0);
mp-wp_add-footnot... 98 function recur(e) {
mp-wp_add-footnot... 99 if (e===artificial) {
mp-wp_add-footnot... 100 if (range.startContainer===e.childNodes[0]) begin=[pos, e.previousSibling.data.length+range.startOffset];
mp-wp_add-footnot... 101 if (range.endContainer ===e.childNodes[0]) end =[pos, e.previousSibling.data.length+range.endOffset ];
mp-wp_add-footnot... 102 } else {
mp-wp_add-footnot... 103 if (e.nodeType==1) pos = (pos&~1)+2;
mp-wp_add-footnot... 104 if (e.nodeType==3) pos = pos|1;
mp-wp_add-footnot... 105 if (range.startContainer===e) begin=[pos, range.startOffset];
mp-wp_add-footnot... 106 if (range.endContainer ===e) end =[pos, range.endOffset ];
mp-wp_add-footnot... 107 for (var i=0; i<e.childNodes.length; i++)
mp-wp_add-footnot... 108 recur(e.childNodes[i]);
mp-wp_add-footnot... 109 if (e.childNodes.length>0 && e.lastChild.nodeType==3)
mp-wp_add-footnot... 110 pos = (pos&~1)+2;
mp-wp_add-footnot... 111 }
mp-wp_add-footnot... 112 }
mp-wp_add-footnot... 113
mp-wp_add-footnot... 114 var content = document.getElementById("shash-<?php the_ID(); ?>");
mp-wp_add-footnot... 115 recur(content.childNodes[content.childNodes[0].nodeType==3 ? 1 : 0]);
mp-wp_add-footnot... 116 if (begin[0]>0 && end[0]>0) {
mp-wp_add-footnot... 117 newhash = "selection-"+begin[0]+"."+begin[1]+"-"+end[0]+"."+end[1];
mp-wp_add-footnot... 118 }
mp-wp_add-footnot... 119 }
mp-wp_add-footnot... 120 } else if (document.selection) { // IE
mp-wp_add-footnot... 121 }
mp-wp_add-footnot... 122
mp-wp_add-footnot... 123 try {
mp-wp_add-footnot... 124 var oldhash = location.hash.replace(/^#/, "");
mp-wp_add-footnot... 125 if (oldhash != newhash) {
mp-wp_add-footnot... 126 prevhash = newhash; /* avoid firing window.onhashchange and scrolling */
mp-wp_add-footnot... 127 if (history.replaceState)
mp-wp_add-footnot... 128 history.replaceState('', document.title, newhash=="" ? window.location.pathname : '#'+newhash);
mp-wp_add-footnot... 129 else
mp-wp_add-footnot... 130 location.hash = newhash;
mp-wp_add-footnot... 131 }
mp-wp_add-footnot... 132 } catch(e) {
mp-wp_add-footnot... 133 }
mp-wp_add-footnot... 134 };
mp-wp_add-footnot... 135 </script>