Jump to content

User:DannyS712 test/MEA clerk bot.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.
//<nowiki>
$(function (){
var MEA_bot_config = {
	name: '[[User:DannyS712/Missing encyclopedic articles bot|Missing encyclopedic articles bot]]',
	testing: true,
	disclaimer: "Bot in trial ([[Wikipedia:Bots/Requests for approval/DannyS712 bot 50|BRFA]])",
	version: "1.1.1",
	debug: false
};
var MEA_bot_summary = 'Task 50: Clerking with ' + MEA_bot_config.name + ' (version ' + MEA_bot_config.version + ')';
if (MEA_bot_config.testing){
	MEA_bot_summary = MEA_bot_config.disclaimer + ": " + MEA_bot_summary;
}

var all_blue_links = [];

$.when(
	mw.loader.using( 'mediawiki.util'),
	mw.loader.getScript( '/w/index.php?title=User:DannyS712 test/page.js&action=raw&ctype=text/javascript' )
).then( function () {
    $(document).ready( function () {
    	if (  mw.config.get('wgPageName').indexOf( 'Wikipedia:WikiProject_Missing_encyclopedic_articles/') > -1 || mw.config.get('wgPageName').indexOf( 'User:DannyS712 test/') > -1 ){
    		$('.mw-redirect').css( {"border": "5px solid yellow", "color": "#006600" } );
    		mw.util.addPortletLink ( 'p-tb', 'javascript:void(0)', 'Missing encyclopedic articles', 'ca-MEA-clerk', 'TOOLTIP');
	    	$('#ca-MEA-clerk').on('click', function() {
	        	MEA();
			} );
	    }
    } );
} );
function get_links(){
	var blue_links = [];
	var link_class = "";
	var a = $('a').filter( function() {
		return !(this.className.indexOf('mw-redirect') > -1 ||
				this.className.indexOf('new') > -1 ||
				this.className.indexOf('mw-disambig') > -1 ||
				this.className.indexOf('mw-echo') > -1 ||
				this.className.indexOf('mw-wiki') > -1 ||
				this.className.indexOf('external text') > -1 ||
				this.className.indexOf('mw-jump-link') > -1 ||
				this.title.indexOf(':') > -1 ||
				this.id === 'top');
	} );
	console.log( a );
	$(a).each( function( index, value ) {
		blue_links.push( value.text);
	});

	console.log( blue_links );
	all_blue_links = blue_links;
}
function MEA(){
	get_links();
	console.log( all_blue_links );
	
	var page_name = mw.config.get( 'wgPageName' );
	var page_content = get_page( page_name );
	console.log( page_content );
	
	var new_content = page_content;
	
	if (page_name.indexOf('Wikipedia:WikiProject_Missing_encyclopedic_articles/List_of_US_Newspapers/') > -1 ) {
		new_content = page_content.replace(/#\s*\[\[(.*?)\]\]\s*\[\[(?:.*?)\]\].*\n?/g, check_line );
	} else if (page_name === 'Wikipedia:WikiProject_Missing_encyclopedic_articles/Dsp13_List/24' ) {
		new_content = page_content.replace(/#\s*\[\[(.*?)\]\]\s*\|\s*\[(?:.*?) DAH\]\s*\|\n?/g, check_line );
	} else if (page_name === 'Wikipedia:WikiProject_Missing_encyclopedic_articles/Dsp13_List/23' ||
				page_name === 'Wikipedia:WikiProject_Missing_encyclopedic_articles/Dsp13_List/25' ||
				page_name === 'Wikipedia:WikiProject_Missing_encyclopedic_articles/Dsp13_List/26' ||
				page_name === 'Wikipedia:WikiProject_Missing_encyclopedic_articles/Dsp13_List/22' ||
				/^Wikipedia:WikiProject_Missing_encyclopedic_articles\/Hotlist_of_/.test( page_name ) ) {
		new_content = page_content.replace(/#\s*\[\[(.*?)\]\].*\n?/g, check_line );
	}
	if (page_content === new_content){
		console.log( "No change, skip");
	} else{ 
		console.log( "Will edit, setting new content to:", new_content );
		if ( mw.config.get('wgUserName') === 'DannyS712 bot') {
			set_new( page_name, new_content );
		}
	}
}
function check_line ( match, p1, offset, string ){
	//console.log( match, p1 );
	for (var iii = 0; iii < all_blue_links.length; iii++){
		if (p1 === all_blue_links[iii]){
			console.log( match, p1 );
			return '';
		}
	}
	return match;
}
function set_new ( page, new_content ){
	if( MEA_bot_config.debug) console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
		//pageid: mw.config.get( 'wgArticleId' ),
        text: new_content,
        notminor: true,
        bot: true,
        summary: MEA_bot_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    console.log( to_send );
    
    $.when(
        $.post( scriptUrl, to_send, function(){ } )
    ).done( function() {
        location.reload();
    } );
}
});
//</nowiki>