Jump to content

User:DannyS712 test/filters.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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/Filter links.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/Filter links.js' ); // Backlink: [[User:DannyS712/Filter links.js]] 				</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function() {
var FL_config = {
	name: '[[User:DannyS712/Filter links|Filter links.js]]',
	version: 1.2,
	debug: true
};
mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	var page = mw.config.get('wgPageName');
    	if (page && page === 'Special:AbuseLog'){
    		FilterLinks();
    	}
    } );
} );
function FilterLinks(){
	var filters = get_filters(1);
	var entries = $('li');
	var description, filter_id;
	if (FL_config.debug) console.log( entries );
	entries.each( function() {
		if (this.innerHTML.indexOf('triggered an edit filter') > -1){
			if (FL_config.debug) console.log( this.innerText );
			description = this.innerText.replace(/.*?Filter description: /, '');
			description = description.replace(/ \(diff\)/, '');
			if (FL_config.debug) console.log( description );
			for (var iii = 0; iii < filters.length; iii++){
				if (description === filters[iii].description){
					filter_id = filters[iii].id;
					if (FL_config.debug) console.log( filters[iii]);
					this.innerHTML = this.innerHTML.replace(/triggered an edit filter/, 'triggered <a href="/wiki/Special:AbuseFilter/' + filter_id + '" title="Special:AbuseFilter/' + filter_id + '">private filter ' + filter_id + '</a>');
				}
			}
		}
	});
}
function get_filters( start ){
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
	var getter = {
        action: 'query',
        list: 'abusefilters',
        abfshow: 'private',
        abflimit: 'max',
        abfprop: 'id|description',
        abfstartid: start,
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: getter,
		dataType: 'json',
		async: false,
		success: function( response ) {
			if (FL_config.debug) console.log( response );
			result = response.query.abusefilters;
			if (response.continue){
				result = result.concat( get_filters( response.continue.abfstartid) );
			}
			if (FL_config.debug) console.log( result );
		} 
	});
	return result;
}
});
//</nowiki>