const permissions = { async add(permission = [], origins = []){ if (!origins?.length){origins = undefined}; if (typeof permission === "string"){permission = [permission]}; return new Promise((r, reject) => { chrome.permissions.request( {permissions: permission, origins}, (granted) => { if (!granted){ reject("Permission not granted"); } else { r(); } } ) }) }, async remove(permission = [], origins = []){ if (!origins?.length){origins = undefined}; if (typeof permission === "string"){permission = [permission]}; return new Promise((r, reject) => { chrome.permissions.remove( {permissions: permission, origins}, (removed) => { if (!removed){ reject("Permission not removed"); } else { r(); } } ) }) }, async with(permission = [], origins = [], func){ if (!origins?.length){origins = undefined}; if (typeof permission === "string"){permission = [permission]}; await new Promise((r, reject) => { chrome.permissions.request( {permissions: permission, origins}, (granted) => { if (!granted){ reject("Permission not granted"); } else { r(); } } ) }); let out = await func(); await new Promise((r, reject) => { chrome.permissions.remove( {permissions: permission, origins}, (removed) => { if (!removed){ reject("Permission not removed"); } else { r(); } } ) }) return out; }, }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter