Jump to content

User:Awesome Aasim/savedraft.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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 (!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) : "");
			$('<span id="savedraft-widget" class="oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-buttonInputWidget"></span>').append($('<button id="wp-savedraft" class="oo-ui-inputWidget-input oo-ui-buttonElement-button"></button>').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($('<span id="savedraft-buttons"></span>').append($("<a/>", {
						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($("<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 saved.");
			})).insertAfter("#wpDiffWidget");
			var itemText;
			try {
				itemText = JSON.parse(localStorage.getItem(savedraft.key)).text;
			} catch(e) {
				itemText = false;
			}
			if (itemText) {
				$("#mw-content-text").prepend($('<div id="savedraft-buttons"></div>').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();
						}
					}
				})));
			}
			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($('<span id="savedraft-buttons"></span>').append($("<a/>", {
							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($("<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.");
				}, 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($('<span id="savedraft-buttons"></span>').append($("<a/>", {
							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($("<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.");
			});
			*/
		} else if (mw.config.get("wgPageName").toLowerCase() == 'Special:BlankPage/Drafts'.toLowerCase()) {
			savedraft.drafts = {};
			savedraft.ul = $('<ul></ul>');
			for (var i in localStorage) {
				if (i.split("#")[0] == mw.config.get("wgUserName")) {
					var sec = i.split("#")[2];
					var temp = $("<li></li>");
					try {
						temp.append(JSON.parse(localStorage.getItem(i)).date + ": " + "<a href=\"/wiki/" + i.split("#")[1] + "?action=edit&draft=1" + (sec ? ("&section=" + sec) : "")  + "\">" + i.split("#")[1] + "</a>");
					} 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('<p>Click on a page below to view and manage a draft that you have stored to your computer.</p>').after(savedraft.ul.html() ? savedraft.ul : "<p><b>No drafts to view.</b></p>");
		}
		/*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($('<span id="savedraft-buttons"></span>').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.");
		}*/
	});
}