diff --git a/context.js b/context.js index 5c9a280..3510617 100644 --- a/context.js +++ b/context.js @@ -26,12 +26,14 @@ function publish(content, font) { url = "https://write.as/"+id; } editToken = data.data.token; - - // Save the data - posts = JSON.parse(H.get('posts', '[]')); - posts.push(H.createPost(id, editToken, content)); - H.set('posts', JSON.stringify(posts)); - + + // Save the data if user wasn't logged in + if (typeof data.data.owner === 'undefined' || data.data.owner == "") { + posts = JSON.parse(H.get('posts', '[]')); + posts.push(H.createPost(id, editToken, post.content)); + H.set('posts', JSON.stringify(posts)); + } + // Launch post chrome.tabs.create({ url: url }); } else { diff --git a/popup.html b/popup.html index ff6fa9b..b297f21 100644 --- a/popup.html +++ b/popup.html @@ -54,7 +54,7 @@ #publish-holder, #result-holder { text-align: right; } - #result-holder { + #result-holder, #account-tools { display: none; } #url { @@ -71,6 +71,15 @@ left: 1em; right: 1em; } + #account-tools { + margin-top: -0.5em; + margin-bottom: 0.5em; + font-size: 0.86em; + } + #account-tools a { + line-height: 24px; + margin-left: 0.5em; + } body.popout textarea { position: fixed; top: 0; @@ -93,6 +102,15 @@ body.popout a#popout { display: none; } + #username { + font-weight: bold; + } + #sync.disabled { + display: inline; + color: #999; + font-style: italic; + text-decoration: none; + } @@ -100,6 +118,9 @@
+
+ Writing as write.as. Sync... +
diff --git a/popup.js b/popup.js index 3fb8ee7..e0cb3ca 100644 --- a/popup.js +++ b/popup.js @@ -41,6 +41,7 @@ function publish(content, font) { } editToken = data.data.token; + document.getElementById("account-tools").style.display = 'none'; document.getElementById("publish-holder").style.display = 'none'; document.getElementById("result-holder").style.display = 'inline'; @@ -49,10 +50,12 @@ function publish(content, font) { var $urlLink = document.getElementById("url-link"); $urlLink.href = url; - // Save the data - posts = JSON.parse(H.get('posts', '[]')); - posts.push(H.createPost(id, editToken, content)); - H.set('posts', JSON.stringify(posts)); + // Save the data if user wasn't logged in + if (typeof data.data.owner === 'undefined' || data.data.owner == "") { + posts = JSON.parse(H.get('posts', '[]')); + posts.push(H.createPost(id, editToken, post.content)); + H.set('posts', JSON.stringify(posts)); + } } else { alert("Failed to post. Please try again."); } @@ -112,6 +115,80 @@ document.addEventListener('DOMContentLoaded', function() { chrome.runtime.sendMessage({msg: $content.value}); }); }); + + document.getElementById('sync').addEventListener('click', function(e) { + e.preventDefault(); + var posts = JSON.parse(H.get('posts', '[]')); + + var p = "There "; + p += ((posts.length==1?'is ':'are ') + posts.length + " post" + (posts.length==1?'':'s')); + var thePosts = posts.length == 1 ? 'it' : 'them'; + p += " saved on this computer.\n\nSyncing "+thePosts+" to your account gives you access to "+thePosts+" from anywhere. Sync now?"; + if (!confirm(p)) { + return; + } + + var $sync = this; + $sync.innerText = "Syncing now..."; + $sync.className = 'disabled'; + + var http = new XMLHttpRequest(); + var params = []; + for (var i=0; i 0) { + if (res.data.length != posts.length) { + // TODO: handle this serious situation + console.error("Request and result array length didn't match!"); + return; + } + for (var i=0; i 0) { + document.getElementById('sync').style.display = 'inline'; + } else { + document.getElementById('sync').style.display = 'none'; + } + //document.getElementById("sync-count").innerText = posts.length + " post" + (posts.length==1?'':'s'); + document.getElementById("username").innerText = data.username; + } + } + } + http.send(); + } + handleRegUser(); if (H.get('updatedPostsMeta', '') == '') { // Add metadata used by Pad to all saved posts