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.
 
 
 

106 lines
3.2 KiB

  1. function publish(content, font) {
  2. if (content.trim() == "") {
  3. return;
  4. }
  5. var post = H.getTitleStrict(content);
  6. var http = new XMLHttpRequest();
  7. var url = "https://write.as/api/posts";
  8. var lang = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
  9. lang = lang.substring(0, 2);
  10. var params = "body=" + encodeURIComponent(post.content) + "&title=" + encodeURIComponent(post.title) + "&font=" + font + "&lang=" + lang + "&rtl=auto";
  11. http.open("POST", url, true);
  12. //Send the proper header information along with the request
  13. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  14. http.onreadystatechange = function() {
  15. if (http.readyState == 4) {
  16. if (http.status == 201) {
  17. data = JSON.parse(http.responseText);
  18. // Pull out data parts
  19. id = data.data.id;
  20. if (font == 'code' || font === 'mono') {
  21. url = "https://paste.as/"+id;
  22. } else {
  23. url = "https://write.as/"+id;
  24. }
  25. editToken = data.data.token;
  26. // Save the data if user wasn't logged in
  27. if (typeof data.data.owner === 'undefined' || data.data.owner == "") {
  28. posts = JSON.parse(H.get('posts', '[]'));
  29. posts.push(H.createPost(id, editToken, post.content));
  30. H.set('posts', JSON.stringify(posts));
  31. }
  32. // Launch post
  33. chrome.tabs.create({ url: url });
  34. } else {
  35. alert("Failed to post. Please try again.");
  36. }
  37. }
  38. }
  39. http.send(params);
  40. }
  41. function getSelectedText(callback) {
  42. // Workaround since info.selectionText in context menu click handler doesn't
  43. // preserve newlines.
  44. // Source: https://code.google.com/p/chromium/issues/detail?id=116429#c11
  45. chrome.tabs.executeScript({
  46. code: "window.getSelection().toString();"
  47. }, function(selection) {
  48. callback(selection[0]);
  49. });
  50. }
  51. chrome.contextMenus.create({"title": "Publish text (sans)", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
  52. getSelectedText(function(sel) {
  53. publish(sel, "sans");
  54. });
  55. }
  56. });
  57. chrome.contextMenus.create({"title": "Publish text (serif)", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
  58. getSelectedText(function(sel) {
  59. publish(sel, "norm");
  60. });
  61. }
  62. });
  63. chrome.contextMenus.create({"title": "Publish code", "contexts": ["selection", "editable", "link"], "onclick": function(info, tab) {
  64. getSelectedText(function(sel) {
  65. publish(sel, "code");
  66. });
  67. }
  68. });
  69. chrome.runtime.onMessageExternal.addListener(function(req, sender, callback) {
  70. if (req) {
  71. if (req.msg) {
  72. if (req.msg == "ping") {
  73. callback("pong");
  74. } else if (req.msg == "posts") {
  75. callback(JSON.parse(H.get('posts', '[]')));
  76. } else if (req.msg == "deletePosts" && req.data && req.data.length > 0) {
  77. // Delete all posts listed in req.data, an array of post IDs.
  78. var posts = JSON.parse(H.get('posts', '[]'));
  79. var exportedPosts = [];
  80. for (var i=0; i<req.data.length; i++) {
  81. for (var j=0; j<posts.length; j++) {
  82. if (posts[j].id === req.data[i]) {
  83. console.log("Removing post " + req.data[i]);
  84. exportedPosts.push(posts.splice(j, 1));
  85. }
  86. }
  87. }
  88. if (exportedPosts.length > 0) {
  89. H.set('posts', JSON.stringify(posts));
  90. H.set('exportedPosts', JSON.stringify(exportedPosts));
  91. }
  92. }
  93. }
  94. }
  95. return true;
  96. });