Jump to content

User:Eumat114/deCOInote.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.
//Copied from [[User:DannyS712/TeahouseTB.js]]
//<nowiki>
$(function() {
	var DS_config = {
	name: '[[User:Eumat114/deCOInote.js|deCOI note]]',
	version: 1.0,
	debug: false
	};
	var DS_summary = 'Notify COI with ' + DS_config.name + ' (version ' + DS_config.version + ')';
	var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
    var pageName = mw.config.get('wgPageName');
    api = new mw.Api();
    mw.loader.using( ['mediawiki.util'], function() {
        if(mw.config.get("wgCanonicalNamespace") == "Draft") {
            mw.util.addPortletLink('p-cactions', 'javascript:void(0)', 'deCOI note', 'aca-decoinote', 'Notify the user of a misplaced COI notice');
            $('#aca-decoinote').on('click', function() {
            	var targetUN = prompt( "Username?                              ","");
                var coinprompt = prompt("COI Notice:                              ","");
                if (targetUN !== null) {
                    templateUser(targetUN, coinprompt);
                }
            });
        }
    });
    function get_page( name ){
    var page_to_get = {
        action: 'query',
        titles: name,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			if (DS_config.debug) console.log( page );
	    	result = page.query.pages["0"].revisions["0"].content;
	    	if (DS_config.debug) console.log( result );
		} 
	});
	return result;
	}
	function set_new ( page, new_content ){
	console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        summary: DS_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() {} );
	}
    function templateUser(targetUN,coinprompt) {
    	var userpage = get_page ( "User:"+targetUN );
    	if (userpage === null) {userpage="";}
    	set_new("User:"+targetUN,coinprompt+userpage);
        api.postWithToken( "edit", {
            action: "edit",
            section: "new",
            sectiontitle: "Note for misplaced COI declaration",
            summary: "Notifying user ([[User:Eumat114/deCOInote|deCOInote]])",
            text: "{{subst:User:Eumat114/bad COI|" + pageName + "}}",
            title: "User talk:"+targetUN
        }).then(function(editData) {
            alert("Successfully notified user");
        },function(error) {
            alert("An error occurred. Please try again.");
        });
    }
});
//</nowiki>