Files
webextensions-examples/store-collected-images/webextension-plain/background.js
Luca Greco 7b6b03a72c Add an indexedDB file storage example: image-reference-collector (#224)
* new example: image-reference-collector (indexedDB file storage demo)

* fix: added missing deps, updated all npm dependencies and webpack config to v.2

* chore: Renamed the example to store-collected-images

* chore: Removed from utils/image-store any direct call to the UI code

* move example built using webpack into its own subdir

* tweak browser action title

* added plain webextension example (without webpack build step)

* added README.md file to plain webextension example

* small changed based on the review comments

* fixed typo in store-collected-images example (webpack-based version)

* Remove React from the store-collected-images (plain webextension version)

* Fix eslint errors on store-collected-images example (both versions)

* Fix some typos in the README files
2017-07-19 15:06:46 -07:00

48 lines
1.3 KiB
JavaScript

// Open the UI to navigate the collection images in a tab.
browser.browserAction.onClicked.addListener(() => {
browser.tabs.create({url: "/navigate-collection.html"});
});
// Add a context menu action on every image element in the page.
browser.contextMenus.create({
id: "collect-image",
title: "Add to the collected images",
contexts: ["image"],
});
// Manage pending collected images.
let pendingCollectedUrls = [];
browser.runtime.onMessage.addListener((msg) => {
if (msg.type === "get-pending-collected-urls") {
let urls = pendingCollectedUrls;
pendingCollectedUrls = [];
return Promise.resolve(urls);
}
});
// Handle the context menu action click events.
browser.contextMenus.onClicked.addListener(async (info) => {
try {
await browser.runtime.sendMessage({
type: "new-collected-images",
url: info.srcUrl,
});
} catch (err) {
if (err.message.includes("Could not establish connection. Receiving end does not exist.")) {
// Add the url to the pending urls and open a popup.
pendingCollectedUrls.push(info.srcUrl);
try {
await browser.windows.create({
type: "popup", url: "/popup.html",
top: 0, left: 0, width: 300, height: 400,
});
} catch (err) {
console.error(err);
}
return;
}
console.error(err);
}
});