I want my chrome extension to access a sqllite file which is part of the extension. I looked up the chrome.storeage api, but it doesnt really help me! And my JavaScript knowledge isnt enough to access a file and read its content.
Also is it possible to start my extension when a specific file type is loaded?
This is how I read a json file named env.json in the root of the extension directory.
Add the following configuration to the manifest.json. You need to add your sqlite file in this configuration.
"web_accessible_resources": [
"*.json"
],
I defined a function to load json file. You need to tweak the response data, cause the sqlite file is a binary data file instead of a text file.
// the path is relative to the extension directory
let loadData = async path => {
let url = chrome.runtime.getURL(path);
let resp = await fetch(url)
let json = resp.json()
return json
}
Usage example
// Use the above function to load extension file
let conf = await loadData('env.json')
Related
I wrote a report generator the other day which generates an XML file on the .NET Core server, as part of the generation it creates temporary files that need removing once the file has been downloaded into the browser.
I have this javascript function in the index.html which does the download into the browser:
var downloadFromUrl = function(url) {
var anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.click();
anchorElement.remove();
}
Once the final report has been generated on the server I call the following to download the file and once complete delete the temporary files from the server:
try
{
await JsRunTime.InvokeVoidAsync("downloadFromUrl", $"api/Export/GetExportFile?exportId={exportRunner.ExportId}&fileDownloadName={exportRunner.FinalExportFileName}&outputFormatType={exportRunner.OutputFormatType}");
}
finally
{
await exportRunner.DeleteTempFilesOnServer();
}
This was working fine on a 100MB+ file.
I have today used the same approach to generate smaller files and am now hitting an issue. It runs the await exportRunner.DeleteTempFilesOnServer() before
await JsRunTime.InvokeVoidAsync("downloadFromUrl",
$"api/Export/GetExportFile?exportId={exportRunner.ExportId}&fileDownloadName={exportRunner.FinalExportFileName}&outputFormatType={exportRunner.OutputFormatType}"
);
so the download into the browser fails as the files have been deleted.
It feels like JsRunTime.InvokeVoidAsync is not being awaited.
Is there a way I can make sure the file has finished downloading into the browser before the delete kicks in?
Is it possible to write a firebase cloud function that triggers when a new fila was created in firebase storage - onFinalize (but we don't know the exact bucket in advance) ?
Inside my firebase storage a have a folder 'loads' and inside I have folders named with load id like:
/loads/-Lw1UySdunYyFMrrV6tl
/loads/-LwisA5hkNl_uxw3k36f
/loads/-LwkPUm-q7wNv-wZ49Un
https://ibb.co/NnQkTyC here's a screenshot of storage
I want to trigger cloud function when new file has been created inside one of these folders. I don't know in andvance where the file will be created. I don't know if this is even possible. That's why I need an advice.
My main goal is to merge 2 pdf files in one within cloud functions. In my app (TMS written with vuejs) on frontend I create confirmationOrder.pdf using html2canvas/jsPDF and then save it to storage/loads/${loadID}. And later on user can manually upload POD.pdf on the same bucket. When it happens I want my cloud function to merge these two pdfs in one new file (in same storage bucket). But again I don't know the bucket in advance.
Here's how I upload PDFs in frontend:
async uploadConfPDF({ commit }, payload) {
const PDF = payload.PDF;
const loadID = payload.loadID;
const fileName = payload.fileName;
const fileData = await firebase
.storage()
.ref(`loads/${loadID}/${fileName}.pdf`)
.put(PDF);
const confOrderURL = await fileData.ref.getDownloadURL();
return confOrderURL;
},
Any help is highly appreciated. Sorry if my explanation could seem not clear enough. English is not my native language.
EDIT FOLLOWING YOUR QUESTION RE-WORKING
Based on your code and on the print screen of your Cloud Storage console, you are working in the default bucket of your project, which location's URL is gs://alex-logistics.appspot.com.
As we can see on the print screen of your Cloud Storage console, the files in your bucket are presented in a hierarchical structure, just like the file system on your local hard disk. However, this is just a way of presenting the files: there aren't genuine folders/directories in a bucket, the Cloud Storage console just uses the different part of the files paths to "simulate" a folder structure.
So, based on the above paragraphs, I think that your question can be re-phrased to "In a Cloud Function, how can I extract the different parts of the path of a file that is uploaded to the default bucket of my Firebase Project?".
Answer:
In a Cloud Function that is triggered when a file is added to the default bucket, you can get the file path as follows:
exports.yourCloudFunction = functions.storage.object().onFinalize(async (object) => {
const filePath = object.name; // File path in the bucket.
//...
});
Since we use an onFinalize event handler, you get the path of this new file by using the name property of the object Object, which is of type ObjectMetadata.
You can then use some String methods to, for example, extract from this path the ${loadID} you refer to in your question.
I searched lot and found nothing about how to send files. Even in google documentation there is nothing about sending file using Javascript sdk.
See here https://developers.google.com/drive/v3/web/manage-uploads
So right now I'm converting the nodeJs script to javascript. And they used fs to get the readstream. And I have no idea how to do that in javascript. Closet I can get to this...
var file = uploadButton.files[0]
var fileName = uploadButton.files[0].name
var fileMetadata = {
'name': fileName
};
var media = {
mimeType: 'image/jpeg',
body: file
};
gapi.client.drive.files.create({
resource: fileMetadata,
media: media.result,
fields: 'id'
}).execute();
Above code creates the empty file with then fileName and no content inside on it.
In order to upload a file to your google drive you need to use a google request object and 'POST' the file. You can see an example in this answer. Keep in mind that you need to get your API keys in order to initialise your google drive client object.
I am trying to create a js file downloader module. Right now I stumbled on some file urls like - https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTA_Rg2GwJVJEmOGGoYFev_eTSZAjkp_stpi4cUXpjWbE6Wh7gSpCvldg.
My question here is how to get the proper extension of the file knowing only the url ?
The only idea I have is to use this module to check the file after I download it.
As suggested by #melpomene you can make HEAD request for file, get Content-Type from response headers
fetch("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTA_Rg2GwJVJEmOGGoYFev_eTSZAjkp_stpi4cUXpjWbE6Wh7gSpCvldg", {method:"HEAD"})
.then(response => response.headers.get("Content-Type"))
.then(type => console.log(`.${type.replace(/.+\/|;.+/g, "")}`));
I am using react-native-fetch-blob to download excel file and these files are stored inside app/documents folder which I am not able to open in iphone and I don't know the absolute path of the iphone's document folder to save the file there.
here is my code:
downloadFile(){
const dirs = RNFetchBlob.fs.dirs;
let context = this,
userBarRelationId = context.state.userBarId;
RNFetchBlob
.config({
fileCache : true,
path : dirs.DocumentDir + '/excel.xlsx'
})
.fetch('GET', 'http://localhost:5000/getExcel/'+userBarRelationId, {
})
.then((res) => {
console.log('The file saved to ', res.path(), res.data)
})
}
Wait, you're asking to save the data in a Documents folder other than the one your sandboxed application has access to?
This is not possible by iOS design. See also here. Not even a minute of google searching reveals you that, not to mention that it is basically a core principle.
If I didn't understand you I apologize, but then I have no clue what you want to know. If you're asking for how to save the file to the user's iCloud Drive (which kind of has a Documents folder, too), you'll have to invest some more work. I've never done it myself, but a good starting point is this.