Jump to content

User:Splarka/watchimages.js

From 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.
/* Semi-automatic image watcher, version [0.0.3a]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/watchimages.js

Operation:
* Checks which images are being used on a page
* Sorts out those from commons, builds a list of those local or missing
* Passes this list to Special:Watchlist/raw (but does not save, being a test script).
* When saved, returns to the original page.

Notes: 
* The logic of which images to watch is easily changed (see below)
* This avoids ajax editing (which is a pain). Possibly the API actions will be enabled later.

To do after more testing:
* Abort if no matching images are found.
* Check page more specifically for a content type (page has ID? etc).
* Auto-submit if so desired.
*/

if(mw.config.get('wgNamespaceNumber') != -1) addOnloadHook(watchAllImagesButton)
function watchAllImagesButton() {
  mw.util.addPortletLink('p-tb','javascript:watchAllImages()','Watch images','t-wai','Add all images on this page to your watchlist.');
}

function watchAllImages() {
  var wai = document.getElementById('t-wai');
  if(wai) {
    wai.getElementsByTagName('a')[0].style.display = 'none';
    var prog = document.createElement('img');
    prog.setAttribute('id','wai-prog');
    prog.setAttribute('src',stylepath + '/common/images/spinner.gif');
    wai.appendChild(document.createTextNode('Working...'));
    wai.appendChild(prog);
  }
  var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') +'/api.php?action=query&format=json&callback=watchAllImagesCB&generator=images&prop=imageinfo&iiprop=&iilimit=1&titles=' + encodeURIComponent(mw.config.get('wgPageName'))
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

function watchAllImagesCB(obj) {
  document.getElementById('wai-prog').style.display = 'none';
  var towatch = '';
  if(!obj['query'] || !obj['query']['pages']) return
  var images = obj['query']['pages'];
  for(var i in images) {
    // Logic variables to determine which images to watch
    // islocal = image is local, rather than shared (from commons).
    // haspage = has a description page, all local and some shared images have this.
    // hasmedia = the image exists, either locally or shared.

    var islocal = (images[i]['imagerepository'] == 'local')
    var haspage = i > 0
    var hasmedia = (images[i]['imagerepository'] != '')

    //(!hasmedia || islocal) => all local images, as well as all missing (deleted or soon to be uploaded) images
    if(!hasmedia || islocal) {
      towatch += images[i]['title'] += '\n';
    }
  }
  var url = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace(/\$1/,'Special:Watchlist/raw') + '?fromtitle=' + encodeURIComponent(mw.config.get('wgPageName')) + '&towatch=' + encodeURIComponent(towatch);
  document.location.href = url;
}

if(queryString('towatch') && mw.config.get('wgCanonicalSpecialPageName') == 'Watchlist') addOnloadHook(addToWatchlist)
function addToWatchlist() {
  var txt = document.getElementById('titles');
  var towatch = decodeURIComponent(queryString('towatch'));
  if(!txt) return
  if(queryString('fromtitle')) {
    var forms = document.forms;
    for(var i=0;i<forms.length;i++) {
      if(forms[i].action.indexOf('Watchlist') != -1) {
        forms[i].action += '&pleasereturnto=' + queryString('fromtitle');
      }
    }
  }
  txt.value += towatch;
  alert('Note: the following images will be added to your watchlist upon save:\n' + towatch);
}

if(queryString('pleasereturnto') && mw.config.get('wgCanonicalSpecialPageName') == 'Watchlist') addOnloadHook(addToWatchlistDone)
function addToWatchlistDone() {
  var url = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace(/\$1/,decodeURIComponent(queryString('pleasereturnto')));
  document.location.href = url;
}

function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}