Jszip: Opening zip in extension's content script fails.

Created on 27 Dec 2018  路  2Comments  路  Source: Stuk/jszip

Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ? jszip.js:3483:17

My goal was to get image files from a zip file (with ) and appendChild them inside the < a > element.

As a copy and paste into console, everything works fine.
As a content script of an extension, jszip gives the above error.

manifest.json

{
  "author": "[email protected]",
  "manifest_version": 2,
  "name": "debug jszip",
  "version": "1.0",
  "description": "fetch-zip => jszip-open => fails",
  "permissions": ["file:///*"],
  "content_scripts": [
    {"matches": ["file:///*"],
      "js": ["jszip.js","debug.js"]
    }
  ]
}

content script debug.js

async function fetchAsync (url) {
        console.log('url:'+url)
    let response = await fetch(url);
        console.log('response status:'+response.status)
    let ffile = await response.blob();
        console.log('file object type:'+ffile.type)
    let unzip = await JSZip.loadAsync(ffile)  //it fails here!
        console.log('unzip script:'+unzip.toString())
}

window.addEventListener("contextmenu", function(e){
    if(e.ctrlKey && e.button ==2){
            fetchAsync(e.target.href)   
    };
});

Some frustrating things I learned.

  • about:debugging is how to load temporary add-ons in Firefox 64.
  • Use ctrl+shift+j, content scripts show things in this console.
  • console.log in content scripts is frustrating.
  • placing var dataType = "arraybuffer"; at jszip.js:3480 was my best attempt at happiness.

All 2 comments

I got it to work by changing
reader.readAsArrayBuffer(data);
to
reader.readAsBinaryString(data);

Where is your "reader"?

Was this page helpful?
0 / 5 - 0 ratings