User:Dschwen/highlightredirects.js: Difference between revisions
Appearance
Content deleted Content added
commit changes by User:Archanamiya |
yeah right. chill, it was only an exercise in XHR to begin with... |
||
Line 4: | Line 4: | ||
tab_redirects : null, |
tab_redirects : null, |
||
status: null, |
|||
xhr : null, |
|||
todo : null, |
|||
num : { total:0, done:0, redir:0 }, |
|||
addStylesheetRule : function(tag, style) { |
|||
// |
|||
var ss = document.styleSheets[0]; |
|||
// Try to create an XMLHTTP request object for each tile |
|||
if (ss.insertRule) { |
|||
// with maximum browser compat. |
|||
ss.insertRule(tag + '{' + style + '}', ss.cssRules.length); |
|||
// code adapted from http://jibbering.com/2002/4/httprequest.html |
|||
} else if (ss.addRule) { |
|||
// |
|||
ss.addRule(tag, style); |
|||
createXMLHTTP : function() |
|||
{ |
|||
var i, xhr; |
|||
/*@cc_on @*/ |
|||
/*@if (@_jscript_version >= 5) |
|||
// Internet Explorer (uses Conditional compilation) |
|||
// traps security blocked creation of the objects. |
|||
wmaDebug('Microsoft section'); |
|||
try { |
|||
xhr = new ActiveXObject("Msxml2.XMLHTTP"); |
|||
} catch (e) { |
|||
try { |
|||
xhr = new ActiveXObject("Microsoft.XMLHTTP"); |
|||
} catch (E) { |
|||
xhr = false; |
|||
} |
|||
} |
|||
@end @*/ |
|||
// Firefox, Konqueror, Safari, Mozilla |
|||
if (!xhr && typeof(XMLHttpRequest) != 'undefined') { |
|||
try { |
|||
xhr = new XMLHttpRequest(); |
|||
} catch (e) { |
|||
xhr = false; |
|||
} |
|||
} |
} |
||
// ICE browser |
|||
if (!xhr && window.createRequest) { |
|||
try { |
|||
xhr = new window.createRequest(); |
|||
} catch (e) { |
|||
xhr = false; |
|||
} |
|||
} |
|||
return xhr; |
|||
}, |
}, |
||
updateStatus : function() |
|||
{ |
|||
with( highlightRedirects ) |
|||
{ |
|||
status.nodeValue = ' (' + num.redir + '/' + num.done + '/' + num.total + ')'; |
|||
} |
|||
}, |
|||
// cross-browser event attachment (John Resig) |
|||
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html |
|||
addEvent : function ( obj, type, fn ) |
|||
{ |
|||
if (obj.addEventListener) |
|||
obj.addEventListener( type, fn, false ); |
|||
else if (obj.attachEvent) |
|||
{ |
|||
obj["e"+type+fn] = fn; |
|||
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); } |
|||
obj.attachEvent( "on"+type, obj[type+fn] ); |
|||
} |
|||
}, |
|||
run : function() |
run : function() |
||
{ |
{ |
||
highlightRedirects.addStylesheetRule('a.mw-redirect', 'color:green'); |
|||
var links = document.getElementById('bodyContent').getElementsByTagName('a'); |
|||
var len = links.length; |
|||
var local = window.location.href + '#' |
|||
if( highlightRedirects.todo != null ) return; |
|||
highlightRedirects.todo = new Array(); |
|||
for( var key = 0; key < len; key++ ) |
|||
{ |
|||
if( links[key].href && |
|||
links[key].pathname && |
|||
links[key].pathname.substr(0,6) == '/wiki/' && |
|||
!( links[key].href.substr(0,local.length) == local ) && |
|||
!( links[key].pathname.substr(6,8) == 'Special:' ) ) |
|||
{ |
|||
highlightRedirects.todo[highlightRedirects.num.total] = |
|||
{ |
|||
link : links[key], |
|||
xhr : highlightRedirects.createXMLHTTP(), |
|||
func : new Function( |
|||
'var me = highlightRedirects.todo['+highlightRedirects.num.total+'];' + |
|||
'if(me.xhr.readyState==4) {' + |
|||
'var ro=eval("("+me.xhr.responseText+")");' + |
|||
'var redir=false;' + |
|||
'for( var k in ro.query.pages ) {' + |
|||
'if( typeof(ro.query.pages[k].redirect) != "undefined" ) redir=true;' + |
|||
'}' + |
|||
'if(redir) {' + |
|||
'me.link.style.color="green";' + |
|||
'highlightRedirects.num.redir++;' + |
|||
'}' + |
|||
'highlightRedirects.num.done++; highlightRedirects.updateStatus();' + |
|||
'}' |
|||
) |
|||
} |
|||
links[key].style.color = 'gray'; |
|||
with(highlightRedirects.todo[highlightRedirects.num.total]) |
|||
{ |
|||
xhr.open("GET", |
|||
"/w/api.php?action=query&prop=info&format=json&titles=" + |
|||
links[key].pathname.substr(6), true); |
|||
xhr.onreadystatechange = func; |
|||
xhr.send( null ); |
|||
} |
|||
highlightRedirects.num.total++; |
|||
} |
|||
} |
|||
}, |
}, |
||
Line 138: | Line 24: | ||
{ |
{ |
||
tab_redirects = addPortletLink ('p-cactions', 'javascript:highlightRedirects.run();', 'redirects'); |
tab_redirects = addPortletLink ('p-cactions', 'javascript:highlightRedirects.run();', 'redirects'); |
||
status = document.createTextNode(''); |
|||
tab_redirects.appendChild( status ); |
|||
if( document.getElementById('ca-history') ) |
if( document.getElementById('ca-history') ) |
Revision as of 15:52, 14 April 2008
if (wgAction != 'edit' && wgCanonicalNamespace != 'Special')
{
var highlightRedirects = {
tab_redirects : null,
addStylesheetRule : function(tag, style) {
var ss = document.styleSheets[0];
if (ss.insertRule) {
ss.insertRule(tag + '{' + style + '}', ss.cssRules.length);
} else if (ss.addRule) {
ss.addRule(tag, style);
}
},
run : function()
{
highlightRedirects.addStylesheetRule('a.mw-redirect', 'color:green');
},
install : function()
{
with(highlightRedirects)
{
tab_redirects = addPortletLink ('p-cactions', 'javascript:highlightRedirects.run();', 'redirects');
if( document.getElementById('ca-history') )
document.getElementById('ca-history').parentNode.appendChild( tab_redirects );
}
}
};
//
// Hook up installation function
//
addOnloadHook(highlightRedirects.install);
}