Jump to content

User:Pmartin/cache.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function addcache() {
    
    if (typeof no_external_cache !== "undefined" && no_external_cache) {
        return;
    }
 
   
    var links  = document.getElementsByClassName('external');


    for (var i = 0, l = links.length; i < l; i++) {
        var current_link = links[i];
        var path = current_link.href;
        if (path.indexOf("http://wikiwix.com/cache/") > -1 || path.indexOf("http://web.archive.org/web/") > -1 || path.indexOf("wikipedia.org") > -1 || path.indexOf("wikimedia.org") > -1 || path.indexOf("stable.toolserver.org") > -1) {
            continue;
        }
        var element_parent = current_link.parentNode;
        if (hasClass(element_parent, "noarchive")) {
            continue;
        }
        var page_title = getTextContent(current_link);
        var last = document.createElement("small");
        last.className = "cachelinks";
        last.appendChild(document.createTextNode("\u00a0["));
 
        var link = document.createElement("a");
        link.setAttribute("href", "http://wikiwix.com/cache/?url=" + path.replace(/%/g, "%25").replace(/&/g, "%26") + "&title=" + encodeURIComponent(page_title));
        link.setAttribute("title", "archive of " + page_title);
        link.appendChild(document.createTextNode("archive"));
 
        last.appendChild(link);
        last.appendChild(document.createTextNode("]"));
 
        element_parent.insertBefore(last, current_link.nextSibling);
    }
}

// If we're on an article page, run addcache
if (wgNamespaceNumber === 0) {
    $( addcache );
}

// Retrieves the actual text content of a node and its descendant nodes
// Copyright Harmen Christophe, http://openweb.eu.org/articles/validation_avancee, CC
function getTextContent(oNode) {
  if(!oNode) return null;
  if (typeof(oNode.textContent)!="undefined") {return oNode.textContent;}
  switch (oNode.nodeType) {
    case 3: // TEXT_NODE
    case 4: // CDATA_SECTION_NODE
      return oNode.nodeValue;
      break;
    case 7: // PROCESSING_INSTRUCTION_NODE
    case 8: // COMMENT_NODE
      if (getTextContent.caller!=getTextContent) {
        return oNode.nodeValue;
      }
      break;
    case 9: // DOCUMENT_NODE
    case 10: // DOCUMENT_TYPE_NODE
    case 12: // NOTATION_NODE
      return null;
      break;
  }
  var _textContent = "";
  oNode = oNode.firstChild;
  while (oNode) {
    _textContent += getTextContent(oNode);
    oNode = oNode.nextSibling;
  }
  return _textContent;
}