I am currently using IndexedDB on Firefox to store the output of some file processing that is being done by a web worker. I have managed to get the finished file saved within the IndexedDB as IDBMutableFile, however I can't seem to transfer the file as a blob to the web UI so that the user can download it.
When I read the file from the DB, I tried to use URL.createObjectURL to get a blob: link, however it seems like the file handle gets dereferenced as soon as the onsuccess handler exits (the blob is no longer accessible via a link).
I also tried to keep the file handle open using a FileReader, but that still didn't work (I get a FileHandleInactiveError).
Any ideas? The only thing that works seems to be to read the entire file as an arrayBuffer, and then create a new blob linked via URL.createObjectURL. The problem is that the file is very large (1GB) and loading the entire thing into memory causes an NS_ERROR_OUT_OF_MEMORY exception.
Here is the code:
// Retrieve the file that was just stored
transaction.objectStore("files").get("00b6ac47-7400-4894-8a76-de597c4d7b3e").onsuccess = function (event) {
var fileHandle = event.target.result;
var theFile = fileHandle.getFile();
theFile.onsuccess = function() {
var actualFile = this.result;
// Get window.URL object
var URL = window.URL || window.webkitURL;
var blobUrl = URL.createObjectURL(actualFile);
// Set img src to ObjectURL
var link = document.getElementById("blobLink");
blobLink.setAttribute("href", blobUrl);
//Problem: This link will point to a deleted blob in the UI
I'm trying to access the information within the network tab for automation purposes.
My requirement is to capture the information available in the network in a text file so i can use that data.
I found out the way to do it manually. for this we have to go to network tab and right click and select save as HAR content so this will be saved to file
Now i want to do this automatically so I decided to build a plugin which can save the network tab content to a text file. Is it possible to save this information to text file using java?
Also please tell me any other approaches to capture this information
Thanks in advance
Yes, you can do it in a chrome extension using chrome.devtools.network.getHAR:
function saveAs(arr, name, mime) {
var blob = new window.Blob(arr, { type: mime || 'text/plain;charset=utf-8' });
var url = window.URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = url;
link.download = name || 'save.txt';
link.dispatchEvent(new MouseEvent('click'));
}
chrome.devtools.network.getHAR(function(log) {
var json = JSON.stringify(log, null, '\t');
saveAs([json], 'har.json', 'text/json;charset=utf-8');
});
I'm trying to utilize the momentjs library in Google Apps Script but I'm not clear on how to do so. I'm not sure how to add the library, so obviously running something like the following results in "Reference Error: 'moment' is not defined":
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var difference = a.diff(b);
Most people try to use the library with the key ending in 48. That library is pretty dated (it is version 2.9 which is pretty old).
Using eval and UrlFetchApp.fetch moment.js or any other external library can be used easily in google app scripts.
function testMoment() {
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js').getContentText());
var date = moment().format("MMM Do YY");
Logger.log(date)
}
You may either host the moment.js on your own server, or use a CDN like cloudflare CDN to reference the library.
For cloudflare, here is the page which shows moment.js versions and their urls:
https://cdnjs.com/libraries/moment.js/
As of writing this post 2.18.1 is the latest version.
For the example posted by OP it will look like this:
function testMomentDifference() {
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js').getContentText());
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var difference = a.diff(b);
Logger.log(difference);
}
The moment script ID for the Google Apps Script IDE has changed. It is now "15hgNOjKHUG4UtyZl9clqBbl23sDvWMS8pfDJOyIapZk5RBqwL3i-rlCo"
You can add moment and moment.tz to app scripts by creating a new Script file and adding the following code:
var cacheExpire = 3600;
var momentCache = "momentCache";
var momentUrl = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"
var momentTzCache = "momentTzCache";
var momentTzUrl = "https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data-2012-2022.min.js"
useCachedOrLive(momentCache,momentUrl);
useCachedOrLive(momentTzCache,momentTzUrl);
function useCachedOrLive(cacheToCheck, url){
var cache = CacheService.getUserCache();
var cachedData = cache.get(cacheToCheck);
console.log(cacheToCheck);
if(cachedData !== null){
console.log("using cached " + cacheToCheck)
eval(cachedData);
}
else
{
console.log("getting live " + cacheToCheck);
var response = UrlFetchApp.fetch(url).getContentText();
cache.put(cacheToCheck, response, cacheExpire);
eval(response);
}
}
This uses the cache service to reduce round trip calls and you can modify it to include a subset of data if you want.
Thanks to apadana for getting me started!
There is a better and best way to use moment
Do not use UrlFetchApp, to avoid quota exceeded, caching, and server issues
Download moment.min.js and momemt-timzone.min.js in last versions
and integrate the full files in apps script like the below screen
There is no problems in long run with this approach, just update the files any time when you need.
After adding the two files, just publish a new version and include it in any other script
For example:
I will create a script with name "MomentAPI" and include the two
files mentioned, and publish a new version.
in other script with name "myScript" I will include the library
"MomentAPI" with its script id as known
then will use it like the below examples
const moment = MomentAPI.moment; // init the library
const start = moment().startOf('day').toDate(); // Dec 06 00:00:00
const end = moment().endOf('day').toDate(); // Dec 06 23:59:59
const d = moment(1767139200000).tz('America/New_York').format('ha'); // 7am EST
Using external Javascript library is not so easy... Depending on the context in which you want to use it (a webapp of a document embedded script) the approach will be different.
I didn't try it in client JavaScript and I'm not sure caja will allow it but I found this post that shows a possible way to include it using a Google Script Library that a user has build and if I read the post it seems to work...
The "user" is a Google developper so he knows for sure what he is talking about ;) please update here if it worked for you.
In a Windows Store app project I was reading a File like this:
var uri = new Uri("ms-appx:///DataModel/Accounts.csv");
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var read = await FileIO.ReadTextAsync(file);
The file Accounts.csv was added with build action content.
I have now moved the code to a separate class library. File is still added with build Action content.
But the uri scheme does not seem to work anymore.
I've also tried:
var file = await StorageFile.GetFileFromPathAsync("/DataModel/Accounts.csv");
var read = await FileIO.ReadTextAsync(file);
How do I access a file in a WinRT class library? Does the file library behave differently than the Store app project?
You can access file in Windows Store class library like this.
var _Assembly = Assembly.Load(new AssemblyName("ASSEMBLY_NAME"));
var _Stream = _Assembly.GetManifestResourceStream("YOUR_FILE_NAME_WITH_PATH.xml");
Please note in class library path members are seperated by . not by \
To get all the resource names
var names = _Assembly.GetManifestResourceNames();
The GetManifestResourceStream method will always returns NULL if the resource built action property is not set to embedded resource
I have an extension and I did lots of refactoring in my code, I want to publish a new version but in my new code I want to detect when the extension is being updated and do some actions.
I know that with chrome.management I can detect other extension update but I want to detect my own extension update.
You don't need management permission to do that.
You can use chrome.app.getDetails().version To get the last version of your extension using your background page. Than you can store it in localStorage. Every time you load your background page or once every hour you can check if the version matches the one you have on localStorage and detect new installs or updates.
I wonder if there's a better/easier way to do it. But right now this is what I do.
EDIT:
Alternatively you can use this code to get the latest version:
var url = chrome.extension.getURL("manifest.json");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if(xhr.readyState == 2 && xhr.status == 200) {
var manifest = JSON.parse(xhr.responseText);
alert("Version: " + manifest.version);
}
};
xhr.open("GET", url);
xhr.send();
extracted from this other SO response.