-
+ 01E83A62726BEF4232415B4841942427F31063AC2DDFBAB0048D5F5B63DA00EC2ECCACF3FC30CC71997E304969960F5774037E1245E1F0C4E9853D7D3B6450E7
mp-wp/wp-content/themes/default/selection-magic.php
(0 . 0)(1 . 135)
398 <script type="text/javascript">
399 // Script to allow anchoring of user-selected content on html pages.
400 // Original idea deployed by http://archive.today
401 // Packaged for WordPress on http://trilema.com/2015/that-spiffy-selection-thing/
402
403 function findPos(obj) {
404 var curtop = 0;
405 while (obj && obj.offsetParent) {
406 curtop += obj.offsetTop; // todo: + webkit-transform
407 obj = obj.offsetParent;
408 }
409 return curtop;
410 }
411 var artificial = null;
412 var prevhash = "";
413 function scrollToHash() {
414 if (document.location.hash.replace(/^#/, "")==prevhash.replace(/^#/, ""))
415 return;
416 prevhash = document.location.hash;
417 if (document.location.hash.match(/#[0-9.]+%/)) {
418 var p = parseFloat(document.location.hash.substring(1));
419 if (0 < p && p < 100 /*&& p%5 != 0*/) {
420 var content = document.getElementById("shash-<?php the_ID(); ?>")
421 var y = findPos(content) + (content.offsetHeight)*p/100;
422 window.scrollTo(0, y-16);
423 }
424 }
425
426 var adr = document.location.hash.match(/selection-(\d+).(\d+)-(\d+).(\d+)/);
427 if (adr) {
428 var pos=0,begin=null,end=null;
429 function recur(e) {
430 if (e.nodeType==1) pos = (pos&~1)+2;
431 if (e.nodeType==3) pos = pos|1;
432 if (pos==adr[1]) begin=[e, adr[2]];
433 if (pos==adr[3]) end =[e, adr[4]];
434 for (var i=0; i<e.childNodes.length; i++)
435 recur(e.childNodes[i]);
436 if (e.childNodes.length>0 && e.lastChild.nodeType==3)
437 pos = (pos&~1)+2;
438 }
439 // remove old "artificial" span if any
440 if (artificial) {
441 artificial.previousSibling.data += artificial.childNodes[0].data;
442 artificial.parentNode.removeChild(artificial);
443 }
444 var content = document.getElementById("shash-<?php the_ID(); ?>");
445 recur(content.childNodes[content.childNodes[0].nodeType==3 ? 1 : 0]);
446 if (begin!=null && end!=null) {
447 // scroll to selection
448 if (begin[0].nodeType==3) {
449 var text = document.createTextNode(begin[0].data.substr(0, begin[1]));
450 artificial = document.createElement("SPAN");
451 artificial.appendChild(document.createTextNode(begin[0].data.substr(begin[1])));
452
453 begin[0].parentNode.insertBefore(text, begin[0]);
454 begin[0].parentNode.replaceChild(artificial, begin[0]);
455
456 if (end[0]===begin[0])
457 end = [artificial.childNodes[0], end[1]-begin[1]];
458 begin = [artificial.childNodes[0], 0];
459 /* window.scrollTo(0, findPos(artificial)-8); */ artificial.scrollIntoView(true);
460 } else if (begin[0].nodeType==1) {
461 /* window.scrollTo(0, findPos(begin[0])-8); */ begin[0].scrollIntoView(true);
462 }
463
464 if (window.getSelection) {
465 var sel = window.getSelection();
466 sel.removeAllRanges();
467 var range = document.createRange();
468 range.setStart(begin[0], begin[1]);
469 range.setEnd ( end[0], end[1]);
470 sel.addRange(range);
471 } else if (document.selection) { // IE
472 }
473 }
474 }
475 }
476 window.onhashchange = scrollToHash;
477 var initScrollToHashDone = false;
478 function initScrollToHash() {
479 if (!initScrollToHashDone) {
480 initScrollToHashDone = true;
481 scrollToHash();
482 }
483 }
484 window.onload = initScrollToHash;
485 setTimeout(initScrollToHash, 500); /* onload can be delayed by counter code */
486
487 //document.onselectionchange = /* only webkit has working document.onselectionchange */
488 document.onmousedown = document.onmouseup = function(e) {
489 var newhash = "";
490 if (window.getSelection) {
491 var sel=window.getSelection();
492 if (!sel.isCollapsed) {
493 var pos=0,begin=[0,0],end=[0,0];
494 var range=sel.getRangeAt(0);
495 function recur(e) {
496 if (e===artificial) {
497 if (range.startContainer===e.childNodes[0]) begin=[pos, e.previousSibling.data.length+range.startOffset];
498 if (range.endContainer ===e.childNodes[0]) end =[pos, e.previousSibling.data.length+range.endOffset ];
499 } else {
500 if (e.nodeType==1) pos = (pos&~1)+2;
501 if (e.nodeType==3) pos = pos|1;
502 if (range.startContainer===e) begin=[pos, range.startOffset];
503 if (range.endContainer ===e) end =[pos, range.endOffset ];
504 for (var i=0; i<e.childNodes.length; i++)
505 recur(e.childNodes[i]);
506 if (e.childNodes.length>0 && e.lastChild.nodeType==3)
507 pos = (pos&~1)+2;
508 }
509 }
510
511 var content = document.getElementById("shash-<?php the_ID(); ?>");
512 recur(content.childNodes[content.childNodes[0].nodeType==3 ? 1 : 0]);
513 if (begin[0]>0 && end[0]>0) {
514 newhash = "selection-"+begin[0]+"."+begin[1]+"-"+end[0]+"."+end[1];
515 }
516 }
517 } else if (document.selection) { // IE
518 }
519
520 try {
521 var oldhash = location.hash.replace(/^#/, "");
522 if (oldhash != newhash) {
523 prevhash = newhash; /* avoid firing window.onhashchange and scrolling */
524 if (history.replaceState)
525 history.replaceState('', document.title, newhash=="" ? window.location.pathname : '#'+newhash);
526 else
527 location.hash = newhash;
528 }
529 } catch(e) {
530 }
531 };
532 </script>