User talk:Dschwen/highlightredirects.js

  • You might look at querying api.php instead of loading the whole page with action=raw.
  • #REDIRECT is not enough. there can be whitespace between # and REDIRECT which does not have to be uppercase.

Nifty though! — Hippietrail 01:26, 23 January 2007 (UTC)Reply

api.php is a great suggestion, I just implemented it. Drastically reduces the request volume, and creates a appealingly absurd third layer of JavaScript interpretation (to eval the returned JSON string) :-). --Dschwen(A) 23:36, 14 February 2007 (UTC)Reply

Suggested changes

edit

G'day, very impressive! I respectfully suggest a couple of changes, though:

  • it currently doesn't go past any anchors that are targets (i.e. have a name but no href). Fixed by testing links[key].href when you test links[key].pathname:
if( links[key].href && 
   links[key].pathname &&
   links[key].pathname.substr(0,6) == '/wiki/' &&
   !( links[key].href.substr(0,local.length) == local ) )
  • it currently only picks up the uppercase form of redirect, and as stated above, ignores the permissible form with a space between the # and the redirect. Fixed by changing the test to a regex:
'if(/\#.?redirect/i.test(me.xhr.responseText)) {' +

cheers, Webaware talk 14:08, 14 February 2007 (UTC)Reply

Thanks a lot for your suggestions, and especially for providing the code snippets along with them! The whole thing was created as a proof of concept. If you have further ideas or suggestions please don't hold back. I appreciate any kind of input. --Dschwen(A) 22:44, 14 February 2007 (UTC)Reply
Thanks a lot for coming up with this. Not only is it very useful, it's also quite instructive - I must get my head around AJAX sometime, and now I have somewhere I can play with it without it being too boring!
I've just noticed a couple of other instances where it stops short of getting all links, so I'll investigate and post back the fix(es). cheers, Webaware talk 23:18, 14 February 2007 (UTC)Reply
Ok, I've noticed failing XHRs too, but I just cannot get my finger on them. And I just switched to requesting api.php (see above). --Dschwen(A) 23:36, 14 February 2007 (UTC)Reply
This looks interesting, and the script now picks up all links on the pages where I found it wasn't getting them before. However, you currently have it hard-coded for en.wiki.x.io pages; it would be handy if you truncate that to just /w/api.php?..., so that the script can still be used from other Wiki projects (like Wikibooks, *.wiki.x.io, etc.) Webaware talk 02:19, 15 February 2007 (UTC)Reply
Yeah, what was I thinking (it was late, local time, in my defense). Works great, plus I added a line to ignore links to Special: pages (as in the category list below the article). --Dschwen(A) 08:56, 15 February 2007 (UTC)Reply
Beaut, it works nicely now on Wikibooks too. Very nice! And it's inspiring me to stick my nose into some AJAX finally, too :-) cheers, Webaware talk 09:13, 15 February 2007 (UTC)Reply


Hi! Can you make this script to work in IE too? I'd like to use it but I cannot. (And I won't use Firefox!!!) Diaby talk 19:46, 14 January 2008 (UTC)Reply

I can try, it's been a while since I worked on that script, and I forgot the IE quirk that makes it fail. I'll try and see if i can find the reason again... --Dschwen 20:07, 14 January 2008 (UTC)Reply

Thank you! Diaby talk 19:46, 22 January 2008 (UTC)Reply

Comment

edit

This looks like a very useful script :) It turns out, though, that the functionality has recently been added to MediaWiki, so run could be replaced with:

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);
    }
}

addStylesheetRule('a.mw-redirect', 'color:green');

...or something like that. (addStylesheetRule from User:Quarl/redirector.js) In addition, users could add something to their monobook.css to have redirects be highlighted on every page. Cheers, GracenotesT § 17:32, 6 April 2008 (UTC)Reply

I second that. Why make a lot of queries to the server (which, by the way, should have been just ONE query from the beginning) for information that's already included on the page? —AlexSm 15:45, 14 April 2008 (UTC)Reply
Done. But please, enlighten me how I could have done this with ONE query. In any case the script was a programming exercise for XHRs in the first place.. --Dschwen 15:55, 14 April 2008 (UTC)Reply
...&titles=Main Page|Main page|Mainpage, unless, of course, this wasn't implemented at the time the script was written. Thanks for the rewrite. However, you might want to actually count those mw-redirect links in order to duplicate previous functionality, otherwise some users might complain that the script doesn't seem to work anymore :) —AlexSm 16:10, 14 April 2008 (UTC)Reply

Appears on Preview page

edit

Since you purposely hide this function from the Edit page, I don't know if it is intentional that the button still appears for the Preview page? Gary King (talk) 23:02, 5 May 2008 (UTC)Reply

Hmm, why not, there is visible rendered article text on the preview page, so it should work there, unlike the edit page, which has no article text links. --Dschwen 02:01, 6 May 2008 (UTC)Reply
That depends on a user's settings; I've got the text already showing on the edit page. Also, I see no reason for the script to work on any page except for the Article namespace, and possibly the Wikipedia namespace. I'm going to place the script in a wrapper to remove it from the User talk namespace because even with 1440 pixels horizontally on my screen, I still get a horizontal scroll bar for User Talk pages. Gary King (talk) 02:17, 6 May 2008 (UTC)Reply

addStylesheetRule

edit

addStylesheetRule breaks with the change of CSSs to bits.wikimedia.org Consider using appendCSS instead. Platonides (talk) 15:26, 30 January 2010 (UTC)Reply

Thanks. I made the change. --Dschwen 16:33, 30 January 2010 (UTC)Reply

ResourceLoader utlitities + future proof

edit

Hi,

While I was clean up some old javascript and checking out gadgets on another wiki I came accros this script (it was copied there) and I gave it a quick update.

// Makes all links to redirects into green
// [[:en:User:Dschwen/highlightredirects.js]]

// Don't load on edit or special pages & don't load twice
if ( mw.config.get( 'wgAction' ) !== 'edit' && mw.config.get( 'wgCanonicalNamespace' ) !== 'Special'
	&& typeof highlightRedirects == 'undefined' ) {

	var highlightRedirects = {

		tab_redirects : null,

		run : function() {
			mw.util.addCSS( 'a.mw-redirect{ color:green }' );
		},

		install : function() {

			this.tab_redirects = mw.util.addPortletLink( 'p-cactions', 'javascript:highlightRedirects.run();', 'Redirects' );
			
			if ( document.getElementById('ca-history') ) {
				document.getElementById('ca-history').parentNode.appendChild( this.tab_redirects  );
			}
		}
	};
	jQuery(document).ready( highlightRedirects.install );
};

Please note that this is meant to be run on MediaWiki 1.17 or higher (it won't work on English Wikipedia yet, and the current version will probably not brake right away under 1.17 but this version is future proof and free of function calls that are deprecated (Deprecated functions?) which may be removed in a few months. Krinkle (talk) 02:05, 15 February 2011 (UTC)Reply

There is another update on pt:MediaWiki:Gadget-Redirecionamentos.js.
The script doesn't seems to be useful in actions other than 'view', 'submit' or 'purge' and it was necessary to use window.highlightRedirects to use it with [ResourceLoader] option in MediaWiki:Gadgets-definition. Helder 15:21, 27 February 2011 (UTC)Reply