commit f6f7a8dd54759c08da8466846ed42db0855a1d11 Author: Matt Baer Date: Fri May 1 23:34:24 2015 -0400 Paste by Write.as diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..3322130 Binary files /dev/null and b/icon.png differ diff --git a/icon128.png b/icon128.png new file mode 100644 index 0000000..acdd087 Binary files /dev/null and b/icon128.png differ diff --git a/icon48.png b/icon48.png new file mode 100644 index 0000000..9a433db Binary files /dev/null and b/icon48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..f982b21 --- /dev/null +++ b/manifest.json @@ -0,0 +1,23 @@ +{ + "manifest_version": 2, + + "name": "Paste by Write.as", + "description": "Effortlessly share text online.", + "version": "1.0", + + "icons": { + "48": "icon48.png", + "128": "icon128.png" + }, + "browser_action": { + "default_icon": "icon.png", + "default_popup": "popup.html", + "default_title": "Publish this" + }, + "permissions": [ + "activeTab", + "webRequest", + "*://*.write.as/", + "https://fonts.googleapis.com/" + ] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..90c7f6e --- /dev/null +++ b/popup.html @@ -0,0 +1,84 @@ + + + + Paste by Write.as + + + + + +
+ +
+ + Open +
+
+ + + + + +
+
+ + + + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..45c919c --- /dev/null +++ b/popup.js @@ -0,0 +1,115 @@ +var $content; +var $publish; +var $url; + +function publish(content, font) { + if (content.trim() == "") { + return; + } + + $publish.classList.add('disabled'); + $publish.value = "Publishing..."; + $publish.disabled = true; + + var http = new XMLHttpRequest(); + var url = "https://write.as/api/"; + var params = "w=" + encodeURIComponent(content) + "&font=" + font; + http.open("POST", url, true); + + //Send the proper header information along with the request + http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + http.setRequestHeader("User-Agent", "Write.as Chrome Extension v1.0"); + http.setRequestHeader("Content-length", params.length); + http.setRequestHeader("Connection", "close"); + + http.onreadystatechange = function() { //Call a function when the state changes. + if (http.readyState == 4) { + $publish.classList.remove('disabled'); + $publish.value = "Publish"; + $publish.disabled = false; + + if (http.status == 200) { + $publish.style.display = 'none'; + + data = http.responseText.split("\n"); + // Pull out data parts + url = data[0]; + id = url.substr(url.lastIndexOf("/")+1); + editToken = data[1]; + + document.getElementById("publish-holder").style.display = 'none'; + document.getElementById("result-holder").style.display = 'inline'; + + $url = document.getElementById("url"); + $url.value = url; + var $urlLink = document.getElementById("url-link"); + $urlLink.href = url; + + console.log("writeas add " + id + " " + editToken); + } else { + alert("Failed to post. Please try again."); + } + } + } + http.send(params); +} + +var H = { + save: function($el, key) { + localStorage.setItem(key, $el.value); + }, + load: function($el, key) { + $el.value = localStorage.getItem(key); + }, +}; + +document.addEventListener('DOMContentLoaded', function() { + $content = document.getElementById("content"); + $publish = document.getElementById("publish"); + $url = document.getElementById("url"); + var fontRadios = document.postForm.font; + + chrome.tabs.executeScript({ + code: "window.getSelection().toString();" + }, function(selection) { + if (typeof selection !== 'undefined') { + $content.value = selection[0]; + } + // load previous draft + if ($content.value == "") { + H.load($content, 'ext-draft'); + } + }); + + // focus on the paste field + $content.focus(); + + // bind publish action + $publish.addEventListener('click', function(e) { + e.preventDefault(); + publish($content.value, fontRadios.value); + }); + $content.addEventListener('keydown', function(e){ + if (e.ctrlKey && e.keyCode == 13) { + e.preventDefault(); + publish($content.value, fontRadios.value); + } + }); + + // bind URL select-all action + $url.addEventListener('focus', function(e) { + e.preventDefault(); + this.select(); + }); + + // load font setting + H.load(fontRadios, 'font'); + $content.className = fontRadios.value; + // bind font changing action + for(var i = 0; i < fontRadios.length; i++) { + fontRadios[i].onclick = function() { + $content.className = this.value; + H.save(fontRadios, 'font'); + }; + } +});