var hiliteColor = 'ffff99'; // light yellow var hiliteTimeout, hiliteTimeout2; var fadeTime = '.5s'; var hiliteTime = 3000; // 3 seconds, amount of time to keep target highlighted var cookieCheckInterval = 500; // 0.5 seconds. /// // Check for highlight cookie and set up recurring checking /// function check_for_hilite(){ doHilite(); /// // We need to check for cookie fairly often because if the page is redisplayed, it may not reload, so // we can't rely on using the onload function. /// setInterval(function(){doHilite();}, cookieCheckInterval); } /// // Get cookie to let us know what to highlight /// function doHilite(){ var isHilite = getCookie('hilite-link-hilite'); if (typeof isHilite !== 'undefined' && isHilite !== ''){ hl(isHilite, 'd'); deleteCookie('hilite-link-hilite'); } } /// // Highlight a link target for a short time. // Function name is short because it is used many times in the eBook. // myType = (d) Direct, target is in current html file. (c) Use cookie, target is in another file /// function hl(myTarget, myType){ if (myType == 'd') hilite_function(myTarget); else setCookie('hilite-link-hilite', myTarget, 1); } // return the containing
element for thisElem function containingParagraph(thisElem) { var parent = thisElem.parentNode; if (parent.nodeName!='p') { parent = containingParagraph(parent); } return parent; } /// // Highlight spans or divs based on class name. // myTarget = class name you want highlighted. /// function hilite_function(myTarget){ var myStyle = '-webkit-transition:background-color ' + fadeTime + ' ease-in; background:#' + hiliteColor; var myObj = document.getElementById(myTarget); var myEndObj = document.getElementById(myTarget + '_end'); var myFoundEndObj = 0; var mySpanList = new Array(); // list of span elements that were added, these will need to be set to highlight. /// // If there is no end element, just highlight the paragraph /// if (typeof(myEndObj) === 'undefined' || myEndObj === null){ mySpanList[0] = containingParagraph(myObj); mySpanList[0].setAttribute('style', myStyle); } else { // Highlight from start element to end element myFoundEndObj = 1; var myRange = document.createRange(); myRange.setStart(myObj, 0); myRange.setEnd(myEndObj, 0); var pastStartNode = false, reachedEndNode = false; function getTextNodesInRange(node){ if (node == myObj) { pastStartNode = true; } else if (node == myEndObj) { reachedEndNode = true; } else if (node.nodeType == 3) { if (pastStartNode && !reachedEndNode && !/^\s*$/.test(node.nodeValue)) { node.innerHTML = '' + node.textContent + ''; var span = document.createElement("span"); span.className = "target_highlight"; span.textContent = node.textContent; node.parentNode.replaceChild(span, node); } } else { for (var i = 0, len = node.childNodes.length; !reachedEndNode && i < len; ++i){ getTextNodesInRange(node.childNodes[i]); } } } getTextNodesInRange(myRange.commonAncestorContainer); var mySpanList = document.getElementsByClassName('target_highlight'); for (var i = 0; i < mySpanList.length; i++) { mySpanList[i].setAttribute('style', myStyle); } } //remove hilite hiliteTimeout = setTimeout(function(){ for (var i = 0; i < mySpanList.length; i++){ mySpanList[i].setAttribute('style', '-webkit-transition:background-color ' + fadeTime + ' ease-out; background:none;'); } hiliteTimeout = null; hiliteTimeout2 = null; }, hiliteTime); //remove spans 1 second later if (myFoundEndObj){ hiliteTimeout2 = setTimeout(function(){ for (var i = 0; i < myList.length; i++){ var myText = createTextNode(mySpanList[i].textContent); mySpanList[i].parentNode.replaceChild(myText, mySpanList[i]); } }, hiliteTime + (parseFloat(fadeTime) * 1000)); } } /// // Set highlight cookie /// function setCookie(c_name, value, expdays){ var expdate = new Date(); expdate.setDate(expdate.getDate() + expdays); var c_value = escape(value) + ((expdays === null) ? "" : "; expires="+expdate.toUTCString()); document.cookie=c_name + "=" + c_value; } /// // Retrieve highlight cookie /// function getCookie(c_name){ var i,x,y,ARRcookies=document.cookie.split(";"); for (i = 0; i