I'm writing a chrome extension which will add custom images to a webpage. I want the users to put all their images in the "backgrounds" folder, and I want my extension to be able to retrieve the file names of every file in the "backgrounds" directory in the extensions folder. It seems like there is no way to do this in chrome extensions. When I try to use the chrome fileSystem API, I get this error:
There were warnings when trying to install this extension:
'fileSystem' is only allowed for packaged apps, but this is a extension.
How can I do this?
You can get read-only access to your extension's folder with chrome.runtime.getPackageDirectoryEntry, with which you can work using the HTML5 FileSystem API.
However, this will not allow you to do what you want to do.
While you're developing an extension, it will work fine, as Chrome does not mind changes to the extension's folder - they are expected.
However, when the extension is deployed to users, Chrome will maintain a cryptographic hash of the extension folder's contents. In case there are any external modification to the files, the extension is considered compromised and is forcibly disabled.
So you should consider other approaches instead, such as:
using the above HTML5 FileSystem API to have a virtual persistent filesystem to which you can let the user "upload" files through your UI;
storing data as blob:/data: URIs in chrome.storage or IndexedDB;
asking the user to put the files in a cloud drive your extension can access using its usual API.
Related
I'm wondering if it's possible to for certain JS files to be added to the web extension directory later?
Like say I have an app where users can select certain settings from within the app and those files (js and html files, images or blobs) are somehow added into the extension from the web. Like some sort of ondemand updater without using any native apps but it seems that upgrades are done by the appstores automatically.
I'm reading the files using ajax and adding them to indexeddb but because it could be more than one file that's getting messy.
Say a user wants a certain feature on the extension and there's an html page, js files and images then this gets downloaded to a certain folder inside the installed extension.
function download() { //only saves to downloads directory
var imgurl = "https://www.google.com.hk/images/srpr/logo11w.png";
console.log('download');
browser.downloads.download({url:imgurl},function(downloadId){
console.log("download begin, the downId is:" + downloadId);
});
}
I also tried the chrome download function above but that only works for the downloads folder not the extension folder.
Is there any way to make a custom updater?! I know we can't save to disk but any leniency or workarounds for the extension folder?! Even something silly like making a shell call to some dos (and linux/mac) thing that saves the file to the extension folder. I can fetch the files, just not save them.
Ok so I'll put it as an answer. This is the solution I'm leaning on which works for my scenario and I've listed some alternatives below:
Having the other files as separate extensions and giving the user an install link instead where they can install that extension, then those child extensions talk to the mother extension and they know the address to the resources in their child extension folder, so the mother gets the just the file locations from the children to load those assets from that folder. The child extensions are like bundles of those html and js with a background script which sends the addresses of these items to the mother.
https://developer.chrome.com/extensions/messaging#external
The drawback is that I'll have to see how that affects the urls like if I inject the html page from the child extension folder into the main interface using ajax then I can't use relative url's to any images in that 'cos the urls are relative to the mother extension folder.. I'll have to rewrite the child extension urls with the absolute paths into the html page to load images and js from the child extension html code which has relative urls.
Pros:
Cleaner and more persistent than indexeddb.
Files can be loaded normally from disk.
Cons:
User has to install separate extensions.
URL structure might be a bit confusing, need to rewrite urls if loading html from child. However this is only for image src's and where the javascript is loaded from so it's not such a big deal.
Other Possible Solutions:
Indexeddb which I'm already doing seems to be the preferred way of doing this but I really do not want to store every html asset in indexeddb. The upside is that while extensions need to be installed, this method can be done silently fetching and adding files without user interaction and indexeddb seems to be somewhat persistent. Might still end up using this because it is silent but having to load each asset from a database sounds like a nightmare.
The File Handle Api might have worked if I was working on Firefox only https://wiki.mozilla.org/WebAPI/FileHandleAPI
I haven't tried the shell copy, maybe if I fetch with ajax and then save to disk using some dos function and then doing different save functions for different OS systems.
Filesystem Api only saves to downloads and doesn't work for extensions anyways, so that's useless.
UPDATE
In windows there isn't any sudo, but this worked without admin priveleges for a subfolder (not on the C:\ root though). It would work for a linux only app very nicely. If I just wanted to save a file to a windows machine this might work.
Shell copy method would be to grab the contents of file with ajax from the local or remote location, output to DOS as a stream to save to file on windows. And do this for every operating system with a shell exec command or detect the OS and do that command. This way I can even put the files in the exact folder location.
Like say I make this sort of command from the contents:
//To append you can use >> instead of >
//folder seems necessary, can't save to root without admin
echo the content I want to save > C:\folder\textfile.txt
I thought of calling it using shell exec that only works in nodejs, so digging through the other answers on
How to execute shell command in Javascript
//full code to save file using javascript on windows
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("echo content to save > C:\folder\textfile.txt");
The shell command doesn't seem to work. i can't find what this is for. There doesn't seem to be a shell command in regular javascript for windows. It seems to require IE ActiveX. Doesn't work with Firefox or Chrome.
Extensions can't modify their sources because the browser verifies them and resets/disables the extension if they change. Also, in Firefox the extensions aren't even unpacked.
The solution is actually quite trivial: save the code in any storage (localStorage, chrome.storage.local, IndexedDB) as a string and then add it in your extension page as a standard DOM script element. You'll have to relax the standard CSP a bit for that.
I would like to list all the images I have in a specified folder inside of my chrome extension. However, I think I am fighting web technologies on this one. Is there any env in the chrome extension that executes javascript in a "server" context, as in, could read the files in it's neighboring folders?
Here's what my web accessible resources param looks like:
"web_accessible_resources": ["*.jpg","*.JPG", "*.png", "*.PNG"]
I have background script and content script both enabled.
I've tried running an ajax query against the dir to see if it will index but also no luck.
There seems to be no direct way.
As a workaround you can set up a native app. Get the resources and directories from the manifest file and if there are any wildcards, as in your example, search the directories for them using traditional system calls.
Here's a link to how you can set up the native app and communicate with it using your extension: https://developer.chrome.com/extensions/nativeMessaging
I am new to building Chrome extensions. I want it to be able to allow the user to choose a local folder and one random picture from there should be displayed for every new tab.
As of now, I have the images in the extension folder and have hardcoded the image names for access.
I read the following SO questions around this:
Open (Import) file in a chrome extension
Access Local Files using a Google Chrome Extension
But I am not sure which one to implement and if they are the way to go.
It's not possible* with an extension to maintain a persistent access to a folder in a filesystem. Chrome Apps can do it, extensions cannot.
Your best bet is to allow upload of files into a virtual filesystem. But it will not allow modifying the pictures without interacting with your extension again.
Alternatively, you could integrate with some cloud provider, i.e. monitor a folder in Dropbox.
* P.S. Regarding NPAPI, yes, it's being deprecated, but there is an alternative: you can have a Native Host program that your extension talks to. However, it makes it very awkward to distribute the extension - Native Host can't be submitted to Chrome Web Store. But in principle that can give you the full power of a native app.
I'm making an extension that among other things edit a javascript file in an external editor (one on the user's computer). The extension has the javascript file saved in chrome.storage but it will ofcourse be a lot easier for the user to write code in their own editor.
This is why I decided to find something that creates a file on the user's filesystem which the user can find and edit it themselves, and if any changes are made, sync that back up to the extension (either by periodically checking or by using some listener).
I have looked around but nothing really seems to fit what I'm trying to do. Chrome's fileSystem API only works for chrome apps, not chrome extensions and the HTML5 fileSystem API does not allow for a simple filesystem URL to be requested and opened, instead it obfuscates the stored file and makes it practically impossible to edit that file easily.
Something else I looked at which might be more promising is letting the user edit one of the files in the directory where the extension is stored and somehow retrieving that content. This is however going to be a bit tough to implement with chrome's all the hash checking going on in chrome extensions not to mention the general modifying of those files' contents by the extension (possibly by hacking around by specifying your own update URL and "updating" a dummy javascript file that is going to be written to).
Is there any way to simply ask for a location to store a file and then allow the user to edit that file and sync it back up?
No, extensions are sandboxed from the real filesystem.
As you said, it's possible to read extension's own files; however, this is read-only for the extension and modifying those files on a deployed extension will result in Chrome detecting extension "tampering" and immediate disabling as a precaution.
The only way for a Chrome extension to escape the sandbox is, as wOxxOm suggested, a Native Host module. Note that this cannot be distributed in Chrome Web Store with the extension; it needs a separate installer.
Alternatively, you could use some sort of cloud storage with API to access it; e.g. a user could store something in a Dropbox subfolder, and your extension can authorize access to it via Dropbox API. Unfortunately, there is no "native" solution like syncFileSystem for Apps.
I have downloaded a image file using third party api and written in window persistent storage
name called pogo.png how can I retrieve and execute.
For debugging the Filesystem API, you have a few options:
1.Use this this extension to view/remove files.
2.See the tips here: tips That includes viewing stored files very easily using the filesystem: URLs.
3.Drop the Filesystem Playground demo (http://html5-demos.appspot.com/static/filesystem/filer.js/demos/index.html) into your origin. You can use that to view/rename/delete files/folders easily.
4.Chrome DevTools now has support for the Filesystem API in Chrome Canary...at least viewing the files stored under an origin. To use that, right now you need to enable Developer Tools experiments in about:flags, restart, hit the gear in the devtools (lower right corner), and enable the 'FileSystem inspection' under the experimental tab.