A Chrome extension for Write.as
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

126 satır
3.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 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.onreadystatechange = function() {
  18. if (http.readyState == 4) {
  19. $publish.classList.remove('disabled');
  20. $publish.value = "Publish";
  21. $publish.disabled = false;
  22. if (http.status == 200) {
  23. $publish.style.display = 'none';
  24. data = http.responseText.split("\n");
  25. // Pull out data parts
  26. url = data[0];
  27. id = url.substr(url.lastIndexOf("/")+1);
  28. editToken = data[1];
  29. document.getElementById("publish-holder").style.display = 'none';
  30. document.getElementById("result-holder").style.display = 'inline';
  31. $url = document.getElementById("url");
  32. $url.value = url;
  33. var $urlLink = document.getElementById("url-link");
  34. $urlLink.href = url;
  35. // Save the data
  36. posts = JSON.parse(H.get('posts', '[]'));
  37. posts.push({id: id, token: editToken});
  38. H.set('posts', JSON.stringify(posts));
  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. this.set(key, $el.value);
  49. },
  50. load: function($el, key) {
  51. $el.value = this.get(key, "");
  52. },
  53. set: function(key, value) {
  54. localStorage.setItem(key, value);
  55. },
  56. get: function(key, defaultValue) {
  57. var val = localStorage.getItem(key);
  58. if (val == null) {
  59. val = defaultValue;
  60. }
  61. return val;
  62. },
  63. };
  64. document.addEventListener('DOMContentLoaded', function() {
  65. $content = document.getElementById("content");
  66. $publish = document.getElementById("publish");
  67. $url = document.getElementById("url");
  68. var fontRadios = document.postForm.font;
  69. chrome.tabs.executeScript({
  70. code: "window.getSelection().toString();"
  71. }, function(selection) {
  72. if (typeof selection !== 'undefined') {
  73. $content.value = selection[0];
  74. }
  75. // load previous draft
  76. if ($content.value == "") {
  77. H.load($content, 'ext-draft');
  78. }
  79. });
  80. // focus on the paste field
  81. $content.focus();
  82. // bind publish action
  83. $publish.addEventListener('click', function(e) {
  84. e.preventDefault();
  85. publish($content.value, fontRadios.value);
  86. });
  87. $content.addEventListener('keydown', function(e){
  88. if (e.ctrlKey && e.keyCode == 13) {
  89. e.preventDefault();
  90. publish($content.value, fontRadios.value);
  91. }
  92. });
  93. // bind URL select-all action
  94. $url.addEventListener('focus', function(e) {
  95. e.preventDefault();
  96. this.select();
  97. });
  98. // load font setting
  99. H.load(fontRadios, 'font');
  100. $content.className = fontRadios.value;
  101. // bind font changing action
  102. for(var i = 0; i < fontRadios.length; i++) {
  103. fontRadios[i].onclick = function() {
  104. $content.className = this.value;
  105. H.save(fontRadios, 'font');
  106. };
  107. }
  108. });