Jump to content

User:Awesome Aasim/quicknote.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
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($("<a/>", {
					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($("<a/>", {
					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();
				$('<div id="quicknote-dialog"></div>').append(`<form id="quicknote-form"><p>Leave a quick note for ` + user + `:</p>
					<input id="quicknote-title" type="text" placeholder="Subject" value="Quick note on ` + quicknote.pagelink +  `">
					<textarea id="quicknote-box" placeholder="Message" rows="6">Thanks for your edits to ` + quicknote.pagelink + `.</textarea>
					<p>Your message will automatically be signed with four tildes (~~~~).</p>
					<span id="quicknote-buttons"></div>
				</form>`).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($("<span class=\"error\">" + tokens.error.info + "</span>"));
										$(".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() + "*/ " : "") + "[[User:Awesome Aasim/quicknote|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) {
																$("<div><p>The following message triggered an abuse filter: <textarea disabled rows=\"6\">" + "\n" + ($("#quicknote-title").val() ? "== " + $("#quicknote-title").val() + " ==\n" : "") + $("#quicknote-box").val() + "\n~~~~" + "</textarea></p><hr><p>The warning given was:" + result.parse.text["*"].replace("$1", that.error.abusefilter.description) + "</p><hr><p>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.</p></div>").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() + "*/ " : "") + "[[User:Awesome Aasim/quicknote|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($("<span class=\"error\">" + editresult.error.info + "</span>"));
																						$(".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($("<span class=\"error\">" + editresult.error.info + "</span>"));
																				});
																			}
																		},
																		{
																			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($("<span class=\"error\">" + that.error.info + "</span>"));
																$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
																$("#quicknote-title").prop("disabled", false);
																$("#quicknote-box").prop("disabled", false);
															})
														}
													}).fail(function(result) {
														mw.notify($("<span class=\"error\">" + that.error.info + "</span>"));
														$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
														$("#quicknote-title").prop("disabled", false);
														$("#quicknote-box").prop("disabled", false);
													});
												} else {
													mw.notify($("<span class=\"error\">" + editresult.error.info + "</span>"));
													$(".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($("<span class=\"error\">" + editresult + "</span>"));
											$(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
										});
									}
								}).fail(function(token) {
									debugger;
									mw.notify($("<span class=\"error\">" + token + "</span>"));
									$(".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($("<span class=\"error\">" + preview.error.info + "</span>"));
									} else {
										//$("#quicknote-preview").html(preview.parse.text["*"]);
										$("<div class=\"quicknote-preview\"></div>").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($("<span class=\"error\">" + preview + "</span>"));
									$(".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("<p>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.</p>");
				$("#quicknote-buttons").append($("<button id=\"quicknote-good\" class=\"mw-ui-button\">Basic thanks</button>").click(function(e) {
					e.preventDefault();
					$("#quicknote-title").val("Thank you!");
					$("#quicknote-box").val(quicknote.good);
				}));
				$("#quicknote-buttons").append($("<button id=\"quicknote-sharedip\" class=\"mw-ui-button\">Add shared IP advice</button>").click(function(e) {
					e.preventDefault();
					$("#quicknote-box").val($("#quicknote-box").val() + "\n''If this is a shared IP address, and you did not make the edit(s) mentioned, you can [[Special:UserLogin|log in]] or [[Special:UserLogin/signup|create an account]] to avoid future irrelevant messages.''");
				}));
				$("#quicknote-buttons").append($("<button id=\"quicknote-welcome\" class=\"mw-ui-button\">Warm welcome</button>").click(function(e) {
					e.preventDefault();
					$("#quicknote-title").val("Welcome!");
					$("#quicknote-box").val(quicknote.welcome);
				}));
				$("#quicknote-buttons").append($("<button id=\"quicknote-bad\" class=\"mw-ui-button\">Coach for bad</button>").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") ? "<!--Template:uw-quicknote--> <!--Template:uw-test1--> <!--Template:uw-cluebotwarning1-->" : ""));
				}));
				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, " ")) {
					$('<table class="plainlinks plainlinks ombox ombox-content" role="presentation"><tbody><tr><td class="mbox-image"><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/40px-OOjs_UI_icon_alert-warning.svg.png" decoding="async" width="40" height="40" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/60px-OOjs_UI_icon_alert-warning.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/3b/OOjs_UI_icon_alert-warning.svg/80px-OOjs_UI_icon_alert-warning.svg.png 2x" data-file-width="20" data-file-height="20"></td><td class="mbox-text"><b>Warning:</b><br><p>You are about to post to your own talk page. Please verify that you actually want to do this.</p></td></tr></tbody></table>"').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"));
			}
		}
	});
}
//</nowiki>