MediaWiki:Gadget-quickvfd.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
// Depends on Sparkle ([[MediaWiki:Gadget-sparkle.js]]).

qvfd = {};

// Suffix for identifying gadget
qvfd.suffix = " ([[MediaWiki:Gadget-quickvfd.js|quickvfd]])";

qvfd.addQVFD = function() {
	if ($('#ca-edit') && (($('#ca-history')
	&& mw.config.get("wgCanonicalNamespace") != "File")
	|| mw.config.get("wgCanonicalNamespace") == "File")) {
		Sparkle.addPortletLink('p-cactions', qvfd.go, 'QVFD', 'td-qvfd',
		'Add page to QuickVFD', 0);
		Sparkle.addPortletLink('p-cactions', qvfd.go, 'QVFD-C', 'td-qvfdc',
		'Add page to QuickVFD, with a comment', 1);
	} else if (mw.config.get("wgCanonicalSpecialPageName") == 'Newpages'
	|| mw.config.get("wgCanonicalSpecialPageName") == 'Recentchanges') {
		var rows = $('ul')[0].getElementsByTagName('li');
		try {
			for(var x in rows) {
				if (mw.config.get("wgCanonicalSpecialPageName") == 'Newpages') {
					var title = rows[x].getElementsByTagName('a')[0].title;
					rows[x].innerHTML = rows[x].innerHTML.replace(/\[/,
					'(<a href="javascript:qvfd.go(0,\'' + title + '\')">qvfd</a>) [');
				} else {
					var title = rows[x].getElementsByTagName('a')[1].title;
					rows[x].innerHTML = rows[x].innerHTML.replace(/\(diff\)/,
					'(<a href="javascript:qvfd.go(0,\'' + title + '\')">qvfd</a>)'); 
				}
			}
		} catch(e) {
		}
	}
};

qvfd.go = function(com, page) {
	qvfd.comment = (com) ? " - " + escape(prompt("Comment:","")) : '';
	if (com && qvfd.comment == " - null") return;
	qvfd.page = (page) ? page : mw.config.get("wgPageName").replace(/_/g,' ');
	mw.notify( $( '<hr>Originally by <a href="/wiki/User:Villahj_Ideeut">Villahj Ideeut</a>, '
	+ 'modz by <a href="/wiki/User:Spang">Spang</a> and <a href="/wiki/User:Mitch_1_2">Mitch</a>' ),
	{ title: 'QVFDing, please wait...' } );
	qvfd.output = '';
	mw.notify("Loading QVFD page...");
	var api = new mw.Api();
	api.get ({
		action: "query",
		indexpageids: 1,
		prop: "revisions|info|links",
		pllimit: 500,
		rvprop: "content",
		rvlimit: 1,
		rvsection: 1,
		rvslots: "main",
		titles: "Uncyclopedia:QuickVFD"
	}).done(function(o) {
		mw.notify("Checking QVFD for selected page...");
		var id = o.query.pageids[0];
		var titles = o.query.pages[id].links;
		for (var lk in titles) {
			if (titles[lk].title === qvfd.page) {
				mw.notify( "Page already on QVFD!" );
				return;
			}
		}
		mw.notify("Page not listed, adding it now...");
		try {
			var sectionText = o.query.pages[id].revisions[0].slots.main["*"];
			var lines = sectionText.split('\n');
			var redirect = ($('#contentSub').innerHTML.indexOf('Redirect page') == -1) ? 0 : 1;
			if (redirect === 0) {
				lines.splice(1,0,' [[:' + qvfd.page + ']]' + qvfd.comment);
			} else {
				lines.splice(1,0,' {{redirect|' + qvfd.page + '}}' + qvfd.comment);
			}
			sectionText = lines.join('\n');
		} catch(e) {
			mw.notify( $( "Could not add text to the page!<br /><a href=\"\">Close</a>" ) );
			return;
		}
		mw.notify("Saving page...");

		var api = new mw.Api();
		api.post({
			action: "edit",
			title: "Uncyclopedia:QuickVFD",
			section: 1,
			token: mw.user.tokens.get("csrfToken"),
			summary: "+[[" + qvfd.page + "]]" + qvfd.suffix,
			text: sectionText
		}).done(function(o) {
			try {
				result = (o.edit.result == 'Success') ? 1 : 0;
			} catch(e) {
				result = 0;
			}
			if (result) {
				mw.notify( $( "<a href=\"/wiki/Uncyclopedia:QuickVFD\">Go to QVFD</a> "
				+ "or <br /><a href=\"\">close</a>" ), { title: "Saved!" } );
			} else {
				mw.notify( $( "Error: " + getAttribute('code')
				+ ' - ' + getAttribute('info') + '<br /><a href=\"/wiki/Uncyclopedia:QuickVFD\">Add manually</a> '
				+ 'or <br /><a href="javascript:qvfd.panel.hide()">close</a>' ), { title: "Saving failed :(" } );
			}
		}).fail(function(o) {
			mw.notify( $( "<a href=\"/wiki/Uncyclopedia:QuickVFD\">Go to QVFD</a> "
			+ "or <br /><a href=\"\">close</a>" ), { title: "Connection failed :(" } );
		});
	}).fail(function() {
		mw.notify( $( "Couldn\'t connect to QVFD page!<br />"
		+ "<a href=\"javascript:qvfd.panel.hide()\">Close</a>" ) );
	});
};

$('p-cactions', qvfd.addQVFD);