Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Explosion-Scratch/4e868a4a55da531b6715c9461cea88a2 to your computer and use it in GitHub Desktop.
Save Explosion-Scratch/4e868a4a55da531b6715c9461cea88a2 to your computer and use it in GitHub Desktop.
Chrome Extension Utilities (manifest V3)
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;
},
}
function getActiveTab(){
return new Promise((r, reject) => {
chrome.tabs.query({
active: true,
lastFocusedWindow: true,
}, (tabs) => {
if (tabs.length){
r(tabs[0]);
} else {
chrome.tabs.query({}, tabs => {
if (tabs.length){
if (tabs.length === 1){
r(tabs[0]);
} else if (tabs.find(i => i.active)){
r(tabs.find(i => i.active))
} else {
reject("No tab found");
}
} else {
reject("No tab found");
}
})
}
})
});
}
async function hash(str, iterations = 1000) {
const buf = await crypto.subtle.digest(
"SHA-256",
new TextEncoder("utf-8").encode(str)
);
let result = Array.prototype.map
.call(new Uint8Array(buf), (x) => ("00" + x.toString(16)).slice(-2))
.join("");
if (iterations === 0) {
return result;
} else {
// Note: Don't rename this function without renaming this line
return await hash(result, iterations - 1);
}
}
function runFunction(func){
return new Promise(async resolve => {
let tab = await getActiveTab();
chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
func,
}, ({result}) => resolve(result))
});
}
const storage = {
async set(key, value){
return new Promise(resolve => {
chrome.storage.local.set({[key]: value}, (a) => resolve(a[key]))
})
},
get(key){
return new Promise(resolve => {
chrome.storage.local.get([key], (a) => resolve(a[key]));
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment