User:Jrp/monobook.js
Appearance
< User:Jrp
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. |
![]() | The accompanying .css page for this skin can be added at User:Jrp/monobook.css. |
importScript('User:AndyZ/peerreviewer.js'); //[[User:AndyZ/peerreviewer.js]]
/* Disambiguation lookup script, version [0.1.5b]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/dabfinder.js
Notes:
* Uses the API using head/appendchild(script) and callback to avoid ajax.
* Alt command finds and hilights redirects with simple CSS append (class="mw-redirect").
Operation:
* Adds portlet button (or works with URI parameter &finddab=true)
** Makes call to MediaWiki:Disambiguationspage links, generates array of these.
*** Makes call to generator=links / prop=templates on page title.
**** Builds array of registered links that link to disambiguation pages (that contain a disambiguation template).
***** Iterates over all <a> link objects on page, matching any that link to disambiguation, and applies red dashed border.
This is a bit messy but at the time was the easiest and most thorough way to do it I could think of.
Of course, a bot might be much more efficient, but this is handy for quick on-the-fly live checking.
To do:
* test it
* centralized link list in contentSub?
* ?
*/
var dabnames = new Array();
if(wgNamespaceNumber != -1) addOnloadHook(findDABsButton)
function findDABsButton() {
addPortletLink('p-tb','javascript:findDABs()','Find disambiguations','t-dab');
if(queryString('finddab')=='true') findDABs();
addPortletLink('p-tb','javascript:findRDRs()','Find redirects','t-rdr');
if(queryString('findrdr')=='true') findRDRs();
}
function findRDRs() {
var text = '.mw-redirect { background-color: #ffff00;}\n#t-rdr:after {content:" (redirects hilighted)"; background-color:#ffff00; }';
var s = document.createElement('style');
s.type = 'text/css';
s.rel = 'stylesheet';
if (s.styleSheet) s.styleSheet.cssText = text
else s.appendChild(document.createTextNode(text + ''))
document.getElementsByTagName('head')[0].appendChild(s);
}
function findDABs() {
var dab = document.getElementById('t-dab');
if(dab) {
dab.getElementsByTagName('a')[0].style.display = 'none';
var prog = document.createElement('img');
prog.setAttribute('id','dab-prog');
prog.setAttribute('src',stylepath + '/common/images/spinner.gif');
dab.appendChild(document.createTextNode('Searching...'));
dab.appendChild(prog);
}
var url = wgServer + wgScriptPath + '/api.php?action=query&prop=links&tlnamespace=10&format=json&callback=findDABsCB&titles=MediaWiki:Disambiguationspage';
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',url);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
function findDABsCB(obj) {
if(!obj['query'] || !obj['query']['pages']) return
for(var i in obj['query']['pages']) var links = obj['query']['pages'][i]['links']
if(!links) return
for(var i=0;i<links.length;i++) {
dabnames[dabnames.length] = links[i]['title'];
}
var url = wgServer + wgScriptPath + '/api.php?action=query&redirects&generator=links&prop=templates&format=json&callback=findDABlinksCB&titles=' + encodeURIComponent(wgPageName);
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',url);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
function findDABlinksCB(obj) {
var dablinks = new Array();
if(!obj['query'] || !obj['query']['pages']) return
var links = new Array()
for(var i in obj['query']['pages']) {
for(var j in obj['query']['pages'][i]['templates']) {
var tpl = obj['query']['pages'][i]['templates'][j]['title'];
for(var k=0;k<dabnames.length;k++) {
if(tpl == dabnames[k]) {
dablinks[dablinks.length] = obj['query']['pages'][i]['title'];
}
}
}
}
if(obj['query']['redirects']) {
var dablen = dablinks.length //don't iterate over additions.
for(var i in obj['query']['redirects']) {
for(var j=0;j<dablen;j++) {
if(obj['query']['redirects'][i]['to'] == dablinks[j]) {
dablinks[dablinks.length] = obj['query']['redirects'][i]['from'];
}
}
}
}
var links = (document.getElementById('content')) ? document.getElementById('content').getElementsByTagName('a') : document.getElementById('mw_content').getElementsByTagName('a')
var found = 0;
for(var i=0;i<links.length;i++) {
for(var j=0;j<dablinks.length;j++) {
//to match "Foo (bar)" with "/wiki/Foo_%28bar%29", have to do some hacky string manipulation
var dablink = wgArticlePath.replace(/\$1/,'') + escape(dablinks[j].replace(/ /g,'_'));
if(links[i].href.replace(wgServer,'') == dablink) {
links[i].style.border = '2px solid #00ff00';
found++;
}
}
}
var dab = document.getElementById('t-dab');
if(dab) {
document.getElementById('dab-prog').style.display = 'none';
dab.appendChild(document.createElement('br'));
if(found > 0) {
var span = document.createElement('span');
span.appendChild(document.createTextNode(found + ' links to disambiguation pages found.'));
span.style.border = '2px solid #00ff00';
dab.appendChild(span);
} else {
dab.appendChild(document.createTextNode('No disambiguation links found.'));
}
} else {
alert(found + ' links to disambiguation pages found.');
}
}
function queryString(p) {
var re = RegExp('[&?]' + p + '=([^&]*)');
var matches;
if (matches = re.exec(document.location)) {
try {
return decodeURI(matches[1]);
} catch (e) {
}
}
return null;
}