Benutzer:Wurgl/normdaten-mark.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
var NormdatenMarkierung = {
  execute : function() {
    if(mw.config.get('wgNamespaceNumber') == 0 || mw.config.get('wgNamespaceNumber') == 14) {
      var action = mw.config.get('wgAction');
      if(action !== 'view')
        return;
      var id = mw.config.get('wgArticleId');
      if(id > 0)
        mw.loader.load('//persondata.toolforge.org/all_normdaten.php?id=' + id);
    }
  },
  gotData : function(json) {
    if(json === false)
      return;
    var links = document.getElementById('mw-content-text');
    if(links) {
      links = links.getElementsByTagName('a');
      var sheet = mw.util.addCSS('.subsup {position: absolute}\n.subsup sub {display:block; position:relative; left:1px; top: -.2em}\n.subsup sup {display:block; position:relative; left:1px; top: -.2em}');
      var head = document.getElementsByTagName('head')[0];
      head.insertBefore(sheet.ownerNode ? sheet.ownerNode : sheet, head.firstChild);
      for(var naviNoTableAll = 0; naviNoTableAll < 3; ++ naviNoTableAll) {
        for(var i = 0; i < links.length; ++ i) {
          if(!links[i].attributes.getNamedItem('href') || !NormdatenMarkierung.isIn(links[i], naviNoTableAll))
            continue;
          var href = links[i].attributes.getNamedItem('href').value;
          var hash = href.indexOf('#');
          if(hash != -1)
            href = href.substring(0, hash);
          if(!href.startsWith('/wiki/')) {
            /* if(!href.startsWith('/w/index.php?title=')) */
              continue;
            /* var article = href.substring(19);
            var article = decodeURIComponent(article.substring(0, article.indexOf('&action'))); */
          }
          else
            var article = decodeURIComponent(href.substring(6));
          if(json[article] == null)
            continue;
          var details = json[article];
          if(details === true) {
            // keine Normdaten
            links[i].insertAdjacentHTML('afterend', '<span style="background-color:#e0e0e0; -moz-user-select:none;"><span class="subsup"><sup style="color:#8f0000">✘</sup><sub style="color:#8f0000">✘</sub></span>&nbsp;&nbsp;</span>');
            delete json[article];
            continue;
          }
          var html = NormdatenMarkierung.makeHtml(details);
          if(details.redir) {
            details = json[details.redir];
            if(details != null) {
              html += NormdatenMarkierung.makeHtml(details);
              delete json[details.redir];
            }
          }
          links[i].insertAdjacentHTML('afterend', html);
          delete json[article];
        }
      }
    }
  },

  isIn : function(element, naviNoTableAll, maxRecursion = 15) {
    if(naviNoTableAll == 0) {
      // in Navigationsleiste
      element = element.parentElement;
      if(element == null)
        return false;
      if(element.tagName == 'DIV' && (element.classList.contains('NavFrame') || element.classList.contains('NavHead') || element.classList.contains('NavContent')))
        return true;
    }
    else if(naviNoTableAll == 1) {
      // Nicht in Tabellen (Infoboxen)
      if(element == null)
        return true;
      if(element.tagName == 'TABLE' && (element.classList.contains('infobox') || element.classList.contains('float-right')))
        return false;
      if(element.tagName == 'FIGCAPTION')
        return true;
      if(element.id == 'mw-content-text')
        return true;
    }
    else 
      return true;

    if(maxRecursion == 0)
      return true;
    return NormdatenMarkierung.isIn(element, naviNoTableAll, maxRecursion - 1);
  },

  makeHtml : function(details) {
    var html = '';
    if(details.typ)
      html = '<span style="color:#003f00; background-color:#e0e0e0; -moz-user-select:none;">' + String.fromCharCode(details.typ.charCodeAt(0) + 9327);
    else
      html = '<span style="background-color:#e0e0e0; -moz-user-select:none;">';
    html += '<span class="subsup">';
    if(details.gnd)
      if(details.viaf)
        html += '<sup style="color:#008f00">✔</sup><sub style="color:#008f00">✔</sub>';
      else if(details.typ == 's')
        html += '<sup style="color:#008f00">✔</sup><sub>&nbsp;</sub>';
      else
        html += '<sup style="color:#008f00">✔</sup><sub style="color:#8f0000">✘</sub>';
    else if(details.viaf)
      html += '<sup style="color:#8f0000">✘</sup><sub style="color:#008f00">✔</sub>';
    else if(details.typ == 's')
      html += '<sup style="color:#8f0000">✘</sup><sub>&nbsp;</sub>';
    else
      html += '<sup style="color:#8f0000">✘</sup><sub style="color:#8f0000">✘</sub>';
    return html + '</span>&nbsp;&nbsp;</span>';
  }
  /* vim: se ai sw=2 expandtab */
};
$(NormdatenMarkierung.execute);