Thomas Mühlbauer
Thomas Mühlbauer

Reputation: 71

Chrome extension: Get content of downloaded file

is there a option to get the content of an local (downloaded) pdf file, that is opened in google chrome?

last years it works so:

User open pdf file with google chrome. Then he clicks my extension in chrome browser. My extension "downloaded" the local pdf file to chrome downloads folder. Then my extension reads the content of the downloaded file via HTTPRequest

var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
	
	var pdfblob = (this.response);	

};

...but now since about 5 days, this will not work anymore. I get this error:

GET file:///C:/Users/Standard/Downloads/test.pdf net::ERR_UNKNOWN_URL_SCHEME

Knows someone a solution for this problem? Perhaps i can read the files content while the file gets downloaded?

Thomas

UPDATE With this Code i try at the moment:

var xhr = new XMLHttpRequest();

xhr.open('GET', 'file:///C:/Users/Standard/GoogleDrive/Dropbox/FLIESENLEGER.pdf', true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
	
	var pdfblob = (this.response);	

    console.log(pdfblob);


};

manifest.json:

{
"update_url": "https://clients2.google.com/service/update2/crx",

  "manifest_version": 2,


"permissions": [
"activeTab",
"file://*",
"http://www.pdfzorro.com/",
"https://www.pdfzorro.com/",
"https://www.google.com/",
"downloads",
"file://*"
],


  "name": "Save to Google Drive™",
  "version": "0.0.0.20",
  "short_name": "Save to a folder on Google Drive™",
  "description": "Save PDF, images or webpages - opened in Chrome -  to your Google Drive™. You can select a folder where the file should be saved.",
  "icons": { "16": "logo16.png",
          "128": "logo.png" },  
  

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }

  
}

Upvotes: 4

Views: 3620

Answers (2)

Htin Aung
Htin Aung

Reputation: 579

I have similar requirements and manage to read the downloaded files using fetch in background.js. I got CORS error in ContentScript.js. Make sure you have file://* permission in manifest file.

function getLatestDownloadBlob(filename) {
  return new Promise((resolve, reject) => {
    fetch("File://" + filename)
    .then(response => {
        if (!response.ok) {
            throw new Error(`HTTP error! Status: ${response.status}`);
        }
        resolve(response.blob());
    })
    .catch(error => {
        reject(error);
    });            
  });
}

Upvotes: 0

herodrigues
herodrigues

Reputation: 967

You have to enable access to file URLs in your extension and also add the file:/// permission in the manifest file.

Add to manifest.json

{
  "permissions": [
    // other permissions
    "file:///"
  ]
}

Go to chrome://extensions/, click on the extension details button and check the Allow access to file URLs option.

You'll have to warn your extension users to check this option!

enter image description here

In addition to this, you can use chrome.downloads to get information about downloaded files, such as filepath, size, etc.

Upvotes: 2

Related Questions