Skip to content

Instantly share code, notes, and snippets.

@dotproto
Created September 1, 2025 17:29
Show Gist options
  • Select an option

  • Save dotproto/f337f6234a54616d7a98d4555c427264 to your computer and use it in GitHub Desktop.

Select an option

Save dotproto/f337f6234a54616d7a98d4555c427264 to your computer and use it in GitHub Desktop.
WebExtension permission cache
export class PermissionCache {
static areas = ["origins", "permissions", "data_collection"];
origins = [];
permissions = [];
data_collection = [];
ready;
constructor () {
this.ready = browser.permissions.getAll().then((grants) => {
this.handleAdded(grants);
return this;
});
browser.permissions.onAdded.addListener(this.handleAdded);
browser.permissions.onRemoved.addListener(this.handleRemoved);
}
handleAdded(added) {
for (const key of PermissionCache.areas) {
const addedValues = added[key] || [];
this[key].push(...addedValues);
}
}
handleAdded = Object.getPrototypeOf(this).handleAdded.bind(this);
handleRemoved(removed) {
for (const key of PermissionCache.areas) {
const currentValues = this[key] || [];
const removedValues = removed[key] || [];
for (const removedVal of removedValues) {
const index = currentValues.indexOf(removedVal);
if (index !== -1) currentValues.splice(index, 1);
}
}
}
handleRemoved = Object.getPrototypeOf(this).handleRemoved.bind(this);
};
// Example usage
const perms = new PermissionCache();
browser.action.onClicked.addListener(async () => {
if (!perms.isReady) await perms.ready;
if (perms.data_collection.includes("browsingData")) {
} else {
showBrowsingDataPrompt();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment