Jump to content

User:Bawolff/DYKVerified.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bawolff (talk | contribs) at 11:50, 18 December 2009 (init). 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.
importScriptURI('http://en.wikinews.org/w/index.php?action=raw&ctype=text/javascript&title=User:Bawolff/mwapilib2.js')

function filterDYKSuggest(text) {
 var lines = text.split('\n');
 var out = '';
 var mode = 0;
 var h2 = /^==[^=].*[^=]==$/, h3 = /^===[^=].*[^=]===$/, h4 = /^====[^=].*[^=]====$/;
 var verified = /\[\[File:Symbol confirmed\.svg\|16px\]\]|\[\[File:Pictogram voting keep\.svg\|16px\]\]/;
 var buffer = '';
 loop: for (var i = 0; i < lines.length; i++) {
  switch (mode) {
   case 0: //before text
    if (lines[i] === '==Candidate entries==') {
     //alert('cand');
     //out += lines[i] + '\n';
     mode++;
     continue loop;
    }
   break;
   case 1: //in possibly but not yet interesting part
    if (lines[i].match(h2)) { // next section
     //alert('match h2' + lines[i] + mode);
     mode = 3;
     break loop; //done
    }
    if (lines[i].match(h3)) {
     buffer = ''; //reset buffer
     out += lines[i] + '\n';
     continue loop;
    }
    if (lines[i].match(h4)) {
     buffer = lines[i] + '\n';
     continue loop;
    }
    if (lines[i].match(verified)) {
     mode++;
    }
    buffer += lines[i] + '\n';
   break;
   case 2: //in interesting nom.
    if (lines[i].match(h2)) { // next section
     //alert('match h2' + lines[i] + mode);
     out += buffer;
     mode = 3;
     break loop; //done
    }
    if (lines[i].match(h3)) {
     out += buffer;
     buffer = ''; //reset buffer
     out += lines[i] + '\n';
     mode--;
     continue loop;
    }
    if (lines[i].match(h4)) {
     out += buffer;
     buffer = lines[i] + '\n';
     mode--;
     continue loop;
    }
   buffer += lines[i] + '\n'
   break;
   default:
    break loop;
  }
 }
 return out;
}
function DYKUpdateVerified () {
api('template_talk:Did you know').getPage().lift(filterDYKSuggest).
setDefaultSummary('Updating Verified DYK (using experimental [[User:Bawolff/DYKVerified.js]])').
savePage('User:Bawolff/DYKVerified').exec();
}
addOnloadHook(function () {
var button = document.getElementById('DYKUpdateVerified');

if (button) button.innerHTML = "<button type='button' onclick='DYKUpdateVerified();'>Update verified DYK list</button>";

});