Jump to content

User:Suffusion of Yellow/viewsource.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 * Adds a "view source" option to editable pages.
 * Based on [[User:Misza13/viewSource.js]].
 */

// <nowiki>
$.when(mw.loader.using(['mediawiki.util']), $.ready).then(function () {
	var $editlink = $('#ca-edit a');

	if ($editlink.length == 1) {		
		mw.util.addPortletLink(
			'p-cactions',
			$editlink.attr('href').replace(/&(ve)?action=[^&]+/,
										   '&action=submit&viewsource=1'),
			'View source',
			'ca-viewsource-hack',
			'View the source of this page'
		);
	}

	if (mw.config.get('wgAction') == 'submit' &&
		location.href.indexOf('viewsource=1') !== -1) {

		$('#wpTextbox1').prop('readOnly', true);

		// Removing too much can cause other scripts to fail. Just hide it.
		mw.util.addCSS('div.editOptions, div.mw-editTools, div.wikiEditor-ui-top, form#editform div#editpage-specialchars { display: none; }');

		document.title = document.title.replace(/^Editing/, 'View source for');
		$('#firstHeading').text($('#firstHeading').text().replace(/^Editing/, 'View source for'));

		$('#ca-viewsource-hack').addClass('selected');
		$('#ca-edit').removeClass('selected');
	}
});
// </nowiki>