Topic on Project:Support desk

How to write good script

7
Murbaut (talkcontribs)
if(mw.config.get("wgRelevantUserName")){
	var username = mw.config.get("wgRelevantUserName");
	var link = mw.util.addPortletLink(
    	'p-tb',
    	'#',
    	'Diblok4',
    	'pt-diblok4link',
	 	'Add tag diblok4'
	);
}

$(link).click(function(){
	check = confirm("Do you want to give " + username + "tag?");
	if (check) {
		alertUser(username);
	}
});

function alertUser(username) {
	//If page already exists
	$.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User talk:' + username,
            summary: 'menambah tag {{diblok4}}',
            nocreate: 1,
            appendtext: '\n\n{{subst:' + 'Diblok4}}',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done(console.log( "Add tag diblok: " + username )
    ).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
    
    //If pages does not exist
    $.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User:' + username,
            title: 'User talk:' + username,
            summary: 'menambahkan tag {{diblok4}} ke pengguna: ' + username,
            create: 1,
            text: '{{subst:' + 'Diblok4}}',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done(console.log( "add tag diblok: " + username )
    ).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
}
Murbaut (talkcontribs)

how to fix two title " title : 'User' + username, title: 'User talk' + username, " What wrong?

MarkAHershberger (talkcontribs)

What are you trying to do? The two title keys in the second mean that only the second one ("User talk") is seen, which is the same as the place above.

Murbaut (talkcontribs)

@MarkAHershberger: But, Can I create warn in userpage and usertalk, can you provide to me, what is script code?

Ciencia Al Poder (talkcontribs)

Murbaut: You can't edit two (or more) pages in a single API request. You have to do separate api calls, one for user and other for user talk.

Murbaut (talkcontribs)
Ciencia Al Poder (talkcontribs)

Just duplicate the api call code...

Reply to "How to write good script"