User:Awesome Aasim/addmylinks.js
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.
if (!myLinks) {
if (mw.config.get("wgUserName")) {
var myLinks = {};
myLinks.myLinksSpecialPageLocation = "BlankPage/MyLinks";
myLinks.disableEditor = function() {
$("#edit-editarea").prop("disabled", true);
$("#edit-editsummary").prop("disabled", true);
$("#edit-cancel").prop("disabled", true);
$("#edit-previewbutton").prop("disabled", true);
$("#edit-submit").prop("disabled", true);
}
myLinks.editclick = function (e) {
e.preventDefault();
myLinks.myLinksEditForm("#mylinks-content", 'User:' + mw.config.get("wgUserName") + "/mylinks", function () {
var mylinks = "";
$.get(mw.config.get('wgScriptPath') + '/api.php', {
'action': 'parse',
'page': "User:" + mw.config.get("wgUserName") + "/mylinks",
'prop': 'text',
'format': 'json'
}).done(function (d) {
if (d.error) {
mylinks = '<span class="error">Error parsing mylinks. Check the console for more details.</span>';
console.error(d.error);
if (d.error.code == "missingtitle") {
$("#mylinks-content").html(mylinks);
$("#mylinks-edit").off("click");
$("#mylinks-edit").click(myLinks.editclick);
} else {
$("#mylinks-content").html(mylinks);
}
} else {
mylinks = d.parse.text["*"];
$("#mylinks-content").html(mylinks);
$("#mylinks-edit").off("click");
$("#mylinks-edit").click(myLinks.editclick);
}
for (var i = 0; i < document.getElementById("mylinks").getElementsByClassName("mw-editsection").length; i = 0) {
document.getElementById("mylinks").getElementsByClassName("mw-editsection")[i].outerHTML = "";
}
}).fail(function (d) {
console.error("An unknown error has occurred.");
});
});
}
myLinks.enableEditor = function() {
$("#edit-editarea").prop("disabled", false);
$("#edit-editsummary").prop("disabled", false);
$("#edit-cancel").prop("disabled", false);
$("#edit-previewbutton").prop("disabled", false);
$("#edit-submit").prop("disabled", false);
}
myLinks.myLinksEditForm = function(d, p, _then) {
var revert = $(d).html();
var revertEdit = $("#mylinks-edit").text();
var revertEditTitle = $("#mylinks-edit").attr('title');
$("#mylinks-edit").off("click");
$(d).html("<p><b>Loading editor...</b></p>");
$("#mylinks-edit").text("");
$.get(mw.config.get('wgScriptPath') + '/api.php', {
'action': 'parse',
'page': p,
'prop': 'wikitext',
'format': 'json'
}).done(function (f) {
var wt = "";
if (f.error) {
if (f.error.code == "missingtitle") {
wt = "";
} else {
throw f.error.info;
}
} else {
wt = f.parse.wikitext["*"];
}
$.get(mw.config.get('wgScriptPath') + '/api.php', {
"action": "query",
"format": "json",
"meta": "tokens",
"type": "csrf"
}).done(function (r) {
$(d).html("");
$("#mylinks-edit").attr('title', "Cancel");
$("#mylinks-edit").text("Cancel").click(function(e) {
e.preventDefault();
$("#mylinks-edit").off("click");
$(d).html(revert);
$("#mylinks-edit").text(revertEdit).click(myLinks.editclick);
$("#mylinks-edit").attr('title', revertEditTitle);
});
$('<p><a title="Full editor" target="_blank" href="/w/index.php?title=' + p + '&action=edit">Full editor</a></p>').appendTo(d);
$('<textarea id="edit-editarea"/>').val(wt).appendTo(d).css({"height": "500px", "overflow": "auto", "font-family": "sans-serif" });
$('<br>').appendTo(d);
$('<button id="edit-previewbutton">Preview</button>').click(function (_e) {
myLinks.disableEditor();
$.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": "updating mylinks",
"format": "json",
"title": p
}).done(function (r) {
console.log(r);
if (r.error) {
console.error(r.error);
} else {
$("#edit-preview").html(r.parse.text["*"]);
}
for (var i = 0; i < document.getElementById("mylinks").getElementsByClassName("mw-editsection").length; i = 0) {
document.getElementById("mylinks").getElementsByClassName("mw-editsection")[i].outerHTML = "";
}
myLinks.enableEditor();
}).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>");
myLinks.enableEditor();
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo(d);
$('<button id="edit-submit">Save</button>').click(function (_e) {
myLinks.disableEditor();
$("#edit-preview").html("");
$.post(mw.config.get("wgScriptPath") + '/api.php', {
"action": "edit",
"format": "json",
"title": p,
"text": $("#edit-editarea").val(),
"summary": "updating mylinks",
"token": r.query.tokens.csrftoken
}).done(function (h) {
if (h.error) {
$("<p><b><span style=\"color:red;\"></span></b></p>").text(h.error.info).appendTo(d);
} else if (h.edit.nochange == "") {
$("#mylinks-edit").off("click");
$(d).html(revert);
$("#mylinks-edit").text(revertEdit).click(myLinks.editclick);
} else {
$("#mylinks-edit").off("click");
$(d).html("<p><b><span style=\"color:green;\">Edit saved!</span></p>");
$("#mylinks-edit").text("Edit").click(myLinks.editclick);
_then();
}
}).fail(function (_h) {
});
}).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo(d);
$('<div id="edit-preview"></div>').appendTo(d);
}).fail(function (r) {
throw "Unknown error";
});
}).fail(function (f) {
throw "Unknown error";
});
}
myLinks.addToSidebar = function(text) {
if (mw.config.get("wgPageName").toLowerCase() == ("Special:" + myLinks.myLinksSpecialPageLocation).toLowerCase() && false) {
$("title").text("My Links - " + mw.config.get("wgSiteName"));
//var temp = document.getElementById("mw-returnto").innerHTML;
$("#mw-content-text").html(text);
//document.getElementById("mw-content-text").innerHTML += temp;
$("#firstHeading").html($("#mylinks-label").html());
$("#section_0").html($("#mylinks-label").html());
document.getElementById("mylinks-label").outerHTML = "";
for (var i = 0; i < document.getElementById("mylinks").getElementsByClassName("mw-editsection").length; i = 0) {
document.getElementsByClassName("mw-editsection")[i].outerHTML = "";
}
} else {
$('<div class="sidebar-chunk" id="mylinks"><h2><span>My Links</span></h2><div class="sidebar-inner">' + text + '</div></div>').prependTo("#mw-site-navigation");
$('<div class="portal" role="navigation" id="mylinks" aria-labelledby="p-mylinks-label">' + text + '</div>').prependTo("#mw-panel");
$('<div role="navigation" class="portlet generated-sidebar" id="mylinks" aria-labelledby="p-mylinks-label">' + text + '</div>').prependTo("#sidebar");
$('<div class="portlet" id="mylinks">' + text + '</div>').prependTo("#mw_portlets");
$('<ul id="mylinks">' + text + '</ul>').appendTo("#mw-mf-page-left"); //minerva
$("#p-navigation").prependTo("#mw-panel");
$("#p-search").prependTo("#quickbar");
$('#p-logo').prependTo("#mw-site-navigation");
$('#p-logo').prependTo("#mw-panel");
$('#p-logo').prependTo("#sidebar");
$('#p-logo').prependTo("#mw_portlets");
$('ul.hlist:first').appendTo('#mw-mf-page-left');
try {
//document.getElementById('p-personal').getElementsByTagName("ul")[0].innerHTML += '<li id="mylinks-full"><a title="My Links" href="/wiki/' + ("Special:" + myLinksSpecialPageLocation) + '">View My Links</a></li>';
} catch (Error) {
//$('ul.hlist').append('<li class><a class href="/wiki/' + ("Special:" + myLinksSpecialPageLocation) + '" data-event-name><span>View My Links</span></span></a></li>');
}
if (mw.config.get("skin") == "vector") {
$("#mylinks").addClass("mw-portlet mw-portlet-navigation vector-menu vector-menu-portal");
}
}
$("#mylinks-content").css("height", "auto");
$("#mylinks-content").css("overflow", "auto");
}
$(document).ready(function () {
var mylinks = "";
$.get(mw.config.get('wgScriptPath') + '/api.php', {
'action': 'parse',
'page': "User:" + mw.config.get("wgUserName") + "/mylinks",
'prop': 'text',
'format': 'json'
}).done(function (d) {
$.get(mw.config.get('wgScriptPath') + '/api.php', {
'action': 'parse',
'page': 'User:' + mw.config.get("wgUserName") + "/mylinks",
'prop': 'wikitext',
'format': 'json'
}).done(function (f) {
if (d.error) {
mylinks = '<span class="error">Error parsing mylinks. Check the console for more details.</span>';
console.error(d.error);
if (d.error.code == "missingtitle") {
myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links <span style="text-align:right">[<a id="mylinks-edit" title="Create MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Create</a>]</span></h3><div class="mw-portlet-body body pBody" id="mylinks-content"></div>');
$("#mylinks-edit").click(myLinks.editclick);
} else {
myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links <span style="text-align:right">[<a id="mylinks-edit" title="Edit MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Edit</a>]</span></h3><div class="mw-portlet-body body pBody" id="mylinks-content">' + mylinks + '</div>');
}
} else {
mylinks = d.parse.text["*"];
myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links <span style="text-align:right">[<a id="mylinks-edit" title="Edit MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Edit</a>]</span></h3><div class="mw-portlet-body body pBody" id="mylinks-content">' + mylinks + '</div>');
$("#mylinks-edit").click(myLinks.editclick);
}
for (var i = 0; i < document.getElementById("mylinks").getElementsByClassName("mw-editsection").length; i = 0) {
document.getElementById("mylinks").getElementsByClassName("mw-editsection")[i].outerHTML = "";
}
$(document).click(function(e) {
if ($(e.target).closest("#mylinks").length == 0) {
$("#mylinks").removeClass("dropdown-active");
}
})
$(".sidebar-chunk").find("h2").click(function(e) {
e.preventDefault();
if ($(this).parent().attr("id") != "mylinks") {
$("#mylinks").removeClass("dropdown-active");
} else {
$("#mylinks").toggleClass("dropdown-active");
}
})
}).fail(function (f) {
console.error("An unknown error has occurred.");
});
}).fail(function (d) {
console.error("An unknown error has occurred.");
});
});
}
}