init.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 3.0 of the License, or (at your option) any later version.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with this library.
  12. */
  13. var http = require('http');
  14. var querystring = require('querystring');
  15. Hooks.addMenuItem("Text/Paste to Pamrel", "cmd-shift-p", function () {
  16. Recipe.run(function(recipe) {
  17. var options = {
  18. host: 'pamrel.lu',
  19. port: 80,
  20. method: 'POST',
  21. path: '/',
  22. headers: {
  23. 'Content-Type': 'application/x-www-form-urlencoded',
  24. 'Content-Length': recipe.text.length
  25. }
  26. }
  27. var request = http.request(options, function (response) {
  28. response.setEncoding('utf8');
  29. var buffer = '';
  30. response.on('data', function (chunk) {
  31. buffer += chunk;
  32. })
  33. if (response.statusCode != 200) {
  34. Alert.show('Error: ' + http.STATUS_CODES[response.statusCode] + ' (' + response.statusCode + ')');
  35. return;
  36. }
  37. response.on('end', function () {
  38. // There is a \n at the end for terminal outputs
  39. buffer = buffer.replace('\n', '')
  40. // Copy to paste buffer and tell user
  41. Clipboard.copy(buffer);
  42. Alert.beep();
  43. });
  44. });
  45. var requestBody = querystring.stringify({'content': recipe.text})
  46. request.write(requestBody);
  47. request.end();
  48. });
  49. });