Jump to content

User:Wikifresc/common.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wikifresc (talk | contribs) at 23:55, 24 February 2019 (Created page with '// remove brackets from numbered references function nativeSelector() { var elements = document.querySelectorAll("sup, sup a, sup i, sup em"); var resul...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
// remove brackets from numbered references

function nativeSelector() {
    var elements = document.querySelectorAll("sup, sup a, sup i, sup em");
    var results = [];
    var child;
    for(var i = 0; i < elements.length; i++) {
        child = elements[i].childNodes[0];
        if(elements[i].hasChildNodes() && child.nodeType == 3) {
            results.push(child);
        }
    }
    return results;
}

var textnodes = nativeSelector(), _nv;

for (var i = 0, len = textnodes.length; i<len; i++){
    _nv = textnodes[i].nodeValue;
    textnodes[i].nodeValue = _nv.replace(/\[([0-9]+)\]/gi,'$1');
}


// typekit

  (function(d) {
    var config = {
      kitId: 'nea5oiw',
      scriptTimeout: 3000,
      async: true
    },
    h=d.documentElement,t=setTimeout(function(){h.className=h.className.replace(/\bwf-loading\b/g,"")+" wf-inactive";},config.scriptTimeout),tk=d.createElement("script"),f=false,s=d.getElementsByTagName("script")[0],a;h.className+=" wf-loading";tk.src='https://use.typekit.net/'+config.kitId+'.js';tk.async=true;tk.onload=tk.onreadystatechange=function(){a=this.readyState;if(f||a&&a!="complete"&&a!="loaded")return;f=true;clearTimeout(t);try{Typekit.load(config)}catch(e){}};s.parentNode.insertBefore(tk,s)
  })(document);



// replace acronyms

var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = "html body abbr { font-variant-caps: all-small-caps; font-variant-numeric: normal; }";
document.head.appendChild(css);

var matchText = function(node, regex, callback, excludeElements) { 

    excludeElements || (excludeElements = ['script', 'style', 'iframe', 'canvas', 'code', 'pre', 'input', 'textarea']);
    var child = node.firstChild;

    while (child) {
        switch (child.nodeType) {
        case 1:
            if (excludeElements.indexOf(child.tagName.toLowerCase()) > -1)
                break;
            matchText(child, regex, callback, excludeElements);
            break;
        case 3:
            var bk = 0;
            child.data.replace(regex, function(all) {
                var args = [].slice.call(arguments),
                    offset = args[args.length - 2],
                    newTextNode = child.splitText(offset+bk), tag;
                bk -= child.data.length + all.length;

                newTextNode.data = newTextNode.data.substr(all.length);
                tag = callback.apply(window, [child].concat(args));
                child.parentNode.insertBefore(tag, newTextNode);
                child = newTextNode;
            });
            regex.lastIndex = 0;
            break;
        }

        child = child.nextSibling;
    }

    return node;
};

utfnum = "(U\\+[0-9A-Fa-f]{4,})";

outer = "0-9-";

alphabet = "A-ZЀ-ЯΑ-ΪÁÉÍÓÚÝÀÈÌÒÙỲÂÊÎÔÛŶÄËÏÖÜŸḦẄẌĀĒĪŌŪȲĂĔĬŎŬĞÃẼĨÕŨỸÑṼȦḂĊḊĖḞĠḢİṀṄȮṖṘṠṪẆẊẎŻẠḄḌẸḤỊḲḶṂṆỌṚṢṬỤṾẈỴẒŐŰÆŒØÅŮČĎĚĽŇŘŠŤŽÇĘĢĶĻŅŖŞŢÐĐÞẞΆΈΉΊΌΎΏ";

joiners = "&:_"; // '’

longnum = "(" +
            "[" + alphabet + "]*" +
            "[0-9:—–-]{9,}" +
            "[" + alphabet + "]*" +
          ")";

matchText(document.getElementsByTagName("*")[0], new RegExp(
  utfnum + "|" + longnum + "|" +
  "(" +
    "[" + alphabet + outer + "]*" +
    "[" + joiners + "]*" +
    "[" + alphabet + "]{2,}" +
    "[" + alphabet + outer + "]*" +
  ")",
  "gu"), function(node, match, offset) {
    var span = document.createElement("abbr");
    span.textContent = match;
    return span;
});