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 10:38, 19 December 2009 (use variables). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/******
To use, add the following line to [[special:mypage/monobook.js]] (or [[special:mypage/vector.js]] depending on skin pref)
 importScript('User:Bawolff/DYKVerified.js');

This script looks for the code <span id='DYKUpdateVerified'/> and replaces it with an update button. IF you also want it in the sidebar under the toolbox, add the following line (in addition to the importScript):
 addOnloadHook(function () {addPortletLink('p-tb', 'javascript:DYKUpdateVerified();void%200', 'Update DYKVerified');});

This script scans through [[T:TDYK]], finds are entries that are current candidates that have been either verified or verified AGF (identifying them by the 16px check mark image), and pastes them on to another page (currently [[user:Bawolff/DYKVerified.js]])

As with all my scripts, feel free to send my comments, suggestions, hate mail, etc. Since this is the topic of an active thread at http://en.wikipedia.org/wiki/Wikipedia_talk:Did_you_know#A_more_comprehensive_proposal , comments should go there. Otherwise you can also find me on irc (nick bawolff on #wikinews on irc.freenode.net) or leave a message on my talk page (I check [[wikinews:user talk:Bawolff]] more often then i check my local wikipedia talk page, but i check both quite regurally). 
********/


importScriptURI('http://en.wikinews.org/w/index.php?action=raw&ctype=text/javascript&title=User:Bawolff/mwapilib2.js')

function filterDYKSuggest(text) {
 var target = 'User:Bawolff/DYKVerified.js';
 var lines = text.split('\n');
 var intro = '<noinclude>\n{{ambox|text=This page contains DYK suggestions that have been verified. It is copied from [[T:TDYK]] by a script. If you wish to add a comment to one of these entries, please do so on [[T:TDYK]].<div id="DYKUpdateVerified"/>}}\n<!--\n************************\nPLEASE DO NOT EDIT THIS PAGE. EDIT [[T:TDYK]] INSTEAD.\n Edits to this page will be lost\n\n****************-->\n</noinclude>\n'
 var out = '';
 var mode = 0;
 var h2 = /^==[^=].*[^=]==$/, h3 = /^===[^=].*[^=]===$/, h4 = /^====[^=].*[^=]====$/;
 var verified = /\[\[[FfIi][mMiI][aALl][gGEe][eE]?:Symbol confirmed\.svg\|16px\]\]|\[\[[FfIi][mMiI][aALl][gGEe][eE]?: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 intro + out;
}
function DYKUpdateVerified () {
 api('template_talk:Did you know').
 getPage().
 lift(filterDYKSuggest).
 setDefaultSummary('Updating Verified DYK (using experimental [[User:Bawolff/DYKVerified.js]])').
 savePage(target).
 lift(function () { 
  if (wgPageName === target) {
   location.reload();
  }
  else {
   alert('DYKVerified updated.');
  }
 }).
 exec();
}
addOnloadHook(function () {
var button = document.getElementById('DYKUpdateVerified');

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

});