User:Ajithkumarpalani/common.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | The accompanying .css page for this skin is at User:Ajithkumarpalani/common.css. |
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 = "/media/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);
console.log("content",content)
var content_string = content.startContainer.nodeValue;
console.log("content_string",content_string)
var content_edited_string = content_string.substring(0, content.startOffset) + replace_text + content_string.substring(content.endOffset);
console.log("content_edited_string",content_edited_string);
debugger
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;
console.log("before_content", before_content)
console.log("content_edited_string", 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();
} );
}