User:Rusty Cat/Scripts/sectionRemover.js

// <nowiki>
// Originally by User:Flooded with them hundreds
// Copied to [[User:DannyS712/SectionRemover.js]] after FWTH retired, who took over as maintainer
// Fork by Rusty4321 to [[User:Rusty Cat/Scripts/sectionRemover.js]]
// {{subst:iusc|User:Rusty Cat/Scripts/sectionRemover.js}}
// or with
// importScript( 'User:Rusty Cat/Scripts/sectionRemover.js' ); // Backlink: [[User:Rusty Cat/Scripts/sectionRemover.js]]
// If forking this script, please give all authors of use credit
$.when( mw.loader.using(['mediawiki.util','mediawiki.api']), $.ready).done( function () {
	var validSections = {},
		fromTos = {},
		wikiText = '',
		revStamp, startRemoveButton, overlay;

	if ( mw.config.get( 'wgNamespaceNumber' ) == -1 ) {
		// is a special page
		return;
	}
	$( 'head' ).append(
		'<style>a.arxylink { font-weight:bold } .arxyhighlight { background-color:#b3ffcc }</style>'
	);

	startRemoveButton = mw.util.addPortletLink(
		'p-cactions',
		'#',
		'Remove section',
		'pt-oeca',
		'Enter/exit the removal process',
		null,
		null
	);
	overlay = $( document.createElement( 'button' ) );
	$( startRemoveButton ).click( function ( e ) {
		$( '.arxylink' ).click();
		$( '.arxy' ).toggle();
		$( '#Removebutton' ).toggle();
	} );
	overlay.html( 'remove' )
		.attr( 'id', 'Removebutton' )
		.css( 'position', 'fixed' )
		.css( 'bottom', '20px' )
		.css( 'height', '50px' )
		.css( 'width', '100%' )
		.css( 'font-size', '200%' );
	$( document.body ).append( overlay );
	overlay.toggle();
	overlay.click( function ( e ) {
		var numOfThreads, archiveTarget, sections, archiveThis, cutOffset,
			revisedPage;
		function cut( s, start, end ) {
			return s.substr( 0, start ) + s.substring( end );
		}
		cutOffset = numOfThreads = 0;
		revisedPage = wikiText;
		sections = $( 'a.arxylink' ).map( function () {
			return $( this ).attr( 'data-section' );
		} );
		if ( !( numOfThreads = sections.length ) ) {
			return alert( 'No sections selected, aborting' );
		}
		sections.each( function ( i, n ) {
			revisedPage = cut(
				revisedPage,
				fromTos[ n ][ 0 ] - cutOffset,
				fromTos[ n ][ 1 ] - cutOffset
			);
			cutOffset += fromTos[ n ][ 1 ] - fromTos[ n ][ 0 ];
		} );
		archiveThis = sections.map( function () {
			return wikiText.substring( fromTos[ this ][ 0 ], fromTos[ this ][ 1 ] );
		} ).toArray().join( '' );
		console.log( 'archive this:' + archiveThis );
		console.log( 'revised page:' + revisedPage );
		if ( 1 ) {
			new mw.Api().postWithToken(
				'csrf',
				{
					action: 'edit',
					title: mw.config.get( 'wgPageName' ),
					text: revisedPage,
					summary: 'Removed sections using [[User:DannyS712/SectionRemover|SectionRemover]]',
					basetimestamp: revStamp,
					starttimestamp: revStamp
				}
			).done( function ( res1 ) {
				alert( 'Successfully removed sections' );
				console.log( res1 );
			} ).fail( function ( res1 ) {
					alert( 'failed to remove sections. aborting.' );
					console.log( res1 );
					window.location.reload();
				} );
		}
	} );
	new mw.Api().get( {
		action: 'parse',
		page: mw.config.get( 'wgPageName' )
	} ).done( function ( dataShit ) {
			var i;
			new mw.Api().get( {
				action: 'query',
				pageids: mw.config.get( 'wgArticleId' ),
				prop: [ 'revisions' ],
				rvprop: [ 'content', 'timestamp' ]
			} ).done( function ( shit ) {
				var rv;
				rv = shit.query.pages[ mw.config.get( 'wgArticleId' ) ].revisions[ 0 ];
				wikiText = rv[ '*' ];
				revStamp = rv.timestamp;
			} );
			$( dataShit.parse.sections )
				.filter( function ( i, s ) { return s.index == parseInt( s.index ) } )
				.each( function ( i, s ) { validSections[ s.index ] = s } );
			for ( i in validSections ) {
				i = parseInt( i );
				fromTos[ i ] = [
					validSections[ i ].byteoffset,
					validSections.hasOwnProperty( i + 1 ) ? validSections[ i + 1 ].byteoffset : Infinity
				];
			}
			$( '#mw-content-text' )
				.find( ':header' )
				.find( 'span.mw-headline' )
				.each( function ( i, title ) {
					var header, editSection, sectionNumber;
					header = $( this ).parent();
					editSection = header.find( '.mw-editsection' ); // 1st child
					sectionNumber = header.find( '.mw-editsection a:first' );
					if ( sectionNumber[ 0 ] ) {
						sectionNumber = sectionNumber.attr( 'href' ).match( /&section=(\d+)/ );
						if ( sectionNumber ) {
							sectionNumber = sectionNumber[ 1 ];
						} else {
							// eg <h2>not a real section</h2>
							sectionNumber = undefined;
						}
					}
					if ( validSections.hasOwnProperty( sectionNumber ) ) {
						editSection[ 0 ].innerHTML += '&nbsp;'
							+ '<span class=arxy style=display:none>'
							+ '<span class=mw-editsection-bracket>[</span>'
							+ '<a data-section=' + sectionNumber + ' '
							+ 'onclick=$(this).closest(\':header\').toggleClass(\'arxyhighlight\');$(this).toggleClass(\'arxylink\');>'
							+ 'remove section</a>'
							+ '<span class=mw-editsection-bracket>]</span>'
							+ '</span>';
					}
				} );
		} );
} );
// </nowiki>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.