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.
 
 
 

116 lines
3.0 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 http = new XMLHttpRequest();
  12. var url = "https://write.as/api/";
  13. var params = "w=" + encodeURIComponent(content) + "&font=" + font;
  14. http.open("POST", url, true);
  15. //Send the proper header information along with the request
  16. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  17. http.setRequestHeader("User-Agent", "Write.as Chrome Extension v1.0");
  18. http.setRequestHeader("Content-length", params.length);
  19. http.setRequestHeader("Connection", "close");
  20. http.onreadystatechange = function() { //Call a function when the state changes.
  21. if (http.readyState == 4) {
  22. $publish.classList.remove('disabled');
  23. $publish.value = "Publish";
  24. $publish.disabled = false;
  25. if (http.status == 200) {
  26. $publish.style.display = 'none';
  27. data = http.responseText.split("\n");
  28. // Pull out data parts
  29. url = data[0];
  30. id = url.substr(url.lastIndexOf("/")+1);
  31. editToken = data[1];
  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. console.log("writeas add " + id + " " + editToken);
  39. } else {
  40. alert("Failed to post. Please try again.");
  41. }
  42. }
  43. }
  44. http.send(params);
  45. }
  46. var H = {
  47. save: function($el, key) {
  48. localStorage.setItem(key, $el.value);
  49. },
  50. load: function($el, key) {
  51. $el.value = localStorage.getItem(key);
  52. },
  53. };
  54. document.addEventListener('DOMContentLoaded', function() {
  55. $content = document.getElementById("content");
  56. $publish = document.getElementById("publish");
  57. $url = document.getElementById("url");
  58. var fontRadios = document.postForm.font;
  59. chrome.tabs.executeScript({
  60. code: "window.getSelection().toString();"
  61. }, function(selection) {
  62. if (typeof selection !== 'undefined') {
  63. $content.value = selection[0];
  64. }
  65. // load previous draft
  66. if ($content.value == "") {
  67. H.load($content, 'ext-draft');
  68. }
  69. });
  70. // focus on the paste field
  71. $content.focus();
  72. // bind publish action
  73. $publish.addEventListener('click', function(e) {
  74. e.preventDefault();
  75. publish($content.value, fontRadios.value);
  76. });
  77. $content.addEventListener('keydown', function(e){
  78. if (e.ctrlKey && e.keyCode == 13) {
  79. e.preventDefault();
  80. publish($content.value, fontRadios.value);
  81. }
  82. });
  83. // bind URL select-all action
  84. $url.addEventListener('focus', function(e) {
  85. e.preventDefault();
  86. this.select();
  87. });
  88. // load font setting
  89. H.load(fontRadios, 'font');
  90. $content.className = fontRadios.value;
  91. // bind font changing action
  92. for(var i = 0; i < fontRadios.length; i++) {
  93. fontRadios[i].onclick = function() {
  94. $content.className = this.value;
  95. H.save(fontRadios, 'font');
  96. };
  97. }
  98. });