Created
July 7, 2022 02:47
-
-
Save Explosion-Scratch/4e868a4a55da531b6715c9461cea88a2 to your computer and use it in GitHub Desktop.
Chrome Extension Utilities (manifest V3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
}) | |
} | |
}) | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runFunction(func){ | |
return new Promise(async resolve => { | |
let tab = await getActiveTab(); | |
chrome.scripting.executeScript({ | |
target: { | |
tabId: tab.id, | |
}, | |
func, | |
}, ({result}) => resolve(result)) | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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