View difference between Paste ID: BHZ6YVth and eQLafHp8
SHOW: | | - or go back to the newest paste.
1-
$('#WikiHeader').append('<span class="wikia-button" onclick="name_of_function();">Text on the button</span>');
1+
$('#WikiHeader').append('<a class="wikia-button" onclick="this.innerHTML = \'Processing...\'; FixRedlinks();">Fix redlinks</a>');
2
3-
$.getJSON('/api.php?action=query&prop=revisions&titles=' + wgPageName + '&rvprop=content&indexpageids=1&format=json', function(result) {
3+
function FixRedlinks() {
4
	$.getJSON('/api.php?action=query&prop=revisions&titles=' + wgPageName + '&rvprop=content&indexpageids=1&format=json', function(result) {
5-
 result.query.pages[result.query.pageids[0]].revisions[0]['*']
5+
		var text = result.query.pages[result.query.pageids[0]].revisions[0]['*'];
6-
 $('a.new')
6+
		var redlinks = $('a.new');
7
 
8-
var ref = 0;
8+
		var ref = 0;
9-
while(text.indexOf('[[', ref) != -1) {
9+
		while(text.indexOf('[[', ref) != -1) {
10-
	if(text.indexOf(']]', text.indexOf('[[', ref)) != -1) {
10+
			if(text.indexOf(']]', text.indexOf('[[', ref)) != -1) {
11-
		var open = text.indexOf('[[', ref);
11+
				var open = text.indexOf('[[', ref);
12-
		var pipe = text.indexOf('|', open);
12+
				var pipe = text.indexOf('|', open);
13-
		var close = text.indexOf(']]', open);
13+
				var close = text.indexOf(']]', open);
14-
		if(text.indexOf('<br/>', open) != -1 && text.indexOf('<br/>', open) < close) {ref = open + 1; continue;}
14+
				if(text.indexOf('<br/>', open) != -1 && text.indexOf('<br/>', open) < close) {ref = open + 1; continue;}
15-
		if(pipe != -1 && pipe < close) { //is [[page|display]]
15+
				if(pipe != -1 && pipe < close) { //is [[page|display]]
16-
			if(pipe == close - 1) {
16+
					if(pipe == close - 1) {
17-
				var title = text.substring(open + 2, pipe);
17+
						var title = text.substring(open + 2, pipe);
18-
				var display = title.substring(title.indexOf(':') + 1);
18+
						var display = title.substring(title.indexOf(':') + 1);
19
					}
20-
			else {
20+
					else {
21-
				var title = text.substring(open + 2, pipe);
21+
						var title = text.substring(open + 2, pipe);
22-
				var display = text.substring(pipe + 1, close);
22+
						var display = text.substring(pipe + 1, close);
23
					}
24
				}
25-
		else { //is [[page]]
25+
				else { //is [[page]]
26-
			var title = text.substring(open + 2, close);
26+
					var title = text.substring(open + 2, close);
27-
			var display = title;
27+
					var display = title;
28
				}
29-
		if(!title) {ref = open + 1; continue;} //skip [[]] and [[|<anything>]]
29+
				if(!title) {ref = open + 1; continue;} //skip [[]] and [[|<anything>]]
30
		
31-
		for(var i = 0; i < redlinks.length; i++) {
31+
				for(var i = 0; i < redlinks.length; i++) {
32-
			if(title[0].toUpperCase() + title.substring(1) == redlinks[i].title.substring(0, ' (page does not exist)'.length)) {
32+
					var utitle = title[0].toUpperCase() + title.substring(1);
33-
				if(pipe == close - 1) {var show = title;}
33+
					if(redlinks[i].title.indexOf(' (page does not exist)')) {var redlink = redlinks[i].title.substring(0, redlinks[i].title.length - 22);}
34-
				else {var show = display;}
34+
					else {var redlink = redlinks[i];}
35-
				text = text.substring(0, open) + show + text.substring(close + 2);
35+
					if(utitle == redlink || utitle == ':' + redlink) {
36-
				break;
36+
						if(pipe == close - 1) {var show = title;}
37
						else {var show = display;}
38
						text = text.substring(0, open) + show + text.substring(close + 2);
39-
		ref = open + 2;
39+
						break;
40-
	}
40+
					}
41-
	else {break;}
41+
				}
42-
}
42+
				ref = open + 2;
43
			}
44-
});
44+
			else {break;}
45
		}
46-
 $.getJSON('action=query&prop=info&titles=' + wgPageName + '&intoken=edit&indexpageids=1&format=json', function(result) { var token = result.query.pages[result.query.pageids[0]].edittoken; });
46+
47-
 $.post('/api.php', {action: 'edit', title: wgPageName, text: text, summary: 'Fixing redlinks', token: token}, function() {window.location.reload();});
47+
		$.getJSON('/api.php?action=query&prop=info&titles=' + wgPageName + '&intoken=edit&indexpageids=1&format=json', function(result) {
48
			var token = result.query.pages[result.query.pageids[0]].edittoken;
49
			$.post('/api.php', {action: 'edit', title: wgPageName, text: text, summary: 'Fixing redlinks', token: token}, function() {window.location.reload();});
50
		});
51
	});
52
}