pre.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Module.canvas = document.getElementById("canvas");
  2. if(Module.canvas == undefined) Module.canvas = document.getElementByTagName("canvas");
  3. if(Module.canvas == undefined) alert("No canvas?");
  4. Module.canvas.addEventListener("contextmenu", function() {event.preventDefault()});
  5. Module.canvas.addEventListener("dragover", function() {event.preventDefault()});
  6. Module.canvas.addEventListener("drop", function() {meg4_openfile(event)});
  7. var tag = document.createElement("INPUT");
  8. tag.setAttribute("id", "meg4_upload");
  9. tag.setAttribute("type", "file");
  10. tag.setAttribute("style", "display:none");
  11. tag.addEventListener("change", function() {
  12. inp = document.getElementById("meg4_upload");
  13. if(inp.files.length>0) {
  14. var reader = new FileReader();
  15. reader.onloadend = function() { meg4_execute(inp.files[0].toString(), new Uint8Array(reader.result)); };
  16. reader.readAsArrayBuffer(inp.files[0]);
  17. }
  18. });
  19. document.body.appendChild(tag);
  20. tag = document.createElement("A");
  21. tag.setAttribute("id", "meg4_download");
  22. tag.setAttribute("style", "display:none");
  23. tag.setAttribute("download", "");
  24. document.body.appendChild(tag);
  25. function meg4_execute(name, data) {
  26. var base = name.split('/').pop();
  27. var file = new TextEncoder("utf-8").encode(base == undefined ? name : base);
  28. if(data != undefined && data.length > 0) {
  29. var meg4 = Module.cwrap("meg4_insert", "number", [ "number", "number", "number" ]);
  30. const fn = Module._malloc(file.length);
  31. const buf = Module._malloc(data.length);
  32. Module.HEAPU8.set(file, fn);
  33. Module.HEAPU8.set(data, buf);
  34. if(meg4 != undefined) meg4(fn, buf, data.length);
  35. Module._free(buf);
  36. Module._free(fn);
  37. }
  38. }
  39. function meg4_openfile(e) {
  40. e.stopPropagation();
  41. e.preventDefault();
  42. var floppy = e.dataTransfer.getData("url");
  43. if(floppy == undefined || floppy == "")
  44. floppy = e.dataTransfer.getData("src");
  45. if(floppy == undefined || floppy == "")
  46. floppy = new DOMParser().parseFromString(e.dataTransfer.getData('text/html'), "text/html").querySelector('img').src;
  47. if(floppy != undefined && floppy != "") {
  48. fetch(floppy).then((response) => {
  49. if(response.ok) response.arrayBuffer().then((buf) => { meg4_execute(floppy, new Uint8Array(buf)); });
  50. })
  51. .catch((error) => {
  52. var reader = new FileReader();
  53. reader.onloadend = function() { meg4_execute(floppy, new Uint8Array(reader.result)); };
  54. reader.readAsArrayBuffer(floppy);
  55. });
  56. }
  57. }
  58. function meg4_savefile(fn, fnlen, buf, len) {
  59. const fview = new Uint8Array(Module.HEAPU8.buffer,fn,fnlen);
  60. const bview = new Uint8Array(Module.HEAPU8.buffer,buf,len);
  61. var name = new TextDecoder("utf-8").decode(fview);
  62. var blob = new Blob([bview], { type: "application/octet-stream" });
  63. var url = window.URL.createObjectURL(blob);
  64. var a = document.getElementById('meg4_download');
  65. a.setAttribute('href',url);
  66. a.setAttribute('download', name != undefined ? name : "noname.png");
  67. a.click();
  68. window.URL.revokeObjectURL(url);
  69. }