I'm currently creating an extension for google chrome which can save all images or links to images on the harddrive.
The problem is I don't know how to save file on disk with JS or with Google Chrome Extension API.
Have you got an idea ?
When extensions are installed into Chrome they are extracted into the C:\Users\[login_name]\AppData\Local\Google\Chrome\User Data\Default\Extensions folder. Each extension will be stored in its own folder named after the ID of the extension.
Right-click the small Google Drive icon to the right of your Chrome address bar and select Options to configure the extension. From the Options page, you can set the following: The Save To location for Google Drive (it will default to the root of your Google Drive)
One way to share data between a background script and the other scripts that make up the extension is to save the data to a location which is accessible to all the scripts in the extension. We can use the browser localStorage API or chrome. storage which is specific to Chrome extensions.
1) Select the tab you want to auto save (only 1 tab can be autosaved), open the Autosave extension, click + and it will show the selected tab. 2) Choose a sub-directory from the Google Downloads directory (only Downloads and sub-directories can be saved to with a Chrome extension). Leave blank to save to Downloads.
You can use HTML5 FileSystem features to write to disk using the Download API. That is the only way to download files to disk and it is limited.
You could take a look at NPAPI plugin. Another way to do what you need is simply send a request to an external website via XHR POST and then another GET request to retrieve the file back which will appear as a save file dialog.
For example, for my browser extension My Hangouts I created a utility to download a photo from HTML5 Canvas directly to disk. You can take a look at the code here capture_gallery_downloader.js the code that does that is:
var url = window.webkitURL || window.URL || window.mozURL || window.msURL; var a = document.createElement('a'); a.download = 'MyHangouts-MomentCapture.jpg'; a.href = url.createObjectURL(dataURIToBlob(data.active, 'jpg')); a.textContent = 'Click here to download!'; a.dataset.downloadurl = ['jpg', a.download, a.href].join(':');
If you would like the implementation of converting a URI to a Blob in HTML5 here is how I did it:
/** * Converts the Data Image URI to a Blob. * * @param {string} dataURI base64 data image URI. * @param {string} mimetype the image mimetype. */ var dataURIToBlob = function(dataURI, mimetype) { var BASE64_MARKER = ';base64,'; var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length; var base64 = dataURI.substring(base64Index); var raw = window.atob(base64); var rawLength = raw.length; var uInt8Array = new Uint8Array(rawLength); for (var i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } var bb = new this.BlobBuilder(); bb.append(uInt8Array.buffer); return bb.getBlob(mimetype); };
Then after the user clicks on the download button, it will use the "download" HTML5 File API to download the blob URI into a file.
I had long been wishing to make a chrome extension for myself to batch download images. Yet every time I got frustrated because the only seemingly applicable option is NPAPI, which both chrome and firefox seem to have not desire in supporting any longer.
I suggest those who still wanted to implement 'save-file-on-disk' functionality to have a look at this Stackoverflow post, the comment below this post help me a lot.
Now since chrome 31+, the chrome.downloads
API became stable. We can use it to programmatically download file. If the user didn't set the ask me before every download
advance option in chrome setting, we can save file without prompting user to confirm!
Here is what I use (at extension's background page):
// remember to add "permissions": ["downloads"] to manifest.json // this snippet is inside a onMessage() listener function var imgurl = "https://www.google.com.hk/images/srpr/logo11w.png"; chrome.downloads.download({url:imgurl},function(downloadId){ console.log("download begin, the downId is:" + downloadId); });
Though it's a pity that chrome still doesn't provide an Event
when the download completes.chrome.downloads.download
's callback function is called when the download begin
successfully (not on completed)
The Official documentation about chrome.downloads
is here.
It's not my original idea about the solution, but I posted here hoping that it may be of some use to someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With