User:Awesome Aasim/quicknote.js

//

if (!quicknote) {

var quicknote = {};

mw.loader.using("jquery.ui", function() {

mw.loader.load("https://en.wikipedia.org/wiki/User:Awesome_Aasim/dialog.css", "text/css");

// from: https://stackoverflow.com/questions/6411282/how-to-lock-scrolling-of-a-web-page-temporarily

quicknote.lockScrolling = function() {

// $('body').css({'overflow':'hidden'});

// $(document).bind('scroll',function () {

// window.scrollTo(0,0);

// });

}

quicknote.unlockScrolling = function() {

// $(document).unbind('scroll');

// $('body').css({'overflow':'visible'});

}

if (location.href.includes("diff=") || mw.config.get("wgAction") == "rollback") {

quicknote.pagelink = ":" + mw.config.get("wgPageName").replace(/_/g, " ") + "";

/*

quicknote.good = quicknote.messages.good[Math.floor(Math.random() * quicknote.messages.good.length)];

quicknote.bad = quicknote.messages.bad[Math.floor(Math.random() * quicknote.messages.bad.length)];

*/

$(document).ready(function() {

if (location.href.includes("diff=")) {

quicknote.prevuser = $(".diff").find(".mw-userlink").eq(0).text();

quicknote.curruser = $(".diff").find(".mw-userlink").eq(1).text();

} else {

quicknote.prevuser = $(".mw-userlink").eq(0).text();

quicknote.curruser = $(".mw-userlink").eq(1).text();

}

quicknote.prevuserlink = $(".mw-usertoollinks").eq(0);

quicknote.curruserlink = $(".mw-usertoollinks").eq(1);

quicknote.prevuserlink.html(quicknote.prevuserlink.html().substring(0, $(".mw-usertoollinks").eq(0).html().length-1)).append(" | ").append($("", {

href: location.href + "&quicknote=" + quicknote.curruser,

title: "Leave a quick note for " + quicknote.prevuser,

text: "quick note",

click: function(e) {

e.preventDefault();

history.replaceState({}, "Quick note - " + mw.config.get("wgSiteName"), location.href + "&quicknote=" + quicknote.prevuser);

quicknote.leavequicknote(quicknote.prevuser);

}

})).append(")");

quicknote.curruserlink.html(quicknote.curruserlink.html().substring(0, $(".mw-usertoollinks").eq(1).html().length-1)).append(" | ").append($("", {

href: location.href + "&quicknote=" + quicknote.curruser,

title: "Leave a quick note for " + quicknote.curruser,

text: "quick note",

click: function(e) {

e.preventDefault();

history.replaceState({}, "Quick note - " + mw.config.get("wgSiteName"), location.href + "&quicknote=" + quicknote.curruser);

quicknote.leavequicknote(quicknote.curruser);

}

})).append(")");

});

quicknote.leavequicknote = function(user) {

history.replaceState({}, mw.config.get("wgPageName").replace(/_/g, " ") + ": Difference between revisions - " + mw.config.get("wgSiteName"), location.href.replace("_", "%20").replace("&quicknote=" + quicknote.curruser.replace(" ", "%20"), ""));

quicknote.lockScrolling();

$('

').append(`

Leave a quick note for ` + user + `:

Your message will automatically be signed with four tildes (~~~~).

`).dialog({

buttons: [

{

/*class: "mw-ui-button mw-ui-progressive",*/

text: 'Post',

click: function() {

$("#quicknote-title").prop("disabled", true);

$("#quicknote-box").prop("disabled", true);

$(".ui-button").prop("disabled", true).addClass("ui-state-disabled");

//post to talk page

$.get(mw.config.get('wgScriptPath') + '/api.php', {

"action": "query",

"meta": "tokens",

"type": "csrf",

"format": "json"

}).done(function(token) {

if (token.error) {

mw.notify($("" + tokens.error.info + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

} else {

$.post(mw.config.get('wgScriptPath') + '/api.php', {

"action": "edit",

"summary": ($("#quicknote-title").val() ? "/*" + $("#quicknote-title").val() + "*/ " : "") + "Quick note",

"appendtext": "\n" + ($("#quicknote-title").val() ? "== " + $("#quicknote-title").val() + " ==\n" : "") + $("#quicknote-box").val() + "\n~~~~",

"title": "User_talk:" + user,

"format": "json",

"token": token.query.tokens.csrftoken

}).done(function(editresult) {

if (editresult.error) {

if (editresult.error.code.substring(0, "abusefilter".length) == "abusefilter") {

var that = editresult;

console.log(editresult.error.info.substring(1, editresult.error.info.length-1));

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"prop": "wikitext",

"format": "json",

"page": "MediaWiki:" + editresult.error.info.substring(1, editresult.error.info.length-1)

}).done(function(result) {

if (result.error) {

mw.notify(that.error.info);

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

} else {

console.log(result.parse.wikitext["*"]);

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"prop": "text",

"format": "json",

"title": mw.config.get("wgPageName"),

"pst": true,

"text": result.parse.wikitext["*"]

}).done(function(result) {

$("

The following message triggered an abuse filter:


The warning given was:" + result.parse.text["*"].replace("$1", that.error.abusefilter.description) + "


Are you sure you want to leave this message? Only choose \"Yes\" if you are sure the message does not violate policy. Select \"Go back\" to make appropriate corrections.

").dialog({

closeOnEscape: false,

modal: true,

title: "Abuse filter warning",

width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300,

buttons: [

{

text: "Yes",

click: function() {

$(this).dialog('destroy').remove();

$.post(mw.config.get('wgScriptPath') + '/api.php', {

"action": "edit",

"summary": ($("#quicknote-title").val() ? "/*" + $("#quicknote-title").val() + "*/ " : "") + "Quick note",

"appendtext": "\n" + ($("#quicknote-title").val() ? "== " + $("#quicknote-title").val() + " ==\n" : "") + $("#quicknote-box").val() + "\n~~~~",

"title": "User_talk:" + user,

"format": "json",

"token": token.query.tokens.csrftoken

}).done(function(editresult) {

if (editresult.error) {

mw.notify($("" + editresult.error.info + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

} else {

mw.notify("You have successfully left a message for " + user + ".");

$("#quicknote-dialog").dialog("destroy").remove();

}

}).fail(function(editresult) {

mw.notify($("" + editresult.error.info + ""));

});

}

},

{

text: "Go back",

click: function() {

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

$(this).dialog('destroy').remove();

}

}

]

});

}).fail(function(result) {

mw.notify($("" + that.error.info + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

})

}

}).fail(function(result) {

mw.notify($("" + that.error.info + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

});

} else {

mw.notify($("" + editresult.error.info + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

$("#quicknote-title").prop("disabled", false);

$("#quicknote-box").prop("disabled", false);

}

} else {

mw.notify("You have successfully left a message for " + user + ".");

$( "#quicknote-dialog" ).dialog( 'destroy' ).remove();

quicknote.unlockScrolling();

history.replaceState({}, mw.config.get("wgPageName").replace(/_/g, " ") + ": Difference between revisions - " + mw.config.get("wgSiteName"), location.href.replace("_", "%20").replace("&quicknote=" + quicknote.curruser.replace(" ", "%20"), ""));

}

}).fail(function(editresult) {

debugger;

mw.notify($("" + editresult + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

});

}

}).fail(function(token) {

debugger;

mw.notify($("" + token + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

});

}

},

{

/*class: "mw-ui-button",*/

text: 'Preview',

click: function() {

$(".ui-button").prop("disabled", true).addClass("ui-state-disabled");

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "text",

"text": "\n" + ($("#quicknote-title").val() ? "== " + $("#quicknote-title").val() + " ==\n" : "") + $("#quicknote-box").val() + "\n~~~~",

"title": "User_talk:" + user,

"pst": true

}).done(function(preview) {

if (preview.error) {

mw.notify($("" + preview.error.info + ""));

} else {

//$("#quicknote-preview").html(preview.parse.text["*"]);

$("

").html(preview.parse.text["*"]).dialog({

closeOnEscape: false,

modal: true,

title: "Quick note preview",

width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300

});

$(".quicknote-preview").find(".mw-editsection").css("display", "none");

$(".quicknote-preview").find("a").attr("target", "_blank");

}

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

}).fail(function(preview) {

debugger;

mw.notify($("" + preview + ""));

$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");

});

}

},

{

/*class: "mw-ui-button",*/

text: 'Cancel',

click: function() {

history.replaceState({}, mw.config.get("wgPageName").replace(/_/g, " ") + ": Difference between revisions - " + mw.config.get("wgSiteName"), location.href.replace("_", "%20").replace("&quicknote=" + quicknote.curruser.replace(" ", "%20"), ""));

quicknote.unlockScrolling();

$( this ).dialog( 'destroy' ).remove();

}

}

],

closeOnEscape: false,

modal: true,

title: "Quick note",

width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300

});

$(".ui-dialog-titlebar-close").hide();

/*

$(".ui-button").removeClass("ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only");

$(".ui-button, .ui-dialog-buttonset, .ui-dialog-buttonpane, .ui-widget-content, .ui-helper-clearfix").unbind('mouseenter mouseleave hover');

*/

mw.loader.load("https://en.wikipedia.org/wiki/User:Awesome Aasim/quicknote.js/messages.js?action=raw&ctype=text/javascript");

$('#quicknote-buttons').append("

You can use one of the buttons below to send a thank you message to the editor if they made a good edit or coach the editor if they made a mistake.

");

$("#quicknote-buttons").append($("").click(function(e) {

e.preventDefault();

$("#quicknote-title").val("Thank you!");

$("#quicknote-box").val(quicknote.good);

}));

$("#quicknote-buttons").append($("").click(function(e) {

e.preventDefault();

$("#quicknote-box").val($("#quicknote-box").val() + "\nIf this is a shared IP address, and you did not make the edit(s) mentioned, you can log in or create an account to avoid future irrelevant messages.");

}));

$("#quicknote-buttons").append($("").click(function(e) {

e.preventDefault();

$("#quicknote-title").val("Welcome!");

$("#quicknote-box").val(quicknote.welcome);

}));

$("#quicknote-buttons").append($("").click(function(e) {

e.preventDefault();

$("#quicknote-title").val("Your edit to " + quicknote.pagelink);

$("#quicknote-box").val(quicknote.bad + ((mw.config.get("wgContentLanguage") == "en" && mw.config.get("wgSiteName") == "Wikipedia") ? " " : ""));

}));

if (mw.config.get("wgContentLanguage") == "en" && mw.config.get("wgSiteName") == "Wikipedia") {

//Wikipedia editnotice

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "wikitext",

"page": "User_talk:" + user + "/Editnotice"

}).done(function(wikitext) {

if (!wikitext.error) {

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "text",

"title": "User_talk:" + user,

"pst": true,

"text": wikitext.parse.wikitext["*"]

}).done(function(text) {

if (!text.error && wikitext.parse.wikitext["*"] != "") {

$(text.parse.text["*"]).dialog({

buttons: [{

text: "OK",

click: function() {

$(this).dialog('destroy').remove();

}

}],

closeOnEscape: false,

modal: true,

title: "Talk Notice",

width: window.innerWidth,

height: window.innerHeight

});

}

});

}

});

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "wikitext",

"page": "Template:Editnotices/Page/User_talk:" + user

}).done(function(wikitext) {

if (!wikitext.error) {

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "text",

"title": "User_talk:" + user,

"pst": true,

"text": wikitext.parse.wikitext["*"]

}).done(function(text) {

if (!text.error && wikitext.parse.wikitext["*"] != "") {

$(text.parse.text["*"]).dialog({

buttons: [{

text: "OK",

click: function() {

$(this).dialog('destroy').remove();

}

}],

closeOnEscape: false,

modal: true,

title: "Edit Notice",

width: window.innerWidth,

height: window.innerHeight

});

}

});

}

});

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "wikitext",

"page": "Template:Editnotices/Group/User_talk:" + user

}).done(function(wikitext) {

if (!wikitext.error) {

$.get(mw.config.get("wgScriptPath") + "/api.php", {

"action": "parse",

"format": "json",

"prop": "text",

"title": "User_talk:" + user,

"pst": true,

"text": wikitext.parse.wikitext["*"]

}).done(function(text) {

if (!text.error && wikitext.parse.wikitext["*"] != "") {

$(text.parse.text["*"]).dialog({

buttons: [{

text: "OK",

click: function() {

$(this).dialog('destroy').remove();

}

}],

closeOnEscape: false,

modal: true,

title: "Group Notice",

width: window.innerWidth,

height: window.innerHeight

});

}

});

}

});

}

if (mw.config.get("wgUserName") == user.replace(/_/g, " ")) {

$('

"').dialog({

buttons: [{

text: "OK",

click: function() {

$(this).dialog('destroy').remove();

}

}],

closeOnEscape: false,

modal: true,

title: "Warning"

});

}

};

if ((new URL(document.location)).searchParams.get("quicknote")) {

quicknote.leavequicknote((new URL(document.location)).searchParams.get("quicknote"));

}

}

});

}

//