Commits

Mathias Panzenböck committed 653d640

removed migration code and moved default stylesheet to a resource

  • Participants
  • Parent commits b4930bf

Comments (0)

Files changed (2)

File default_style.css

+/* sticky note */
+.at-ac-tuwien-student-e0427417-sticky-note {
+  border-style:     solid;
+  background-color: #ffe555;
+  padding:          10px;
+  border-width:      2px;
+  border-color:     #ccaa22;
+  cursor:           default;
+}
+
+/* top bar */
+.at-ac-tuwien-student-e0427417-sticky-note-top {
+  text-align:       right;
+  background-color: inherit;
+  margin-top:       -4px;
+}
+
+/* close button */
+.at-ac-tuwien-student-e0427417-sticky-note-top a {
+  font-family:     DejaVu Sans, sans-serif;
+  text-decoration: none;
+  font-weight:     bold;
+  color:           #ccaa22;
+  font-size:       13pt;
+  cursor:          pointer;
+}
+
+/* close button when hovered */
+.at-ac-tuwien-student-e0427417-sticky-note-top a:hover {
+  color: #333333;
+}
+
+/* text when displayed / editing */
+.at-ac-tuwien-student-e0427417-sticky-note-display-text,
+.at-ac-tuwien-student-e0427417-sticky-note-edit-text {
+  font-family:      sans-serif;
+  font-size:        10pt;
+  background-color: inherit;
+  color:            #333333;
+  text-align:       left;
+}
+
+/* sticky note when editing */
+.at-ac-tuwien-student-e0427417-sticky-note-edit-mode {
+  opacity: 1;
+}
+
+/* sticky note when displayed / moving / resizing */
+.at-ac-tuwien-student-e0427417-sticky-note-display-mode,
+.at-ac-tuwien-student-e0427417-sticky-note-move-mode,
+.at-ac-tuwien-student-e0427417-sticky-note-resize-mode {
+  opacity: 0.85;
+}
+
+/* sticky note when moving */
+.at-ac-tuwien-student-e0427417-sticky-note-move-mode {
+  cursor: move;
+}
+
+/* resize grabber */
+.at-ac-tuwien-student-e0427417-sticky-note-resize-grabber {
+  position: absolute;
+  right:    -2px;
+  bottom:   -2px;
+  width:    16px;
+  height:   16px;
+  cursor:   se-resize;
+}
+
+/* blanket */
+#at-ac-tuwien-student-e0427417-sticky-note-blanket {
+  background-color: #111111;
+  opacity:          0.65;
+}
+
+/* overlay */
+#at-ac-tuwien-student-e0427417-sticky-note-overlay {
+  text-align:       left;
+  padding:          15px;
+  color:            #000000;
+  background-color: #FFFFFF;
+  border:           5px solid #D0D0D0;
+  width:            800px;
+  height:           600px;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-overlay-buttons {
+  text-align: right;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-overlay-buttons input {
+  margin:  2px;
+  padding: 2px;
+  height: 24px;
+}
+
+/* management interface */
+#at-ac-tuwien-student-e0427417-sticky-note-manage {
+  overflow:      auto;
+  font-size:     12pt;
+  padding-right: 10px;
+}
+
+#at-ac-tuwien-student-e0427417-sticky-note-manage ul {
+  padding:    0px 0px 0px 30px;
+  margin:     0px;
+  list-style: square outside;
+}
+
+#at-ac-tuwien-student-e0427417-sticky-note-manage li ul {
+  list-style: disc outside;
+}
+
+/* delete icon in management interface */
+.at-ac-tuwien-student-e0427417-sticky-note-manage-delete {
+  float:       right;
+  font-weight: bold;
+}
+
+/* site row in management interface */
+.at-ac-tuwien-student-e0427417-sticky-note-manage-site:hover {
+  background-color: #D0D0D0;
+}
+
+/* note row in management interface */
+.at-ac-tuwien-student-e0427417-sticky-note-manage-note:hover {
+  background-color: #D0D0D0;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-manage-site,
+.at-ac-tuwien-student-e0427417-sticky-note-manage-note {
+  cursor: pointer;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-manage-site a,
+.at-ac-tuwien-student-e0427417-sticky-note-manage-note a {
+  color:  #000000;
+  cursor: pointer;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-manage-site a:hover,
+.at-ac-tuwien-student-e0427417-sticky-note-manage-note a:hover {
+  color: #FF0000;
+}
+
+.at-ac-tuwien-student-e0427417-sticky-note-manage-link {
+  float: right;
+}

File stickyNote.user.js

 // @name           Sticky Notes
 // @namespace      http://twoday.tuwien.ac.at/pub/
 // @description    add sticky notes to websites
+// @resource       style https://web.student.tuwien.ac.at/~e0427417/downloads/sticky-note/default_style.v1.css
 // @include        *
 // ==/UserScript==
 //
 var STATE_RESIZE  = 3;
 var MIN_SIZE      = 15;
 
-var DEFAULT_STYLESHEET =
-"/* sticky note */\n\
-.at-ac-tuwien-student-e0427417-sticky-note {\n\
-  border-style:     solid;\n\
-  background-color: #ffe555;\n\
-  padding:          10px;\n\
-  border-width:      2px;\n\
-  border-color:     #ccaa22;\n\
-  cursor:           default;\n\
-}\n\
-\n\
-/* top bar */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-top {\n\
-  text-align:       right;\n\
-  background-color: inherit;\n\
-  margin-top:       -4px;\n\
-}\n\
-\n\
-/* close button */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-top a {\n\
-  font-family:     DejaVu Sans, sans-serif;\n\
-  text-decoration: none;\n\
-  font-weight:     bold;\n\
-  color:           #ccaa22;\n\
-  font-size:       13pt;\n\
-  cursor:          pointer;\n\
-}\n\
-\n\
-/* close button when hovered */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-top a:hover {\n\
-  color: #333333;\n\
-}\n\
-\n\
-/* text when displayed / editing */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-display-text,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-edit-text {\n\
-  font-family:      sans-serif;\n\
-  font-size:        10pt;\n\
-  background-color: inherit;\n\
-  color:            #333333;\n\
-  text-align:       left;\n\
-}\n\
-\n\
-/* sticky note when editing */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-edit-mode {\n\
-  opacity: 1;\n\
-}\n\
-\n\
-/* sticky note when displayed / moving / resizing */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-display-mode,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-move-mode,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-resize-mode {\n\
-  opacity: 0.85;\n\
-}\n\
-\n\
-/* sticky note when moving */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-move-mode {\n\
-  cursor: move;\n\
-}\n\
-\n\
-/* resize grabber */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-resize-grabber {\n\
-  position: absolute;\n\
-  right:    -2px;\n\
-  bottom:   -2px;\n\
-  width:    16px;\n\
-  height:   16px;\n\
-  cursor:   se-resize;\n\
-}\n\
-\n\
-/* blanket */\n\
-#at-ac-tuwien-student-e0427417-sticky-note-blanket {\n\
-  background-color: #111111;\n\
-  opacity:          0.65;\n\
-}\n\
-\n\
-/* overlay */\n\
-#at-ac-tuwien-student-e0427417-sticky-note-overlay {\n\
-  text-align:       left;\n\
-  padding:          15px;\n\
-  color:            #000000;\n\
-  background-color: #FFFFFF;\n\
-  border:           5px solid #D0D0D0;\n\
-  width:            800px;\n\
-  height:           600px;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-overlay-buttons {\n\
-  text-align: right;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-overlay-buttons input {\n\
-  margin:  2px;\n\
-  padding: 2px;\n\
-  height: 24px;\n\
-}\n\
-\n\
-/* management interface */\n\
-#at-ac-tuwien-student-e0427417-sticky-note-manage {\n\
-  overflow:      auto;\n\
-  font-size:     12pt;\n\
-  padding-right: 10px;\n\
-}\n\
-\n\
-#at-ac-tuwien-student-e0427417-sticky-note-manage ul {\n\
-  padding:    0px 0px 0px 30px;\n\
-  margin:     0px;\n\
-  list-style: square outside;\n\
-}\n\
-\n\
-#at-ac-tuwien-student-e0427417-sticky-note-manage li ul {\n\
-  list-style: disc outside;\n\
-}\n\
-\n\
-/* delete icon in management interface */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-delete {\n\
-  float:       right;\n\
-  font-weight: bold;\n\
-}\n\
-\n\
-/* site row in management interface */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-site:hover {\n\
-  background-color: #D0D0D0;\n\
-}\n\
-\n\
-/* note row in management interface */\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-note:hover {\n\
-  background-color: #D0D0D0;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-site,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-note {\n\
-  cursor: pointer;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-site a,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-note a {\n\
-  color:  #000000;\n\
-  cursor: pointer;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-site a:hover,\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-note a:hover {\n\
-  color: #FF0000;\n\
-}\n\
-\n\
-.at-ac-tuwien-student-e0427417-sticky-note-manage-link {\n\
-  float: right;\n\
-}";
-
 function StickyNote(x,y,width,height,text) {
 	var stickyNote = this;
 
 
 	stickyNote.container = document.createElement("div");
 	stickyNote.container.style.position = "absolute";
-	stickyNote._width  = width  == undefined || width  < MIN_SIZE ? 250 : width;
-	stickyNote._height = height == undefined || height < MIN_SIZE ? 250 : height;
-	stickyNote._x = x == undefined ? ((window.innerWidth - stickyNote._width) / 2 + window.pageXOffset) : x;
-	stickyNote._y = y == undefined ? ((window.innerHeight - stickyNote._height) / 2 + window.pageYOffset) : y;
+	stickyNote._width  = width  === undefined || width  < MIN_SIZE ? 250 : width;
+	stickyNote._height = height === undefined || height < MIN_SIZE ? 250 : height;
+	stickyNote._x = x === undefined ? ((window.innerWidth - stickyNote._width) / 2 + window.pageXOffset) : x;
+	stickyNote._y = y === undefined ? ((window.innerHeight - stickyNote._height) / 2 + window.pageYOffset) : y;
 	stickyNote.container.style.left = stickyNote._x + "px";
 	stickyNote.container.style.top  = stickyNote._y + "px";
 
 }
 
 function listStickyNotes(site) {
-	var s = GM_getValue(site);
+	var notes = GM_getObject(site);
 
-	if (s === undefined) {
+	if (notes === undefined) {
 		return [];
 	}
-	else if (s.indexOf('[') != -1) {
-		return JSON.parse(s);
+	else {
+		return notes;
 	}
-	else {
-		return parseOldStickyNotes(s);
-	}
-}
-
-// DEPRECATED
-function parseOldStickyNotes(s) {
-	var notesList = [];
-	var notes = s.split(" ");
-
-	for (var i = 0; i < notes.length; ++ i) {
-		var note = notes[i];
-
-		if (note.length > 0) {
-			var data = note.split(":");
-			var x = parseInt(data[0]);
-			var y = parseInt(data[1]);
-			var width;
-			var height;
-			var text;
-				
-			if (data.length == 5) {
-				width  = parseInt(data[2]);
-				height = parseInt(data[3]);
-				text   = unescape(data[4]);
-			}
-			else {
-				width  = undefined;
-				height = undefined;
-				text   = unescape(data[2]);
-			}
-
-			notesList.push([x,y,width,height,text]);
-		}
-	}
-
-	return notesList;
 }
 
 function reloadStickyNotes() {
 function loadStickyNotes() {
 	var version = GM_getValue('stickynote:version');
 
-	console.log('version: '+typeof(version)+', '+version);
-
-	if (version === undefined || version < 100) {
-		// migration
-		var sites = listSites();
-
-		if (sites.length > 0) {
-			try {
-				for each (site in sites) {
-					GM_setObject(site, listStickyNotes(site));
-				}
-				alert('Successfuly migrated Sticky Notes.');
-			}
-			catch (e) {
-				alert('Sticky Note migration error: '+e.message);
-			}
-		}
-	}
-
 	if (version !== VERSION) {
 		GM_setValue('stickynote:version', VERSION);
 	}
 }
 
 function showOverlay(element, buttons) {
-	if (buttons == undefined) {
+	if (buttons === undefined) {
 		buttons = [];
 	}
 
 	showOverlay(createTextarea('{'+text+'}'));
 }
 
-// DEPRECATED
-function parseOldStickyNotesExport(s) {
-	var importedNotes = {};
-	var notes = s.split('\n');
-
-	for (var i = 0; i < notes.length; i += 2) {
-		var href = notes[i];
-
-		while (href.length == 0 && (++i) < notes.length) {
-			href = notes[i];
-		}
-
-		if (href.length == 0) {
-			break;
-		}
-
-		if ((i+1) >= notes.length || notes[i+1].length == 0) {
-			break;
-		}
-
-		var newNotes = [];
-		for each (note in notes[i+1].split(' ')) {
-			note = note.split(':');
-			note[0] = parseInt(note[0]);
-			note[1] = parseInt(note[1]);
-
-			if (note.length == 5) {
-				newNotes.push([note[0], note[1],
-					parseInt(note[2]), parseInt(note[3]),
-					unescape(note[4])]);
-			}
-			else {
-				newNotes.push([note[0], note[1],
-					undefined, undefined,
-					unescape(note[2])]);
-			}
-		}
-
-		importedNotes[href] = newNotes;
-	}
-
-	return importedNotes;
-}
-
 function importStickyNotes() {
 	var textarea = createTextarea();
 	var importButton = createButton('Import', function() {
-		var s = textarea.value;
-		var newNotes;
-
-		if (s.indexOf('{') != -1) {
-			newNotes = JSON.parse(s);
-			for (url in newNotes) {
-				var notes = newNotes[url];
-				for (var i = 0; i < notes.length;) {
-					var note = notes[i];
-					// ignore invalid notes
-					if (!(note instanceof Array) || note.length != 5 ||
-							typeof(note[0]) != 'number' || typeof(note[1]) != 'number' ||
-							typeof(note[2]) != 'number' || typeof(note[3]) != 'number' ||
-							typeof(note[4]) != 'string') {
-						notes = notes.splice(i, 1);
-					}
-					else {
-						++ i;
-					}
-				}
-			}
-		}
-		else {
-			newNotes = parseOldStickyNotesExport(s);
-		}
+		var newNotes = JSON.parse(textarea.value);
 
 		for (url in newNotes) {
 			if (/^stickynote:/.test(url)) {
 				continue;
 			}
-			var notes = newNotes[url];
+
+			// ignore invalid notes:
+			var notes = [note for each (note in newNotes[url]) if (
+				note instanceof Array && note.length == 5 &&
+				typeof(note[0]) == 'number' && typeof(note[1]) == 'number' &&
+				typeof(note[2]) == 'number' && typeof(note[3]) == 'number' &&
+				typeof(note[4]) == 'string')];
+
 			var existingNotes = GM_getObject(url);
 
 			if (existingNotes !== undefined) {
 				GM_setObject(url, merged);
 			}
 			else {
+				newNotes[url] = notes;
 				GM_setObject(url, notes);
 			}
 		}
 
-		for (var i = 0; i < stickyNotes.length; ++ i) {
-			stickyNotes[i].remove();
+		for each (note in stickyNotes) {
+			note.remove();
 		}
 		stickyNotes = [];
 		for each ([x,y,width,height,text] in newNotes[siteId]) {
 	var style = GM_getValue("stickynote:stylesheet");
 
 	if (style === undefined) {
-		style = GM_getValue("stylesheet");
-
-		if (style !== undefined) {
-			GM_deleteValue("stylesheet");
-			saveStylesheet(style);
-		}
-	}
-
-	if (style === undefined) {
-		style = DEFAULT_STYLESHEET;
+		style = GM_getResourceText('style');
 	}
 	else {
 		// reset style if its to old
 
 		if (match == null) {
 			GM_deleteValue("stickynote:stylesheet");
-			style = DEFAULT_STYLESHEET;
+			style = GM_getResourceText('style');
 		}
 		else {
 			vers = parseInt(match[1]);
 
 			if (vers < STYLE_VERSION) {
 				GM_deleteValue("stickynote:stylesheet");
-				style = DEFAULT_STYLESHEET;
+				style = GM_getResourceText('style');
 			}
 			else {
 				style = style.substring(match[0].length);
 function setStylesheet(style) {
 	var styleElem = document.getElementById("at-ac-tuwien-student-e0427417-sticky-note-style");
 
-	if (styleElem == undefined) {
+	if (!styleElem) {
 		styleElem = document.createElement("style");
 		styleElem.id = "at-ac-tuwien-student-e0427417-sticky-note-style";
 		styleElem.type = "text/css";
 function getStylesheet() {
 	var styleElem = document.getElementById("at-ac-tuwien-student-e0427417-sticky-note-style");
 
-	if (styleElem == undefined) {
-		return DEFAULT_STYLESHEET;
+	if (!styleElem) {
+		return GM_getResourceText('style');
 	}
 	else {
 		return styleElem.innerHTML;
 function editStylesheet() {
 	var textarea = createTextarea(getStylesheet());
 	var resetButton = createButton("Reset", function() {
-		textarea.value = DEFAULT_STYLESHEET;
+		textarea.value = GM_getResourceText('style');
 	});
 	var saveButton = createButton("Save", function() {
 		saveStylesheet(textarea.value);
 
 unsafeWindow.addStickyNote = addStickyNote;
 
-// HACK: This should be done by greasemonkey..
+// HACK: This should be done by greasemonkey.
 window.addEventListener('keydown', function(e) {
 	if (e.altKey && e.which == 78) {
 		addStickyNote();