How read a file inside the CRX file into a string? - javascript

I want to read the data from a file that is distributed with my CRX (it's in a subfolder myfiles) into a javascript variable. (It is not on the filesystem. It's inside the crx)
I understand I can use XMLHttpRequest, but what would the url be? Would it just be a relative one? or a file:///WHAT_GOES_HERE/myfile.txt? Since this is a packaged app, what would go after the "file:///"?
I don't want to use chrome.runtime.getPackageDirectoryEntry because that requires Chrome 29 or later and some of my users might not have that version
How would I do this?
This is a chrome packaged app, not extension

To get the URL you should use chrome.runtime.getURL.

Related

How make userscript to not download already existing file

I use tampermonkey on mobile firefox. In userscript I use GM_download(url, name) to download file from url. When it start download same file in firefox the result of this is :
Hello.txt
Hello(1).txt
...
Hello(9).txt
I want to know how not download duplicates. Or way to block firefox download duplicate.
Or get directory list. If there file alredy exist dont download.
In bash(linux cml) you write ls ,in windows you write dir. But how it would be in userscript? If yo write file:///storage/emulated/0/Download/Hello.txt in your browser it show all files that you want (in this address is Hello.txt file). But I can't get this html or whatever is this via XMLHttpRequest
Ps: Sorry for my english
You cannot access the filesystem to check if some file exists. That would be pretty dangerous for userscripts - imagine you installed userscript that did that. Firefox doesn't even allow plugins to access filesystem AFAIK, so tampermonkey cannot provide that API.
What you can do is to remember the name of the downloaded file. Your options are:
localStorage - for example set localStorage[filename]=true and then check for that. But if you delete the file on your phone, this will prevent you from downloading it again
indexedDB this is nicer for saving data, but the problem is same as for local storage.
You can store the list of files downloaded with your user script, but cannot access the directory list I think.

Can a Chrome Extension Dynamically Add JS Files to Local Extension Directory? User Upload or Saved From the Web?

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.

Get filenames for all files in a chrome extension directory?

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.

How to validate a local file system directory path?

I am working on an application that should take file system path as directory and then further processing is done on that path. The code is written into javascript. I do no want to use Node.js or any external library for validation.
We just want to give path : C:/Users/Desktop/Test_Folder
How can we validate that path in js?
We need the solution to be worked on Windows, Linux and Mac OS.
If this is just an application in browser, then, generally, it is not possible. See the following questions:
Local file access with javascript
https://stackoverflow.com/questions/24369131/is-html5-file-api-dead
List local directory on Web Application
There was an attempt of a standardized file system API, which was discontinued. Seems like it only worked in Chrome anyway.
Update
A quote from the Chrome documentation:
Use the chrome.fileSystem API to create, read, navigate, and write to the user's local file system. With this API, Chrome Apps can read and write to a user-selected location. For example, a text editor app can use the API to read and write local documents. All failures are notified via chrome.runtime.lastError.

Chrome API retrieving most recently downloaded file

Is there a function in the Chrome API that gets the most recently downloaded file?
My goal is to grab this file, copy it, and save it to a different location since in Chrome you cannot change the default download directory for specific file types/websites:
How to set download location via chrome api
Is this idea feasible?
The chrome.downloads API just became stable with Chrome 31. It allows you to deal with all download interactions, like getting the filename and the MIME type. I'm not sure if and how you can copy and move files on the user's system by a Chrome extension.
For your goal you could use the onDeterminingFilename function and alter the filename to contain subdirectories, like putting all .jpg files into Downloads/images/.

Categories