exportaImagenes.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. function imgCSS(ed) {
  7. var code = ed.getCss(), imags;
  8. local = document.location.host.replace(/[-\/\\^$*+?.()|[\]{}]/g,'\\$&');
  9. regex = new RegExp('(http:|https:){0,1}\/\/'+local,'g');
  10. code = code.replace(regex,''); //limpiar url locales
  11. code = code.replace(/(http:|https:){0,1}\/\//g,'#'); //marcar url no locales
  12. imags = code.match(/\("{0,1}[^#^\(]+?\.(gif|jpg|png|jpeg|tif|tiff|webp|svg|ico)"{0,1}\)/gi);
  13. if (imags !== null){
  14. imags =imags.map(function(x){ return x.replace(/\("{0,1}(.*){0,1}"\)/,'$1');});
  15. }
  16. else
  17. imags =[];
  18. return imags;
  19. }
  20. /*
  21. get image src from HTML file
  22. just local urls
  23. */
  24. function imgHTML(){
  25. var imags = [];
  26. var src;
  27. var code = document.querySelector('.gjs-frame').contentDocument;
  28. code = code.getElementsByTagName('img');
  29. for (i = 0; i < code.length; i++){
  30. src = code[i].src;
  31. src = src.replace(location.origin,'');
  32. if (!src.includes('http')){
  33. imags.push(src);
  34. }
  35. };
  36. imags.push('img/pleca.webp'); //obtiene imagen de la pleca cartel/ en pruebas
  37. return imags;
  38. }
  39. /**
  40. Convierte a binario los datos devueltos en la lectura
  41. **/
  42. function arrayBufferToBinary(buffer) {
  43. var binary = '';
  44. var bytes = [].slice.call(new Uint8Array(buffer));
  45. bytes.forEach((b) => binary += String.fromCharCode(b));
  46. return binary;
  47. };
  48. /*
  49. Leer los archivos de imagen
  50. param: ed el editor
  51. */
  52. async function readImgs(ed){
  53. var ImgData;
  54. var listaImgs, imagsSet;
  55. var peticion, archivo;
  56. var content=[];
  57. listaImgs = imgHTML(ed).concat(imgCSS(ed));
  58. imagsSet = new Set(listaImgs);
  59. listaImgs = [...imagsSet];
  60. for (var i = 0; i<listaImgs.length; i++){
  61. try{
  62. peticion = await fetch(listaImgs[i]);
  63. imgData = await peticion.arrayBuffer();
  64. archivo = peticion.url.match(/[^\/\.]*\.[^\.]*$/, '$&')[0];
  65. content[archivo] = arrayBufferToBinary(imgData);;
  66. }
  67. catch(e){
  68. console.log("error "+e.message);
  69. }
  70. };
  71. return content;
  72. }