Jump to content

User:Jitse Niesen/Client-side preferences/Main.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jitse Niesen (talk | contribs) at 01:22, 28 January 2006 (test). 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.
function clientSidePreferencesLoadFunction() {
   var el = document.getElementById('bodyContent').childNodes[0];
   while (el.nextSibling) {
      el = el.nextSibling;
      if (el.tagName && el.tagName.toLowerCase() == 'p') 
         break;
   }
   var cspForm = document.createElement("form");
   cspForm.innerHTML = '' + 
      '<form><p>' +
      '<input type="checkbox" name="tabPurge" />&nbsp;' +
      'Add \'purge\' tab to clear Wikipedia cache of page<br />' +
      '<input type="checkbox" name="tabSince" />&nbsp;' +
      'Add \'since\' tab to show changes since user last edited given page<br />' +
      '<input type="button" onclick="cspProcessForm();" value="Generate script" /><br />' +
      '</p></form>';
   cspForm.name = 'cspForm';
   el.parentNode.replaceChild(cspForm, el);
}

function cspProcessForm() {
   window.newscript = 'if (location.href == "http://en.wikipedia.org/wiki/' +
                                             'User:Jitse_Niesen/Client-side_preferences")\n' + 
                      '   document.write(\'<script type="text/javascript" src="\' \n' +
                      '                  + \'http://en.wikipedia.org/w/index.php?title=User:Jitse_Niesen' +
                                                            '/Client-side_preferences/Main.js\' \n' +
                      '                  + \'&action=raw&ctype=text/javascript&dontcountme=s"></script>\'); \n';

   window.fragments = new Array();

   if (document.cspForm.tabPurge.checked) 
      window.fragments = window.fragments.concat('Add_purge_to_tabs', 'addLink');
   if (document.cspForm.tabSince.checked) 
      window.fragments = window.fragments.concat('Changes_since_I_last_edited', 'Add_LI_link');

   window.bottom = document.cspForm;

   cspDownloadFragments();
}

function cspDownloadFragments() {
   if (window.fragments.length == 0) {
      cspLoadEditPage();
      return;
   } 

   var fragment = window.fragments[0];

   var msg = document.createTextNode('Loading ' + fragment + '.js ...');
   window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);

   // Copied from http://developer.mozilla.org/en/docs/AJAX:Getting_Started
   if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      http_request = new XMLHttpRequest();
   } else if (window.ActiveXObject) { // IE
      http_request = new ActiveXObject("Microsoft.XMLHTTP");
   }
   window.http_request = new XMLHttpRequest();
   window.http_request.overrideMimeType('text/xml');
   window.http_request.onreadystatechange = cspReceivedFragment;
   var url = 'http://en.wikipedia.org/w/index.php?title=User:Jitse_Niesen/Client-side_preferences/' +
             fragment + '.js&action=raw&ctype=text/javascript&dontcountme=s';
   window.http_request.open('GET', url, true);
   window.http_request.send(null);
}

function cspReceivedFragment() {
   if (http_request.readyState != 4) {
      // still not ready
      return;
   } 
   if (http_request.status != 200) {
      alert('There was a problem with the request.');
      return;
   }

   var msg = document.createTextNode(' done');
   window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
   window.bottom = window.bottom.parentNode.insertBefore(document.createElement('BR'), bottom.nextSibling);

   window.newscript += '\n// From [[User:Jitse Niesen/Client-side preferences/' + window.fragments[0] + 
                       ']]\n\n' + http_request.responseText + '\n';
   window.fragments = window.fragments.slice(1);
   cspDownloadFragments();
}

function cspLoadEditPage() {
   var msg = document.createTextNode('Loading your user script ...');
   window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);

   var userpageURL = document.getElementById('pt-userpage').firstChild.href;
   var username = userpageURL.slice('http://en.wikipedia.org/User:'.length)
   var newURL = 'http://en.wikipedia.org/w/index.php?title=User:' + username + '/monobook.js&action=edit';
   var newwin = window.open(newURL, '_blank');
   newwin.onload = cspSaveEditPage;
   window.newwin = newwin;
}

function cspSaveEditPage() {
   var msg = document.createTextNode(' saving new script');
   window.bottom = window.bottom.parentNode.insertBefore(msg, bottom.nextSibling);
   window.bottom = window.bottom.parentNode.insertBefore(document.createElement('BR'), bottom.nextSibling);

   window.newwin.document.editform.wpTextbox1.value = window.newscript;
   window.newwin.document.editform.wpSummary.value = 'Updated client-side preferences';
   window.newwin.document.editform.wpMinoredit.checked = true;
   window.newwin.document.editform.wpSave.click();
}

addOnloadHook(clientSidePreferencesLoadFunction);