Jump to content

User:Zackmann08/scripts/RedirectTalk.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.
$( function() {
    // 10 is "Template:" and 828 is "Module:"
    if ( ( mw.config.get( "wgNamespaceNumber" ) === 11 || mw.config.get( "wgNamespaceNumber" ) === 829 ) && mw.config.get( "wgPageName" ).includes( "/sandbox" ) ) {
        var link = mw.util.addPortletLink(
            "p-cactions",
            "#",
            "Redirect to main",
            "redirect-main",
            "Redirects sandbox talk page to main talk page"
        );
        mw.loader.using("mediawiki.api", function () {
			link.addEventListener( "click", function () {
				mw.notify('Redirecting page to ' + mw.config.get( "wgPageName" ).replace(/\/sandbox$/, ''), {type: 'notice'});
				var api = new mw.Api();
				api.get({
					prop: 'revisions',
					rvprop: 'content',
					rvlimit: 1,
					titles: mw.config.get( "wgPageName" )
				}).done( function ( data ) {
					if ( !data.query || !data.query.pages ) return;
					api.postWithEditToken( {
						action: "edit",
						title: mw.config.get( "wgPageName" ),
						summary: 'redirecting to main talk page using [[User:Zackmann08/scripts/RedirectTalk.js]]',
						text: '#redirect [[' + mw.config.get( "wgPageName" ).replace(/\/sandbox$/, '') + ']]'
					}).done ( function ( data ) {
						if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
							mw.notify( "Page redirected! Reloading in 3 seconds", {type: 'success'} );
							window.setTimeout( function () {
                                window.location.href = mw.util.getUrl(mw.config.get( "wgPageName" ), { redirect: 'no' });
                            }, 3000 );
						}
					});
				});
			});
		});
    }
});