User:Awesome Aasim/editform.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. |
![]() | Documentation for this user script can be added at User:Awesome Aasim/editform. |
function EditForm(id, p, _then, presum, postsum) {
this.id = id;
this._then = _then;
this.presum = presum;
this.postsum = postsum;
this.revert = $("#" + this.id).html();
this.wikitext = "";
$("#" + this.id).html("<p><b>Loading editor...</b></p>");
$.get(mw.config.get('wgScriptPath') + '/api.php', {
"action": "query",
"format": "json",
"meta": "tokens",
"type": "csrf"
}).done(function (r) {
console.log("Part 1 done");
$.get(mw.config.get('wgScriptPath') + '/api.php', {
"action": "parse",
"format": "json",
"prop": "wikitext",
"page": p,
"type": "csrf"
}).done(function(wt) {
console.log("Part 2 done");
if (wt.error) {
console.log("Server returned an error");
if (wt.error.code == "missingtitle") {
this.wikitext = "";
$("#" + this.id).html('<form id="' + this.id + '"-editform"></form>');
$('<p><a title="Full editor" target="_blank" href="/w/index.php?title=' + p + '&action=edit">Full editor</a></p>').appendTo("#" + this.id + "-editform");
$('<textarea id="edit-editarea"/>').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });
$('<br>').appendTo("#" + this.id + "-editform");
$('<p></p>').append($('<input id="edit-editsummary" placeholder="Edit summary">').val("")).appendTo("#" + this.id + "-editform");
$('<span id="edit-summary"></span>').appendTo("#" + this.id + "-editform");
$('<input id="edit-minor" type="checkbox"/><label for="edit-minor">This is a minor edit</label>').appendTo("#" + this.id + "-editform");
$('<br>').appendTo("#" + this.id + "-editform");
$('<button id="edit-cancel">Cancel</button>').click(function (_e) {
this.close();
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#" + this.id + "-editform");
$('<button id="edit-previewbutton">Preview</button>').click(function (_e) {
$.get(mw.config.get('wgScriptPath') + '/api.php', {
"action": 'parse',
"prop": 'text',
"pst": 'true', // PST = pre-save transform; this makes substitution work properly
"text": $("#edit-editarea").val(),
"summary": $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "",
"format": "json",
"title": p
}).done(function (r) {
console.log(r);
if (r.error) {
console.error(r.error);
} else {
$("#edit-preview").html(r.parse.text["*"]);
$("#edit-summary").html(r.parse.parsesummary["*"]);
}
}).fail(function (_r) {
$("#edit-preview").html("<span class=\"error\">Could not parse preview. Please check your edit carefully. If in doubt, use the full editor.</span>");
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");
$('<button id="edit-submit">Save</button>').click(function (_e) {
$.post(mw.config.get("wgScriptPath") + '/api.php', {
"action": "edit",
"format": "json",
"title": p,
"text": $("#edit-editarea").val(),
"summary": (presum ? (presum + ($("#edit-editsummary").val() ? ": " + $("#edit-editsummary").val() : "")) : $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "") + postsum,
"minor": $("#edit-minor").is(":checked"),
"notminor": !$("#edit-minor").is(":checked"),
"token": r.query.tokens.csrftoken
}).done(function (h) {
if (h.error) {
mw.notify("<p><b><span style=\"color:red;\">" + h.error.info + "</span></b></p>");
} else if (h.edit.nochange == "") {
this.close();
} else {
$("#" + this.id).html("<p><b><span style=\"color:green;\">Edit saved!</span></p>");
this._then();
}
}).fail(function (_h) {
throw _h;
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo("#" + this.id + "-editform");
$('<div id="edit-preview"></div>').appendTo("#" + this.id + "-editform");
} else {
mw.notify("Error while loading editor: " + wt.error);
}
} else {
this.wikitext = wt.parse.wikitext["*"];
$("#" + this.id).text("");
$("#" + this.id).html('<form id="' + this.id + '"-editform"></form>');
$('<p><a title="Full editor" target="_blank" href="/w/index.php?title=' + p + '&action=edit">Full editor</a></p>').appendTo("#" + this.id + "-editform");
$('<textarea id="edit-editarea"/>').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });
$('<br>').appendTo("#" + this.id + "-editform");
$('<p></p>').append($('<input id="edit-editsummary" placeholder="Edit summary">').val("")).appendTo("#" + this.id + "-editform");
$('<span id="edit-summary"></span>').appendTo("#" + this.id + "-editform");
$('<input id="edit-minor" type="checkbox"/><label for="edit-minor">This is a minor edit</label>').appendTo("#" + this.id + "-editform");
$('<br>').appendTo("#" + this.id + "-editform");
$('<button id="edit-cancel">Cancel</button>').click(function (_e) {
this.close();
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#" + this.id + "-editform");
$('<button id="edit-previewbutton">Preview</button>').click(function (_e) {
$.get(mw.config.get('wgScriptPath') + '/api.php', {
"action": 'parse',
"prop": 'text',
"pst": 'true', // PST = pre-save transform; this makes substitution work properly
"text": $("#edit-editarea").val(),
"summary": $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "",
"format": "json",
"title": p
}).done(function (r) {
console.log(r);
if (r.error) {
console.error(r.error);
} else {
$("#edit-preview").html(r.parse.text["*"]);
$("#edit-summary").html(r.parse.parsesummary["*"]);
}
}).fail(function (_r) {
$("#edit-preview").html("<span class=\"error\">Could not parse preview. Please check your edit carefully. If in doubt, use the full editor.</span>");
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");
$('<button id="edit-submit">Save</button>').click(function (_e) {
$.post(mw.config.get("wgScriptPath") + '/api.php', {
"action": "edit",
"format": "json",
"title": p,
"text": $("#edit-editarea").val(),
"summary": (presum ? (presum + ($("#edit-editsummary").val() ? ": " + $("#edit-editsummary").val() : "")) : $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "") + postsum,
"minor": $("#edit-minor").is(":checked"),
"notminor": !$("#edit-minor").is(":checked"),
"token": r.query.tokens.csrftoken
}).done(function (h) {
if (h.error) {
mw.notify("<p><b><span style=\"color:red;\">" + h.error.info + "</span></b></p>");
} else if (h.edit.nochange == "") {
this.close();
} else {
$("#" + this.id).html("<p><b><span style=\"color:green;\">Edit saved!</span></p>");
this._then();
}
}).fail(function (_h) {
throw _h;
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo("#" + this.id + "-editform");
$('<div id="edit-preview"></div>').appendTo("#" + this.id + "-editform");
}
}).fail(function(wt) {
throw wt;
})
}).fail(function (r) {
throw r;
});
}
EditForm.prototype.disable = function() {
$("#edit-editarea").prop("disabled", true);
$("#edit-editsummary").prop("disabled", true);
$("#edit-minor").prop("disabled", true);
$("#edit-cancel").prop("disabled", true);
$("#edit-previewbutton").prop("disabled", true);
$("#edit-submit").prop("disabled", true);
}
EditForm.prototype.enable = function() {
$("#edit-editarea").prop("disabled", false);
$("#edit-editsummary").prop("disabled", false);
$("#edit-minor").prop("disabled", false);
$("#edit-cancel").prop("disabled", false);
$("#edit-previewbutton").prop("disabled", false);
$("#edit-submit").prop("disabled", false);
}
EditForm.prototype.close = function() {
$("#" + this.id).html(this.revert);
}