User:Mr.Z-man/badimages.js

Source: 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.
// For description, see [[User:Mr.Z-man/badimages]]

if(mw.config.get('wgAction') == 'view') {
	preemptImages();
}
function preemptImages() {
  appendCSS('#bodyContent img {visibility:hidden;}\n#bodyContent img.goodimage {visibility:visible;}');
}
 
function appendCSS(text) {
  var s = document.createElement('style');
  s.type = 'text/css';
  s.rel = 'stylesheet';
  if (s.styleSheet) s.styleSheet.cssText = text //IE
  else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
  document.getElementsByTagName('head')[0].appendChild(s);
  return s;
}

function badImageGet() {
  var url = mw.config.get('wgScriptPath') + '/api.php?action=query&titles=MediaWiki:Bad%20image%20list&prop=links&format=json&plnamespace=6&pllimit=500&callback=badImages';

  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

function badImages(obj) {
  if(!obj['query'] || !obj['query']['pages']) return
  badimages = obj['query']['pages']['1724570']['links'];
  var badimagelist=[];
  for (var q=0;q<badimages.length;q++) {
    badimagelist[q] = badimages[q].title;
  }
  badimagelist = badimagelist.toString();
  for (var i=0; i<document.images.length; i++) {
  imagename = document.images[i].src.toString();
    if (imagename.indexOf('upload.wikimedia.org') != -1) {
      if (imagename.indexOf('/thumb/') == -1) {
        imagename = imagename.replace(/http:\/\/upload\.wikimedia\.org\/wikipedia\/\w+\/\w\/\w\w\//, 'Image:');
      } else {
        imagename = imagename.replace(/http:\/\/upload\.wikimedia\.org\/wikipedia\/\w+\/thumb\/\w\/\w\w\//, 'Image:');
        imagename = imagename.replace(/\/\w+px-.*/, '');
      }
      imagename = imagename.replace(/_/g, ' ');
      if (badimagelist.indexOf(imagename) != -1) {
        document.images[i].src = mw.config.get('wgServer') + mw.config.get('wgScript') +'?title=' + imagename;
        document.images[i].alt = imagename;
        document.images[i].className += ' badimage';
      } else {
        document.images[i].className += ' goodimage';
      }
    }
  }
  appendCSS('#bodyContent img.badimage {visibility:visible;}');
}

if(mw.config.get('wgAction') == "view") { 
  $(badImageGet);
}