Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
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.jsOperation:* 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)functionwatchAllImagesButton(){mw.util.addPortletLink('p-tb','javascript:watchAllImages()','Watch images','t-wai','Add all images on this page to your watchlist.');}functionwatchAllImages(){varwai=document.getElementById('t-wai');if(wai){wai.getElementsByTagName('a')[0].style.display='none';varprog=document.createElement('img');prog.setAttribute('id','wai-prog');prog.setAttribute('src',stylepath+'/common/images/spinner.gif');wai.appendChild(document.createTextNode('Working...'));wai.appendChild(prog);}varurl=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'))varscriptElem=document.createElement('script');scriptElem.setAttribute('src',url);scriptElem.setAttribute('type','text/javascript');document.getElementsByTagName('head')[0].appendChild(scriptElem);}functionwatchAllImagesCB(obj){document.getElementById('wai-prog').style.display='none';vartowatch='';if(!obj['query']||!obj['query']['pages'])returnvarimages=obj['query']['pages'];for(variinimages){// 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.varislocal=(images[i]['imagerepository']=='local')varhaspage=i>0varhasmedia=(images[i]['imagerepository']!='')//(!hasmedia || islocal) => all local images, as well as all missing (deleted or soon to be uploaded) imagesif(!hasmedia||islocal){towatch+=images[i]['title']+='\n';}}varurl=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)functionaddToWatchlist(){vartxt=document.getElementById('titles');vartowatch=decodeURIComponent(queryString('towatch'));if(!txt)returnif(queryString('fromtitle')){varforms=document.forms;for(vari=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)functionaddToWatchlistDone(){varurl=mw.config.get('wgServer')+mw.config.get('wgArticlePath').replace(/\$1/,decodeURIComponent(queryString('pleasereturnto')));document.location.href=url;}functionqueryString(p){varre=RegExp('[&?]'+p+'=([^&]*)');varmatches;if(matches=re.exec(document.location)){try{returndecodeURI(matches[1]);}catch(e){}}returnnull;}