User:Awesome Aasim/savedraft.js

if (!savedraft) { var savedraft = {}; mw.util.addPortletLink("p-personal", "/wiki/Special:BlankPage/Drafts", "Drafts", "pt-drafts", "View my drafts", "", $("#pt-preferences")); $(document).ready(function {		if (mw.config.get("wgAction") == "edit" || mw.config.get("wgAction") == "submit") {			savedraft.section = (new URL(document.location)).searchParams.get("section");			savedraft.key = mw.config.get("wgUserName") + "#" + mw.config.get("wgPageName") + (savedraft.section ? ("#" + savedraft.section) : "");			$(' ').append($(' ').text("Save draft").click(function(e) {				e.preventDefault;				localStorage.setItem(savedraft.key, JSON.stringify({					text: $("#wpTextbox1").val,					date: (new Date).toUTCString				}));				if (!$("#savedraft-buttons").text) {					$("#mw-content-text").prepend($(' ').append($("", { text: "Load draft", id: "savedraft-load", href: "#", click: function(e) { e.preventDefault; try { $("#wpTextbox1").val(JSON.stringify(localStorage.getItem(savedraft.key)).text); } catch(e) { }							mw.notify("Draft loaded."); }					})).append(" &bull; ").append($("", { text: "Delete draft", id: "savedraft-delete", href: "#", click: function(e) { e.preventDefault; if (confirm("Really delete the draft for this page?")) { localStorage.removeItem(savedraft.key); mw.notify("Draft deleted."); $("#savedraft-buttons").remove; }						}					})));				}				mw.notify("Draft saved.");			})).insertAfter("#wpDiffWidget");			var itemText;			try {				itemText = JSON.parse(localStorage.getItem(savedraft.key)).text;			} catch(e) {				itemText = false;			}			if (itemText) {				$("#mw-content-text").prepend($(' ').append($("", { text: "Load draft", id: "savedraft-load", href: "#", click: function(e) { e.preventDefault; try { $("#wpTextbox1").val(JSON.parse(localStorage.getItem(savedraft.key)).text); } catch (e) { }						mw.notify("Draft loaded."); }				})).append(" &bull; ").append($("", { text: "Delete draft", id: "savedraft-delete", href: "#", click: function(e) { e.preventDefault; if (confirm("Really delete the draft for this page?")) { localStorage.removeItem(savedraft.key); mw.notify("Draft deleted."); $("#savedraft-buttons").remove; }					}				})));			}			if ((new URL(document.location)).searchParams.get("draft")) {				try {					$("#wpTextbox1").val(JSON.parse(localStorage.getItem(savedraft.key)).text);				} catch (e) {

}				mw.notify("Draft loaded."); }			savedraft.timeout = function {}; /*			$("#wpTextbox1").on('keyup', function {				clearTimeout(savedraft.timeout);				savedraft.timeout = setTimeout(function { try { localStorage.setItem(savedraft.key, JSON.stringify({ text: $("#wpTextbox1").val, date: (new Date).toUTCString }));					} catch (e) { }					if (!$("#savedraft-buttons").text) { $("#mw-content-text").prepend($(' ').append($("", {							text: "Load draft",							id: "savedraft-load",							href: "#",							click: function(e) {								e.preventDefault;								try {									$("#wpTextbox1").val(JSON.stringify(localStorage.getItem(savedraft.key)).text);								} catch (e) {

}								mw.notify("Draft loaded."); }						})).append(" &bull; ").append($("", { text: "Delete draft", id: "savedraft-delete", href: "#", click: function(e) { e.preventDefault; if (confirm("Really delete the draft for this page?")) { localStorage.removeItem(savedraft.key); mw.notify("Draft deleted."); $("#savedraft-buttons").remove; }							}						})));					}					mw.notify("Draft automatically saved.");				}, 3000); });			$("#wpTextbox1").on('keydown', function { clearTimeout(savedraft.timeout); });			*/			/*			$(".wikiEditor-ui").focusout(function { localStorage.setItem(savedraft.key, JSON.stringify({ text: $("#wpTextbox1").val, date: (new Date).toUTCString }));					if (!$("#savedraft-buttons").text) { $("#mw-content-text").prepend($(' ').append($("", {							text: "Load draft",							id: "savedraft-load",							href: "#",							click: function(e) {								e.preventDefault;								try {									$("#wpTextbox1").val(JSON.stringify(localStorage.getItem(savedraft.key)).text);								} catch (e) {

}								mw.notify("Draft loaded."); }						})).append(" &bull; ").append($("", { text: "Delete draft", id: "savedraft-delete", href: "#", click: function(e) { e.preventDefault; if (confirm("Really delete the draft for this page?")) { localStorage.removeItem(savedraft.key); mw.notify("Draft deleted."); $("#savedraft-buttons").remove; }							}						})));					}					mw.notify("Draft automatically saved.");			}); */		} else if (mw.config.get("wgPageName").toLowerCase == 'Special:BlankPage/Drafts'.toLowerCase) { savedraft.drafts = {}; savedraft.ul = $(''); for (var i in localStorage) { if (i.split("#")[0] == mw.config.get("wgUserName")) { var sec = i.split("#")[2]; var temp = $(""); try { temp.append(JSON.parse(localStorage.getItem(i)).date + ": " + "" + i.split("#")[1] + ""); } catch (e) {

}					temp.append(" (").append($("<a/>", {						href: "#",						"data-draft": i,						text: "delete",						click: function(e) {							e.preventDefault;							if (confirm("Really delete the draft for this page?")) {								localStorage.removeItem($(this).attr("data-draft"));								mw.notify("Draft deleted.");								$(this).parent.remove;							}						}					})).append(")"); savedraft.ul.append(temp); }			}			$('title').text("My drafts - " + mw.config.get("wgSiteName")); $('#firstHeading').text("My drafts"); $('#mw-content-text').html(' Click on a page below to view and manage a draft that you have stored to your computer. ').after(savedraft.ul.html ? savedraft.ul : " No drafts to view. "); }		/*if (mw.config.get("wgAction") == "submit") { localStorage.setItem(savedraft.key, JSON.stringify({ text: $("#wpTextbox1").val, date: (new Date).toUTCString }));			if (!$("#savedraft-buttons").text) { $("#mw-content-text").prepend($(' ').append($("<a/>", {					text: "Load draft",					id: "savedraft-load",					href: "#",					click: function(e) {						e.preventDefault;						try {							$("#wpTextbox1").val(JSON.parse(localStorage.getItem(savedraft.key)).text);						} catch (e) {

}						mw.notify("Draft loaded."); }				})).append(" &bull; ").append($("<a/>", { text: "Delete draft", id: "savedraft-delete", href: "#", click: function(e) { e.preventDefault; if (confirm("Really delete the draft for this page?")) { localStorage.removeItem(savedraft.key); mw.notify("Draft deleted."); $("#savedraft-buttons").remove; }					}				})));			}			mw.notify("Draft automatically saved.");		}*/	}); }