zipped

Use web worker to compress images

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greasyfork.org/scripts/20372/130355/zipped.js

  1. // https://github.com/baivong/Userscript
  2.  
  3. (function () {
  4. 'use strict';
  5.  
  6. function generateZip(index, name) {
  7.  
  8. var zip = new JSZip();
  9.  
  10. box[('item' + index)].forEach(function (imgBlob) {
  11. zip.file(imgBlob.name, imgBlob.content);
  12. });
  13.  
  14. zip.generateAsync({
  15. type: 'blob'
  16. }).then(function (blob) {
  17. self.postMessage({
  18. index: index,
  19. name: name,
  20. content: blob
  21. });
  22. }, function (reason) {
  23. self.postMessage(reason);
  24. });
  25.  
  26. }
  27.  
  28. var box = {};
  29.  
  30. self.addEventListener('message', function (e) {
  31.  
  32. var i = 'item' + e.data.index;
  33. if (!box[i]) box[i] = [];
  34.  
  35. if (e.data.run) {
  36. generateZip(e.data.index, e.data.name);
  37. } else {
  38. box[i].push({
  39. name: e.data.name,
  40. content: e.data.content
  41. });
  42. }
  43.  
  44. }, false);
  45.  
  46. })();