diff --git a/H.js b/H.js index 911093c..6d0f65a 100644 --- a/H.js +++ b/H.js @@ -78,4 +78,22 @@ var H = { return post; }, + getTitleStrict: function(content) { + var eol = content.indexOf("\n"); + var title = ""; + var newContent = content; + if (content.indexOf("# ") === 0) { + // Title is in the format: + // # Some title + if (eol !== -1) { + // First line should start with # and end with \n + newContent = content.substring(eol).leftTrim(); + title = content.substring("# ".length, eol); + } + } + return { + title: title, + content: newContent + }; + }, }; diff --git a/README.md b/README.md index 6798533..30b9a72 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -Paste Chrome extension -====================== +Write.as Chrome extension +========================= -Paste lets you instantly share text or code from your browser. +Write.as for Chrome lets you instantly share text from your browser. -![Selecting text in Paste](https://paste.as/images/pic01.jpg) +![Selecting text in Write.as](https://paste.as/images/pic01.jpg) # License diff --git a/context.js b/context.js index 886c6d7..3510617 100644 --- a/context.js +++ b/context.js @@ -1,9 +1,14 @@ function publish(content, font) { + if (content.trim() == "") { + return; + } + + var post = H.getTitleStrict(content); var http = new XMLHttpRequest(); - var url = "https://write.as/api/"; + var url = "https://write.as/api/posts"; var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage); lang = lang.substring(0, 2); - var params = "w=" + encodeURIComponent(content) + "&font=" + font + "&lang=" + lang; + var params = "body=" + encodeURIComponent(post.content) + "&title=" + encodeURIComponent(post.title) + "&font=" + font + "&lang=" + lang + "&rtl=auto"; http.open("POST", url, true); //Send the proper header information along with the request @@ -11,18 +16,24 @@ function publish(content, font) { http.onreadystatechange = function() { if (http.readyState == 4) { - if (http.status == 200) { - data = http.responseText.split("\n"); + if (http.status == 201) { + data = JSON.parse(http.responseText); // Pull out data parts - url = data[0]; - id = url.substr(url.lastIndexOf("/")+1); - editToken = data[1]; - - // Save the data - posts = JSON.parse(H.get('posts', '[]')); - posts.push(H.createPost(id, editToken, content)); - H.set('posts', JSON.stringify(posts)); - + id = data.data.id; + if (font == 'code' || font === 'mono') { + url = "https://paste.as/"+id; + } else { + url = "https://write.as/"+id; + } + editToken = data.data.token; + + // 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/fonts.css b/fonts.css new file mode 100644 index 0000000..f09061e --- /dev/null +++ b/fonts.css @@ -0,0 +1,18 @@ +@font-face { + font-family: 'Lora'; + font-style: normal; + font-weight: 400; + src: local('Lora'), local('Lora-Regular'), url('fonts/lora.woff2') format('woff2'); +} +@font-face { + font-family: 'Lora'; + font-style: normal; + font-weight: 700; + src: local('Lora Bold'), local('Lora-Bold'), url('fonts/lora-700.woff2') format('woff2'); +} +@font-face { + font-family: 'Open Sans'; + font-style: normal; + font-weight: 400; + src: local('Open Sans'), local('OpenSans'), url('fonts/open-sans.woff2') format('woff2'); +} diff --git a/fonts/lora-700.woff2 b/fonts/lora-700.woff2 new file mode 100644 index 0000000..2eacde2 Binary files /dev/null and b/fonts/lora-700.woff2 differ diff --git a/fonts/lora.woff2 b/fonts/lora.woff2 new file mode 100644 index 0000000..a3ef64d Binary files /dev/null and b/fonts/lora.woff2 differ diff --git a/fonts/open-sans.woff2 b/fonts/open-sans.woff2 new file mode 100644 index 0000000..5287058 Binary files /dev/null and b/fonts/open-sans.woff2 differ diff --git a/img/ic_launch.svg b/img/ic_launch.svg new file mode 100644 index 0000000..ca89ba8 --- /dev/null +++ b/img/ic_launch.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/manifest.json b/manifest.json index 5cad1b8..07ca000 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,10 @@ { "manifest_version": 2, - "name": "Paste by Write.as", - "description": "Effortlessly share text online.", - "version": "1.3", + "name": "Write.as for Chrome", + "short_name": "Write.as", + "description": "Publish a thought in seconds.", + "version": "2.0", "icons": { "16": "icon16.png", @@ -28,8 +29,7 @@ "activeTab", "contextMenus", "webRequest", - "*://*.write.as/", - "https://fonts.googleapis.com/" + "*://*.write.as/" ], "externally_connectable": { "matches": ["*://*.write.as/*"] diff --git a/popup.html b/popup.html index af1e62d..723ceb3 100644 --- a/popup.html +++ b/popup.html @@ -1,7 +1,7 @@ - Paste by Write.as + Write.as @@ -83,23 +124,26 @@
- +
+ Writing as write.as. Sync... +
+
Open
- Pop out + +
- - + diff --git a/popup.js b/popup.js index 826bfce..e0cb3ca 100644 --- a/popup.js +++ b/popup.js @@ -8,14 +8,15 @@ function publish(content, font) { } $publish.classList.add('disabled'); - $publish.value = "Publishing..."; + setPublishText(font, true); $publish.disabled = true; + var post = H.getTitleStrict(content); var http = new XMLHttpRequest(); - var url = "https://write.as/api/"; + var url = "https://write.as/api/posts"; var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage); lang = lang.substring(0, 2); - var params = "w=" + encodeURIComponent(content) + "&font=" + font + "&lang=" + lang; + var params = "body=" + encodeURIComponent(post.content) + "&title=" + encodeURIComponent(post.title) + "&font=" + font + "&lang=" + lang + "&rtl=auto"; http.open("POST", url, true); //Send the proper header information along with the request @@ -24,18 +25,23 @@ function publish(content, font) { http.onreadystatechange = function() { if (http.readyState == 4) { $publish.classList.remove('disabled'); - $publish.value = "Publish"; + setPublishText(font, false); $publish.disabled = false; - if (http.status == 200) { + if (http.status == 201) { $publish.style.display = 'none'; - data = http.responseText.split("\n"); + data = JSON.parse(http.responseText); // Pull out data parts - url = data[0]; - id = url.substr(url.lastIndexOf("/")+1); - editToken = data[1]; + id = data.data.id; + if (font == 'code' || font === 'mono') { + url = "https://paste.as/"+id; + } else { + url = "https://write.as/"+id; + } + 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'; @@ -44,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."); } @@ -56,6 +64,14 @@ function publish(content, font) { http.send(params); } +function setPublishText(font, isPublishing) { + if (font === 'code' || font === 'mono') { + $publish.value = isPublishing ? 'Pasting...' : 'Paste'; + } else { + $publish.value = isPublishing ? 'Publishing...' : 'Publish'; + } +} + document.addEventListener('DOMContentLoaded', function() { $content = document.getElementById("content"); $publish = document.getElementById("publish"); @@ -99,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