Commits

Mathias Panzenböck  committed 13c9882

changed internal and export format

  • Participants
  • Parent commits b72c475
  • Tags migration

Comments (0)

Files changed (2)

+syntax: glob
+*~
+.*
+*.css

File stickyNote.user.js

 
 var BASE_Z_INDEX = 100000;
 var STYLE_VERSION = 1;
+var VERSION = 100;
 
 var STATE_DISPLAY = 0;
 var STATE_EDIT    = 1;
 var siteId = location.href.split('#')[0];
 
 function listSites() {
-	return [name for each (name in GM_listValues()) if (name != "stylesheet")];
+	return [name for each (name in GM_listValues()) if (!/^stickynote:/.test(name))];
+}
+
+function GM_getObject(name,defaultValue) {
+	var s = GM_getValue(name);
+
+	if (s === undefined) {
+		return defaultValue;
+	}
+	else {
+		return JSON.parse(s);
+	}
+}
+
+function GM_setObject(name,value) {
+	GM_setValue(name, JSON.stringify(value));
+}
+
+function arrayEquals(a1, a2) {
+	if (a1.length != a2.length) {
+		return false;
+	}
+	
+	for (var i = 0; i < a1.length; ++ i) {
+		if (a1[i] !== a2[i]) {
+			return false;
+		}
+	}
+
+	return true;
 }
 
 function listStickyNotes(site) {
 	var s = GM_getValue(site);
+
+	if (s === undefined) {
+		return [];
+	}
+	else if (s.indexOf('[') != -1) {
+		return JSON.parse(s);
+	}
+	else {
+		return parseOldStickyNotes(s);
+	}
+}
+
+// DEPRECATED
+function parseOldStickyNotes(s) {
 	var notesList = [];
+	var notes = s.split(" ");
 
-	if (s != undefined) {
-		var notes = s.split(" ");
+	for (var i = 0; i < notes.length; ++ i) {
+		var note = notes[i];
 
-		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]);
+			}
 
-			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]);
-			}
+			notesList.push([x,y,width,height,text]);
 		}
 	}
 
 }
 
 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);
+	}
+
 	for each ([x,y,width,height,text] in listStickyNotes(siteId)) {
 		stickyNotes.push(new StickyNote(x,y,width,height,text));
 	}
 		GM_deleteValue(site);
 	}
 	else {
-		GM_setValue(site, [(x+':'+y+':'+w+':'+h+':'+escape(text))
-			for each ([x,y,w,h,text] in notes)].join(' '));
+		GM_setObject(site, notes);
 	}
 }
 
 	overlay.style.top  = ((window.innerHeight - overlayHeight) / 2) + 'px';
 
 	compStyle = getComputedStyle(buttonDiv,'');
-	var buttonsHeigth = compStyle.height;
 	var contentWidth = overlayWidth;
 	var contentHeight = (overlayHeight -
 		(px(compStyle.height) || buttonDiv.offsetHeight) -
 	var elementWidth = (contentWidth -
 		px(compStyle.marginLeft) -
 		px(compStyle.marginRight) -
-		px(compStyle.paddingLeft) -
-		px(compStyle.paddingRight) -
 		px(compStyle.borderLeftWidth) -
 		px(compStyle.borderRightWidth));
 	var elementHeight = (contentHeight -
 		px(compStyle.marginTop) -
 		px(compStyle.marginBottom) -
-		px(compStyle.paddingTop) -
-		px(compStyle.paddingBottom) -
 		px(compStyle.borderTopWidth) -
 		px(compStyle.borderBottomWidth));
 
 
 function exportStickyNotes() {
 	var text = [
-		(name + '\n' + GM_getValue(name))
+		(JSON.stringify(name) + ':' + GM_getValue(name))
 		for each (name in GM_listValues())
-		if (name != "stylesheet")].join('\n');
-	showOverlay(createTextarea(text));
+		if (!/^stickynote:/.test(name))].join(',');
+	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 notes = textarea.value.split("\n");
+		var s = textarea.value;
+		var newNotes;
 
-		for (var i = 0; i < notes.length; i += 2) {
-			var href = notes[i];
+		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);
+		}
 
-			while (href.length == 0 && (++i) < notes.length) {
-				href = notes[i];
+		for (url in newNotes) {
+			if (/^stickynote:/.test(url)) {
+				continue;
 			}
+			var notes = newNotes[url];
+			var existingNotes = GM_getObject(url);
 
-			if (href.length == 0) {
-				break;
-			}
-
-			var siteNotes = GM_getValue(href);
-
-			if ((i+1) >= notes.length || notes[i+1].length == 0) {
-				break;
-			}
-
-			var newNotes = notes[i+1];
-			
-			if (siteNotes != undefined) {
-				var s = siteNotes;
-				var siteNotes = siteNotes.split(" ");
-				var newNotes  = newNotes.split(" ");
-				
-				for (var inn = 0; inn < newNotes.length; ++ inn) {
-					var newNote = newNotes[inn];
+			if (existingNotes !== undefined) {
+				var merged = [note for each (note in existingNotes)];
+				for each (note in notes) {
 					var exists = false;
-					for (var isn = 0; isn < siteNotes.length; ++ isn) {
-						if (newNote == siteNotes[isn]) {
+					for each (existingNote in existingNotes) {
+						if (arrayEquals(note, existingNote)) {
 							exists = true;
 							break;
 						}
 					}
-
-					if (!exists && newNote.length > 0) {
-						s += newNote + " ";
+					if (!exists) {
+						merged.push(note);
 					}
 				}
-				GM_setValue(href,s);
+				newNotes[url] = merged;
+				GM_setObject(url, merged);
 			}
 			else {
-				GM_setValue(href,newNotes);
+				GM_setObject(url, notes);
 			}
 		}
 
 			stickyNotes[i].remove();
 		}
 		stickyNotes = [];
-		loadStickyNotes();
+		for each ([x,y,width,height,text] in newNotes[siteId]) {
+			stickyNotes.push(new StickyNote(x,y,width,height,text));
+		}
 		hideOverlay();
 	});
 	showOverlay(textarea, [importButton]);
 }
 
 function saveStylesheet(style) {
-	GM_setValue("stylesheet", '/*vers' + STYLE_VERSION + '*/\n' + style);
+	GM_setValue("stickynote:stylesheet", '/*vers' + STYLE_VERSION + '*/\n' + style);
 }
 
 function loadStylesheet() {
-	var style = GM_getValue("stylesheet");
+	var style = GM_getValue("stickynote:stylesheet");
 
-	if (style == undefined) {
+	if (style === undefined) {
+		style = GM_getValue("stylesheet");
+
+		if (style !== undefined) {
+			GM_deleteValue("stylesheet");
+			saveStylesheet(style);
+		}
+	}
+
+	if (style === undefined) {
 		style = DEFAULT_STYLESHEET;
 	}
 	else {
 		var vers = 0;
 
 		if (match == null) {
-			GM_deleteValue("stylesheet");
+			GM_deleteValue("stickynote:stylesheet");
 			style = DEFAULT_STYLESHEET;
 		}
 		else {
 			vers = parseInt(match[1]);
 
 			if (vers < STYLE_VERSION) {
-				GM_deleteValue("stylesheet");
+				GM_deleteValue("stickynote:stylesheet");
 				style = DEFAULT_STYLESHEET;
 			}
 			else {