Skip to content

Instantly share code, notes, and snippets.

@freaktechnik
Last active April 26, 2024 22:10
Show Gist options
  • Select an option

  • Save freaktechnik/45ba1d1b6791984f2039a6c909a4d064 to your computer and use it in GitHub Desktop.

Select an option

Save freaktechnik/45ba1d1b6791984f2039a6c909a4d064 to your computer and use it in GitHub Desktop.
'use strict';
// OMITTED: Working code
/** Serialize the body of a Response to a data: URI */
function responseToDataURI(response) {
return new Promise(function(resolve, reject) {
try {
let reader = new FileReader();
reader.addEventListener("load", function(e) {
resolve(e.target.result);
});
response.blob().then((blob) => reader.readAsDataURL(blob));
} catch (e) {
console.log("RTDU ERR:", response.url);
reject(e);
}
});
}
/** Replace favicon_url in a transformTabsData entry with a data: URI */
function retrieveFavicon(data) {
return fetch(data.favicon_url, { cache: 'force-cache' })
.then(responseToDataURI, (err) => data);
}
/** Call retrieveFavicon on a list with proper error handling */
function retrieveFavicons(entries) {
const promises = entries.map(retrieveFavicon);
return Promise.all(promises).catch((e) => {
console.log("RFS ERR:", e);
return entries;
});
}
// OMITTED: Working code
/** Handler for clicks to the browser action */
function dumpTabs() {
browser.browserAction.setIcon({path: "icons/throbber-32.gif"});
browser.tabs.query({})
.then(transformTabsData)
.then(retrieveFavicons) // It all works if this line is commented out
.then(toJSONBlob)
.then(function(blob) {
browser.downloads.download({
url: URL.createObjectURL(blob),
filename: 'tabs.json',
conflictAction: 'overwrite',
})
})
// FIXME: Why doesn't it log and dimiss the throbber on failure?
// The promises API CAN'T be brain-dead enough to force
// me back to nesting callbacks to get "skip to end on failure"
.catch((e) => console.log("Tab dumping failed", e))
.then((_) => browser.browserAction.setIcon({ path:
browser.runtime.getManifest().browser_action.default_icon}));
}
chrome.browserAction.onClicked.addListener(dumpTabs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment