Browse Source

Show logged in user & support post sync

tags/v2.0
Matt Baer 7 years ago
parent
commit
a6c863828c
3 changed files with 136 additions and 11 deletions
  1. +8
    -6
      context.js
  2. +22
    -1
      popup.html
  3. +106
    -4
      popup.js

+ 8
- 6
context.js View File

@@ -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 {


+ 22
- 1
popup.html View File

@@ -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;
}
</style>

<script src="H.js"></script>
@@ -100,6 +118,9 @@
</head>
<body>
<form name="postForm" method="post">
<div id="account-tools">
Writing as <span id="username">write.as</span>. <a id="sync" href="#">Sync...</a>
</div>
<textarea id="content" placeholder="Write..."></textarea>
<div id="result-holder">
<input id="url" type="url" readonly />


+ 106
- 4
popup.js View File

@@ -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<posts.length; i++) {
params.push({id: posts[i].id, token: posts[i].token});
}
http.open("POST", "https://write.as/api/posts/claim", true);
http.setRequestHeader("Content-type", "application/json");
http.onreadystatechange = function() {
if (http.readyState == 4) {
$sync.innerText = 'Importing now...';
if (http.status == 200) {
var res = JSON.parse(http.responseText);
if (res.data.length > 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<res.data.length; i++) {
if (res.data[i].code == 200 || res.data[i].code == 404) {
// Post successfully claimed.
for (var j=0; j<posts.length; j++) {
// Find post in local store
var id = res.data[i].id;
if (typeof res.data[i].post !== 'undefined') {
id = res.data[i].post.id;
}
if (posts[j].id == id) {
// Remove this post
posts.splice(j, 1);
break;
}
}
} else {
for (var j=0; j<posts.length; j++) {
// Find post in local store
if (posts[j].id == res.data[i].id) {
// Note the error in the local post
posts[j].error = res.data[i].error_msg;
break;
}
}
}
}
H.set('posts', JSON.stringify(posts));
$sync.innerText = 'Synced.';
}
} else {
// TODO: show error visually (option to retry)
console.error("Well that didn't work at all!");
$sync.className = '';
$sync.innerText = 'Sync...';
}
}
};
http.send(JSON.stringify(params));
});
}

// bind publish action
@@ -144,6 +221,31 @@ document.addEventListener('DOMContentLoaded', function() {
H.save(fontRadios, 'font');
};
}

var handleRegUser = function() {
var http = new XMLHttpRequest();
http.open("GET", "https://write.as/api/me/", true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
data = JSON.parse(http.responseText);
data = data.data;
if (typeof data.username !== 'undefined' && data.username != "") {
var $accTools = document.getElementById("account-tools")
$accTools.style.display = 'block';
var posts = JSON.parse(H.get('posts', '[]'));
if (posts.length > 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


Loading…
Cancel
Save