User:Awesome Aasim/addmylinks.js

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 = ' Error parsing mylinks. Check the console for more details. ';		                   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(" Loading editor... "); $("#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);				    }); $(' Full editor ').appendTo(d); $('').val(wt).appendTo(d).css({"height": "500px", "overflow": "auto", "font-family": "sans-serif" }); $(' ').appendTo(d); $('Preview ').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("Could not parse preview. Please check your edit carefully.  If in doubt, use the full editor. "); myLinks.enableEditor; });			       }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo(d); $('Save ').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) { $("   ").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(" Edit saved! "); $("#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); $(' ').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 { $(' My Links ' + text + ' ').prependTo("#mw-site-navigation"); $(' ' + text + ' ').prependTo("#mw-panel"); $(' ' + text + ' ').prependTo("#sidebar"); $(' ' + text + ' ').prependTo("#mw_portlets"); $(' ' + text + '').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 += 'View My Links'; } catch (Error) { //$('ul.hlist').append('  View My Links </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 = ' Error parsing mylinks.  Check the console for more details. ';		                console.error(d.error);		                if (d.error.code == "missingtitle") {		                    myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links [<a  id="mylinks-edit" title="Create MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Create</a>] ');		                    $("#mylinks-edit").click(myLinks.editclick);		                } else {		                    myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links [<a id="mylinks-edit" title="Edit MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Edit</a>] ' + mylinks + ' '); }		           } else { mylinks = d.parse.text["*"]; myLinks.addToSidebar('<h3 id="mylinks-label" lang="en" dir="ltr">My Links [<a id="mylinks-edit" title="Edit MyLinks" href="' + mw.config.get("wgScriptPath") + '/index.php?title=Special:MyPage/mylinks&action=edit">Edit</a>] ' + mylinks + ' '); $("#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.");		    }); });	} }