User:Awesome Aasim/newsection.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. |
This user script seems to have a documentation page at User:Awesome Aasim/newsection. |
//<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>