A Chrome extension for Write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

184 lines
5.1 KiB

  1. var $content;
  2. var $publish;
  3. var $url;
  4. function publish(content, font) {
  5. if (content.trim() == "") {
  6. return;
  7. }
  8. $publish.classList.add('disabled');
  9. $publish.value = "Publishing...";
  10. $publish.disabled = true;
  11. var post = H.getTitleStrict(content);
  12. var http = new XMLHttpRequest();
  13. var url = "https://write.as/api/posts";
  14. var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
  15. lang = lang.substring(0, 2);
  16. var params = "body=" + encodeURIComponent(post.content) + "&title=" + encodeURIComponent(post.title) + "&font=" + font + "&lang=" + lang + "&rtl=auto";
  17. http.open("POST", url, true);
  18. //Send the proper header information along with the request
  19. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  20. http.onreadystatechange = function() {
  21. if (http.readyState == 4) {
  22. $publish.classList.remove('disabled');
  23. $publish.value = "Publish";
  24. $publish.disabled = false;
  25. if (http.status == 201) {
  26. $publish.style.display = 'none';
  27. data = JSON.parse(http.responseText);
  28. // Pull out data parts
  29. id = data.data.id;
  30. url = "https://write.as/"+id;
  31. editToken = data.data.token;
  32. document.getElementById("publish-holder").style.display = 'none';
  33. document.getElementById("result-holder").style.display = 'inline';
  34. $url = document.getElementById("url");
  35. $url.value = url;
  36. var $urlLink = document.getElementById("url-link");
  37. $urlLink.href = url;
  38. // Save the data
  39. posts = JSON.parse(H.get('posts', '[]'));
  40. posts.push(H.createPost(id, editToken, content));
  41. H.set('posts', JSON.stringify(posts));
  42. } else {
  43. alert("Failed to post. Please try again.");
  44. }
  45. }
  46. }
  47. http.send(params);
  48. }
  49. document.addEventListener('DOMContentLoaded', function() {
  50. $content = document.getElementById("content");
  51. $publish = document.getElementById("publish");
  52. $url = document.getElementById("url");
  53. var fontRadios = document.postForm.font;
  54. var isPopout = window.location.search.substring(1) == "popout";
  55. if (isPopout) {
  56. chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  57. $content.value = request.msg;
  58. });
  59. }
  60. chrome.tabs.executeScript({
  61. code: "window.getSelection().toString();"
  62. }, function(selection) {
  63. if (typeof selection !== 'undefined') {
  64. $content.value = selection[0];
  65. }
  66. // load previous draft
  67. if ($content.value == "") {
  68. H.load($content, 'ext-draft');
  69. }
  70. });
  71. // focus on the paste field
  72. $content.focus();
  73. if (isPopout) {
  74. document.body.className = 'popout';
  75. } else {
  76. document.getElementById('popout').addEventListener('click', function(e) {
  77. e.preventDefault();
  78. chrome.windows.create({
  79. url: "popup.html?popout",
  80. width: 640,
  81. height: 400,
  82. focused: true,
  83. type: "popup"
  84. }, function(window) {
  85. chrome.runtime.sendMessage({msg: $content.value});
  86. });
  87. });
  88. }
  89. // bind publish action
  90. $publish.addEventListener('click', function(e) {
  91. e.preventDefault();
  92. publish($content.value, fontRadios.value);
  93. });
  94. $content.addEventListener('keydown', function(e){
  95. if (e.ctrlKey && e.keyCode == 13) {
  96. e.preventDefault();
  97. publish($content.value, fontRadios.value);
  98. }
  99. });
  100. // bind URL select-all action
  101. $url.addEventListener('focus', function(e) {
  102. e.preventDefault();
  103. this.select();
  104. });
  105. // load font setting
  106. H.load(fontRadios, 'font');
  107. $content.className = fontRadios.value;
  108. // bind font changing action
  109. for(var i = 0; i < fontRadios.length; i++) {
  110. fontRadios[i].onclick = function() {
  111. $content.className = this.value;
  112. H.save(fontRadios, 'font');
  113. };
  114. }
  115. if (H.get('updatedPostsMeta', '') == '') {
  116. // Add metadata used by Pad to all saved posts
  117. var addPostMetaData = function() {
  118. console.log('Adding post meta data...');
  119. var fetch = function(id, token, callback) {
  120. var http = new XMLHttpRequest();
  121. http.open("GET", "https://write.as/api/" + id + "?created=1&t=" + token, true);
  122. http.onreadystatechange = function() {
  123. if (http.readyState == 4) {
  124. callback(http.responseText, http.status);
  125. }
  126. }
  127. http.send();
  128. }
  129. var posts = JSON.parse(H.get('posts', '[]'));
  130. var migratedPosts = [];
  131. var failedPosts = [];
  132. if (posts.length > 0) {
  133. var i = 0;
  134. var updateMeta = function(content, status) {
  135. if (status == 200) {
  136. data = content.split("\n\n");
  137. created = data.splice(0, 1);
  138. migratedPosts.push(H.createPost(posts[i].id, posts[i].token, data.join("\n\n"), created));
  139. } else {
  140. posts[i].reason = status;
  141. failedPosts.push(posts[i]);
  142. }
  143. i++;
  144. if (i < posts.length) {
  145. fetch(posts[i].id, posts[i].token, updateMeta);
  146. } else {
  147. console.log('Finished! Success: ' + migratedPosts.length + '. Fail: ' + failedPosts.length);
  148. if (failedPosts.length > 0) {
  149. H.set('failedMigrationPosts', JSON.stringify(failedPosts));
  150. }
  151. H.set('posts', JSON.stringify(migratedPosts));
  152. H.set('updatedPostsMeta', 'yes');
  153. }
  154. };
  155. fetch(posts[i].id, posts[i].token, updateMeta);
  156. } else {
  157. H.set('updatedPostsMeta', 'yes');
  158. }
  159. };
  160. addPostMetaData();
  161. }
  162. });