ext-webappfind.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*globals svgEditor*/
  2. /*
  3. Depends on Firefox add-on and executables from https://github.com/brettz9/webappfind
  4. Todos:
  5. 1. See WebAppFind Readme for SVG-related todos
  6. */
  7. (function () {'use strict';
  8. var pathID,
  9. saveMessage = 'webapp-save',
  10. readMessage = 'webapp-read',
  11. excludedMessages = [readMessage, saveMessage];
  12. window.addEventListener('message', function(e) {
  13. if (e.origin !== window.location.origin || // PRIVACY AND SECURITY! (for viewing and saving, respectively)
  14. (!Array.isArray(e.data) || excludedMessages.indexOf(e.data[0]) > -1) // Validate format and avoid our post below
  15. ) {
  16. return;
  17. }
  18. var svgString,
  19. messageType = e.data[0];
  20. switch (messageType) {
  21. case 'webapp-view':
  22. // Populate the contents
  23. pathID = e.data[1];
  24. svgString = e.data[2];
  25. svgEditor.loadFromString(svgString);
  26. /*if ($('#tool_save_file')) {
  27. $('#tool_save_file').disabled = false;
  28. }*/
  29. break;
  30. case 'webapp-save-end':
  31. alert('save complete for pathID ' + e.data[1] + '!');
  32. break;
  33. default:
  34. throw 'Unexpected mode';
  35. }
  36. }, false);
  37. window.postMessage([readMessage], window.location.origin !== 'null' ? window.location.origin : '*'); // Avoid "null" string error for file: protocol (even though file protocol not currently supported by add-on)
  38. svgEditor.addExtension('WebAppFind', function() {
  39. return {
  40. name: 'WebAppFind',
  41. svgicons: svgEditor.curConfig.extPath + 'webappfind-icon.svg',
  42. buttons: [{
  43. id: 'webappfind_save', //
  44. type: 'app_menu',
  45. title: 'Save Image back to Disk',
  46. position: 4, // Before 0-based index position 4 (after the regular "Save Image (S)")
  47. events: {
  48. click: function () {
  49. if (!pathID) { // Not ready yet as haven't received first payload
  50. return;
  51. }
  52. window.postMessage([saveMessage, pathID, svgEditor.canvas.getSvgString()], window.location.origin);
  53. }
  54. }
  55. }]
  56. };
  57. });
  58. }());