User:RamDevLoveper/common.js

varnullbody_tag= document.getElementsByTagName("body")[0] body_tag.setAttribute("onmouseup", "MouseUP")

function MouseUP{ console.log("Checking event"); var wiki_domain = "wikipedia.org"; console.log(wiki_domain); console.log("Getting into the functions"); if(window.getSelection.toString.length){ var exactText = window.getSelection.toString; var replace_text = prompt(exactText); console.log("Replace text") console.log(replace_text); var content = window.getSelection.getRangeAt(0); var content_string = content.startContainer.nodeValue; var content_edited_string = content_string.substring(0, content.startOffset) + replace_text + content_string.substring(content.endOffset); console.log(content_edited_string) get_csrf(content_string, content_edited_string); } }

function get_title(csrf_token, content_string, content_edited_string){ var current_url = window.location.href; console.log(current_url); var url_list = current_url.split("/wiki/"); console.log(url_list); if (url_list.length > 1){ var wiki_index = url_list.indexOf('wiki') var title = url_list[1]; console.log(title); var domain = url_list[0]; var wiki_domain = "wikipedia.org"; console.log(domain); console.log(wiki_domain); console.log(domain.includes(wiki_domain)) if (domain.includes(wiki_domain)){ edit_request(csrf_token, title, content_string, content_edited_string) }	} }

function get_csrf(content_string, content_edited_string){ var params = { action: 'query', meta: 'tokens', type: 'csrf', format: 'json' },	api = new mw.Api;

api.get( params ).done( function ( data ) {		console.log("CSRF TOKEN");		console.log(data.query.tokens.csrftoken);		get_title(data.query.tokens.csrftoken, content_string, content_edited_string);	} ); }

function edit_request(csrf_token, title, content_string, content_edited_string){ console.log("Enters into edit_request function"); console.log(csrf_token); console.log(title); new mw.Api .edit( title, function ( revision ) {       return revision.content.replace( content_string, content_edited_string );    } ) .then( function {        console.log( 'Saved!' );    } );

//  api = new mw.Api; //  params = { //      action: "edit", //      title: title, //      appendtext: content_edited_string, //      token: csrf_token, //      format: "json" //  }; //   console.log(params); // api.post( params ).done( function ( data ) {	// 	console.log("Done")	// } ); }