User:Deepakragavan/common.js

console.log("Initializing event"); var body_tag = document.getElementsByTagName("body")[0] var div_element = document.createElement("div"); div_element.setAttribute("id","loader"); body_tag.appendChild(div_element);

var float_button = document.createElement("div"); float_button.setAttribute("class", "float"); float_button.setAttribute("onclick", "MouseUP"); var i_button = document.createElement("img"); i_button.src = "https://upload.wikimedia.org/wikipedia/commons/9/96/Antu_scribus.svg"; float_button.appendChild(i_button); body_tag.appendChild(float_button);

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); if (replace_text != null){ console.log("Replace text") console.log(replace_text); document.getElementById("loader").style.display = "block"; 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(exactText, content.startOffset, replace_text); }	} }

function get_title(csrf_token, exactString, startOffset, replace_text){ 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]; //var title = document.getElementById("firstHeading").innerHTML; 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)){ get_page_content(csrf_token, title, exactString, startOffset, replace_text) }	} }

function get_page_content(csrf_token, title, exactString, startOffset, replace_text){ var params = { action: 'query', titles: title, prop: 'revisions', formatversion: 2, rvprop: 'content', rvslots: '*', token: csrf_token, format: 'json' },	api = new mw.Api;

api.get( params ).done( function ( data ) {		console.log("page content");		console.log(data);		console.log(data.query.pages[0].revisions[0].slots.main.content);		var content = data.query.pages[0].revisions[0].slots.main.content;		console.log("StartOFF");		console.log(startOffset);		var before_content = content.substring(0, startOffset);		var content_edited_string = content.substring(startOffset);		content_edited_string = content_edited_string.replace(" "+exactString+" ", " "+replace_text+" ");		complete_content = before_content + content_edited_string;		edit_request(csrf_token, title, complete_content);	} ); }

function get_csrf(exactString, startOffset, replace_text){ 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, exactString, startOffset, replace_text);	} ); }

function edit_request(csrf_token, title, content_edited_string){ console.log("Enters into edit_request function"); console.log(csrf_token); console.log(title); api = new mw.Api; params = { action: "edit", title: title, text: content_edited_string, token: csrf_token, format: "json" };   console.log(params); api.post( params ).done( function ( data ) {		document.getElementById("loader").style.display = "none";		console.log("Done");		window.location.reload;	} ); }