Calls to writeFile fail in Chrome 65 extensions #1051

Closed
opened 2018-03-25 02:56:27 +00:00 by atkinsam · 4 comments
atkinsam commented 2018-03-25 02:56:27 +00:00 (Migrated from github.com)

Calls to writeFile fail to initiate a download of a spreadsheet in Chrome 65 extensions. It worked as of a few days ago but recently stopped working.

Works:
Chromium 67.0.3380.0 64-bit (windows)

Does not work:
Chrome 65.0.3325.181 64-bit (Ubuntu 17.10)
Chrome 65.0.3325.181 64-bit (windows)

The problem can be replicated by creating a directory with the following files and adding the extension to Chrome by adding the folder as an unpacked extension in the Chrome extensions page. No errors show up in the development console for the extension.

manifest.json

{
  "manifest_version": 2,
  "name": "XLSX Download Test",
  "description": " ",
  "version": "0.1",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "storage"
  ]
}

popup.html

<!doctype html>
<html>
<head>
    <title>XLSX Download Test</title>
</head>
<body>
    <button type="button" id="download_button">Download Spreadsheet</button>
    <script type="text/javascript" src="popup.js"></script>
    <script lang="javascript" src="xlsx.full.min.js"></script>
</body>
</html>

popup.js

document.getElementById('download_button').addEventListener('click', function() {
    var data = [{'TestCol1': 'testA1', 'TestCol2': 'testB1'},
                {'TestCol1': 'testA2', 'TestCol2': 'testB2'}];
    var wb = XLSX.utils.book_new();
    var ws = XLSX.utils.json_to_sheet(data, {
        header: ['TestCol1', 'TestCol2']
    });
    XLSX.utils.book_append_sheet(wb, ws, 'TestSheetName');
    XLSX.writeFile(wb, 'testsheet.xlsx');
});

xlsx.full.min.js
Found here

Calls to writeFile fail to initiate a download of a spreadsheet in Chrome 65 extensions. It worked as of a few days ago but recently stopped working. Works: Chromium 67.0.3380.0 64-bit (windows) Does not work: Chrome 65.0.3325.181 64-bit (Ubuntu 17.10) Chrome 65.0.3325.181 64-bit (windows) The problem can be replicated by creating a directory with the following files and adding the extension to Chrome by adding the folder as an unpacked extension in the Chrome extensions page. No errors show up in the development console for the extension. **manifest.json** ``` { "manifest_version": 2, "name": "XLSX Download Test", "description": " ", "version": "0.1", "browser_action": { "default_popup": "popup.html" }, "permissions": [ "activeTab", "storage" ] } ``` **popup.html** ``` <!doctype html> <html> <head> <title>XLSX Download Test</title> </head> <body> <button type="button" id="download_button">Download Spreadsheet</button> <script type="text/javascript" src="popup.js"></script> <script lang="javascript" src="xlsx.full.min.js"></script> </body> </html> ``` **popup.js** ``` document.getElementById('download_button').addEventListener('click', function() { var data = [{'TestCol1': 'testA1', 'TestCol2': 'testB1'}, {'TestCol1': 'testA2', 'TestCol2': 'testB2'}]; var wb = XLSX.utils.book_new(); var ws = XLSX.utils.json_to_sheet(data, { header: ['TestCol1', 'TestCol2'] }); XLSX.utils.book_append_sheet(wb, ws, 'TestSheetName'); XLSX.writeFile(wb, 'testsheet.xlsx'); }); ``` **xlsx.full.min.js** Found [here](https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.12.6/xlsx.full.min.js)
SheetJSDev commented 2018-03-25 03:46:25 +00:00 (Migrated from github.com)

Chrome may have more stringent security restrictions compared to Chromium. The following seems to work for 65.0.3325.181 (Official Build) (64-bit) on OSX. If this works for you, we can add it to the write_dl function:

  "permissions": [
    "activeTab",
    "downloads",
    "storage"
  ]
  • generate an ArrayBuffer with the "array" type, manually generate a data URL, and use chrome.downloads.download to trigger the download:
    /* generate array buffer */
    var wbout = XLSX.write(wb, {type:"array", bookType:'xlsx'});
    /* create data URL */
    var url = URL.createObjectURL(new Blob([wbout], {type: 'application/octet-stream'}));
    /* trigger download with chrome API */
    chrome.downloads.download({ url: url, filename: "testsheet.xlsx", saveAs: true });
Chrome may have more stringent security restrictions compared to Chromium. The following seems to work for 65.0.3325.181 (Official Build) (64-bit) on OSX. If this works for you, we can add it to the [`write_dl` function](https://github.com/SheetJS/js-xlsx/blob/master/bits/19_fsutils.js#L10): - add the [`downloads` permission](https://developers.chrome.com/extensions/downloads) in `manifest.json`: ```js "permissions": [ "activeTab", "downloads", "storage" ] ``` - generate an `ArrayBuffer` with the `"array"` type, manually generate a data URL, and use [`chrome.downloads.download`](https://developers.chrome.com/extensions/downloads#method-download) to trigger the download: ```js /* generate array buffer */ var wbout = XLSX.write(wb, {type:"array", bookType:'xlsx'}); /* create data URL */ var url = URL.createObjectURL(new Blob([wbout], {type: 'application/octet-stream'})); /* trigger download with chrome API */ chrome.downloads.download({ url: url, filename: "testsheet.xlsx", saveAs: true }); ```
atkinsam commented 2018-03-25 03:55:22 +00:00 (Migrated from github.com)

Your solution works for me on the same version of Chrome in Windows and Ubuntu. Thank you!

Your solution works for me on the same version of Chrome in Windows and Ubuntu. Thank you!
SheetJSDev commented 2018-03-29 04:37:24 +00:00 (Migrated from github.com)

The logic has been added to writeFile so it should just work now in a Chrome extension. There is a new Chrome/Chromium demo showing how to export bookmarks from a popup and tables using content script + event page

The logic has been added to `writeFile` so it should just work now in a Chrome extension. There is a new [Chrome/Chromium demo](https://github.com/SheetJS/js-xlsx/tree/master/demos/chrome) showing how to export bookmarks from a popup and tables using content script + event page
Mithuna-01 commented 2021-01-11 13:47:14 +00:00 (Migrated from github.com)

I tried to add "permissions" in manifest.json file. but i get an error when i do 'gulp serve'.
The error is as follows :
Error - [write-manifests] Manifest validation error
(#/) Data does not match any schemas from 'oneOf'
(#/) Missing required property: description
(#/) Missing required property: extensionType
(#/componentType) No enum match for: WebPart
(#/) Additional properties not allowed: permissions
(#/) Missing required property: items
(#/) Missing required property: rootComponentId

could anyone provide a solution

I tried to add "permissions" in manifest.json file. but i get an error when i do 'gulp serve'. The error is as follows : Error - [write-manifests] Manifest validation error (#/) Data does not match any schemas from 'oneOf' (#/) Missing required property: description (#/) Missing required property: extensionType (#/componentType) No enum match for: WebPart (#/) Additional properties not allowed: permissions (#/) Missing required property: items (#/) Missing required property: rootComponentId could anyone provide a solution
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#1051
No description provided.