Jump to content

User:Awesome Aasim/newsection.js

From Wikipedia, the free encyclopedia

This is the current revision of this page, as edited by Awesome Aasim (talk | contribs) at 23:27, 11 April 2023. The present address (URL) is a permanent link to this version.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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>
/**
 * This script is preserved for historical purposes today.
 * Instead, please use discussion tool's "new section" feature.
 **/
if (typeof NS == "undefined") {
	var NS = {};
	NS.run = function() {
		if (document.getElementById("ca-addsection") && mw.config.get("wgAction") == "view") { //run script only if page is editable
			mw.loader.using( 'oojs-ui-core', function () {
				NS.sectiontitle = new OO.ui.TextInputWidget({
					autosize: true,
					placeholder: "Section title"
				});
				NS.sectiontext = new OO.ui.MultilineTextInputWidget( {
					autosize: true,
					rows: 5,
					placeholder: "Add your comment..."
				} );
				NS.buttons = [
					new OO.ui.ButtonWidget( { 
						label: 'Post',
						flags: ['primary', 'progressive']
					} ),
					new OO.ui.ButtonWidget( { 
						label: 'Preview'
					} )
				];
				NS.preview = new OO.ui.LabelWidget();
				NS.preview.toggle(false);
				NS.error = new OO.ui.MessageWidget( {
					type: 'error'
				} );
				NS.error.toggle(false);
				NS.sectiontitle.setElementId("newsection-header");
				NS.sectiontext.setElementId("newsection-body");
				NS.buttons[0].setElementId("newsection-post");
				NS.buttons[1].setElementId("newsection-preview");
				NS.fieldset = new OO.ui.FieldsetLayout({label: "New section"});
				NS.fieldset.addItems([
					NS.sectiontitle,
					NS.sectiontext,
					NS.buttons[0],
					NS.buttons[1],
					new OO.ui.LabelWidget({
						label: $("<p>Your comment will automatically be signed with four tildes (~~~~).</p>")
					}),
					new OO.ui.FieldLayout(NS.preview),
					new OO.ui.FieldLayout(NS.error)
				]);
				NS.form = new OO.ui.FormLayout({
					autosize: true,
					items: [NS.fieldset]
				});
				NS.form.setElementId("newsection-form");
				$("<hr><div id=\"newsection\" style=\"border: thin dashed gray; padding: 0.5em; margin-top: 0.5em\"></div>").append(NS.form.$element).appendTo($("#mw-content-text").parent());
				NS.buttons[0].$element.click(function(e) {
					e.preventDefault();
					if (NS.sectiontext.getValue().length != 0) {
						NS.sectiontitle.setDisabled(true);
						NS.sectiontext.setDisabled(true);
						NS.buttons[0].setDisabled(true);
						NS.buttons[1].setDisabled(true);
						$.get(mw.config.get("wgScriptPath") + "/api.php", {
							"action": "query",
							"meta": "tokens",
							"type": "csrf",
							"format": "json"
						}).done(function(result) {
							if (result.error) {
								NS.error.toggle(true);
								if (!NS.error) {
									NS.error = new OO.ui.MessageWidget( {
										type: 'error',
										label: result.error.info
									} );
									NS.form.addItems(NS.error);
								} else {
									NS.error.setLabel(result.error.info);
								}
								NS.sectiontitle.setDisabled(false);
								NS.sectiontext.setDisabled(false);
								NS.buttons[0].setDisabled(false);
								NS.buttons[1].setDisabled(false);
							} else {
								$.post(mw.config.get("wgScriptPath") + "/api.php", {
									"action": "edit",
									"section": "new",
									"title": mw.config.get("wgPageName"),
									"sectiontitle": NS.sectiontitle.getValue(),
									"text": NS.sectiontext.getValue() + " ~~~~",
									"format": "json",
									"token": result.query.tokens.csrftoken
								}).done(function(result) {
									if (result.error) {
										NS.error.toggle(true);
										console.log(result.error.code.substring(0, "abusefilter".length));
										console.log(result.error.code);
										if (result.error.code.substring(0, "abusefilter".length) == "abusefilter") {
											var that = result;
											console.log(result.error.info.substring(1, result.error.info.length-1));
											$.get(mw.config.get("wgScriptPath") + "/api.php", {
												"action": "parse",
												"prop": "wikitext",
												"format": "json",
												"page": "MediaWiki:" + result.error.info.substring(1, result.error.info.length-1)
											}).done(function(result) {
												if (result.error) {
													NS.error.setLabel(that.error.info);
												} 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) {
														console.log(that.error.abusefilter.description);
														console.log(result.parse.text["*"]);
														NS.error.setLabel($(result.parse.text["*"].replace("$1", that.error.abusefilter.description)));
													}).fail(function(result) {
														NS.error.setLabel(that.error.info);
													})
												}
											}).fail(function(result) {
												NS.error.setLabel(that.error.info);
											});
											NS.form.addItems(NS.error);
										} else {
											NS.error.setLabel(result.error.info);
										}
										NS.sectiontitle.setDisabled(false);
										NS.sectiontext.setDisabled(false);
										NS.buttons[0].setDisabled(false);
										NS.buttons[1].setDisabled(false);
									} else {
										$.get(mw.config.get("wgScriptPath") + "/api.php", {
											"action": "parse",
											"prop": "text",
											"format": "json",
											"title": mw.config.get("wgPageName"),
											"text": (NS.sectiontitle.getValue() ? ('== ' + NS.sectiontitle.getValue() + ' ==\n') : "\n") + NS.sectiontext.getValue() + " ~~~~",
											"pst": true
										}).done(function(result) {
											if (result.error) {
												location.reload();
											} else {
												$("#mw-content-text").append(result.parse.text["*"]);
												NS.sectiontitle.setValue("");
												NS.sectiontext.setValue("");
											}
										});
										NS.preview.toggle(false);
										NS.error.toggle(false);
									}
									NS.sectiontitle.setDisabled(false);
									NS.sectiontext.setDisabled(false);
									NS.buttons[0].setDisabled(false);
									NS.buttons[1].setDisabled(false);
								});
							}
						});
					}
				});
				NS.buttons[1].$element.click(function(e) {
					e.preventDefault();
					if (NS.sectiontext.getValue().length != 0) {
						NS.sectiontitle.setDisabled(true);
						NS.sectiontext.setDisabled(true);
						NS.buttons[0].setDisabled(true);
						NS.buttons[1].setDisabled(true);
						$.get(mw.config.get("wgScriptPath") + "/api.php", {
							"action": "parse",
							"prop": "text",
							"format": "json",
							"title": mw.config.get("wgPageName"),
							"text": (NS.sectiontitle.getValue() ? ('== ' + NS.sectiontitle.getValue() + ' ==\n') : "\n") + NS.sectiontext.getValue() + " ~~~~",
							"pst": true
						}).done(function(result) {
							if (result.error) {
								NS.error.toggle(true);
								if (!NS.error) {
									NS.error = new OO.ui.MessageWidget( {
										type: 'error',
										label: result.error.info
									} );
									NS.form.addItems(NS.error);
								} else {
									NS.error.setLabel(result.error.info);
								}
							} else {
								NS.preview.setLabel($(result.parse.text["*"]));
								NS.preview.toggle(true);
								NS.error.toggle(false);
							}
							NS.sectiontitle.setDisabled(false);
							NS.sectiontext.setDisabled(false);
							NS.buttons[0].setDisabled(false);
							NS.buttons[1].setDisabled(false);
						}).fail(function(result) {
							NS.error.toggle(true);
							if (!NS.error) {
								NS.error = new OO.ui.MessageWidget( {
									type: 'error',
									label: "Preview error"
								} );
								NS.form.addItems(NS.error);
							} else {
								NS.error.setLabel(result.error.info);
							}
						});
					}
				});
				NS.sectiontext.$element.keyup(function(e) {
					if (NS.sectiontext.getValue()) {
						NS.buttons[0].setDisabled(false);
						NS.buttons[1].setDisabled(false);
					} else {
						NS.buttons[0].setDisabled(true);
						NS.buttons[1].setDisabled(true);
					}
				});
				NS.buttons[0].setDisabled(true);
				NS.buttons[1].setDisabled(true);
				$("#ca-addsection").find("a").attr("href", "#newsection");
			});
			if (mw.config.get("wgContentLanguage") == "en" && mw.config.get("wgSiteName") == "Wikipedia") {
				//Wikipedia editnotice
				$(document).ready(function() {
					if (mw.config.get("wgNamespaceNumber") == 3) {
						$.get(mw.config.get("wgScriptPath") + "/api.php", {
							"action": "parse",
							"format": "json",
							"prop": "wikitext",
							"page": mw.config.get("wgPageName") + "/Editnotice"
						}).done(function(wikitext) {
							if (!wikitext.error) {
								$.get(mw.config.get("wgScriptPath") + "/api.php", {
									"action": "parse",
									"format": "json",
									"prop": "text",
									"title": mw.config.get("wgPageName"),
									"pst": true,
									"text": wikitext.parse.wikitext["*"]
								}).done(function(text) {
									if (!text.error && wikitext.parse.wikitext["*"] != "") {
										$(text.parse.text["*"]).prependTo("#newsection");
										$("#newsection").find(".mw-collapsible").removeClass("mw-collapsible").removeClass("mw-collapsed");
									}
								});
							}
						});
					}
					$.get(mw.config.get("wgScriptPath") + "/api.php", {
						"action": "parse",
						"format": "json",
						"prop": "wikitext",
						"page": "Template:Editnotices/Page/" + mw.config.get("wgPageName")
					}).done(function(wikitext) {
						if (!wikitext.error) {
							$.get(mw.config.get("wgScriptPath") + "/api.php", {
								"action": "parse",
								"format": "json",
								"prop": "text",
								"title": mw.config.get("wgPageName"),
								"pst": true,
								"text": wikitext.parse.wikitext["*"]
							}).done(function(text) {
								if (!text.error && wikitext.parse.wikitext["*"] != "") {
									$(text.parse.text["*"]).prependTo("#newsection");
									$("#newsection").find(".mw-collapsible").removeClass("mw-collapsible").removeClass("mw-collapsed");
								}
							});
						}
					});
					$.get(mw.config.get("wgScriptPath") + "/api.php", {
						"action": "parse",
						"format": "json",
						"prop": "wikitext",
						"page": "Template:Editnotices/Group/" + mw.config.get("wgPageName").split("/")[0]
					}).done(function(wikitext) {
						if (!wikitext.error) {
							$.get(mw.config.get("wgScriptPath") + "/api.php", {
								"action": "parse",
								"format": "json",
								"prop": "text",
								"title": mw.config.get("wgPageName"),
								"pst": true,
								"text": wikitext.parse.wikitext["*"]
							}).done(function(text) {
								if (!text.error && wikitext.parse.wikitext["*"] != "") {
									$(text.parse.text["*"]).prependTo("#newsection");
									$("#newsection").find(".mw-collapsible").removeClass("mw-collapsible").removeClass("mw-collapsed");
								}
							});
						}
					});
				})
			}
		}
	};
	//NS.run();
}
//</nowiki>