Skip to content

Commit 5eda7c7

Browse files
committed
Request permissions API
1 parent c0457b0 commit 5eda7c7

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

api/feature.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Feature {
66
finalFeature = el;
77
}
88
});
9+
this.requestPermissions = async function(...permissions) {
10+
return await ScratchTools.sendMessage("request-perms", permissions)
11+
}
912
this.data = finalFeature;
1013
this.msg = function (string) {
1114
return this.data.localesData[`${this.data.id}/`+string] || `ScratchTools.${this.data.id}.${string}`;

api/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ if (
101101
ScratchTools.type = "Website";
102102
}
103103

104+
ScratchTools.MESSAGES = []
105+
ScratchTools.sendMessage = function(id, content) {
106+
let uuid = UUID()
107+
chrome.runtime.sendMessage(ScratchTools.id, { message: id, content, source: "message-api", uuid });
108+
return new Promise((resolve, reject) => {
109+
ScratchTools.MESSAGES.push({ message: id, source: "message-api", uuid, resolve });
110+
});
111+
}
112+
113+
function UUID() {
114+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
115+
const random = Math.random() * 16 | 0;
116+
const value = char === 'x' ? random : (random & 0x3 | 0x8);
117+
return value.toString(16);
118+
});
119+
}
120+
104121
var storagePromises = [];
105122
ScratchTools.storage = {
106123
get: async function (key) {

build/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("./write-permissions")

build/write-permissions.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require("fs")
2+
let features = JSON.parse(fs.readFileSync("./features/features.json"))
3+
let manifest = JSON.parse(fs.readFileSync("./manifest.json"))
4+
let permissions = ["https://api.scratch.mit.edu"]
5+
6+
for (var i in features) {
7+
if (features[i].version === 2) {
8+
let feature = JSON.parse(fs.readFileSync(`./features/${features[i].id}/data.json`))
9+
if (feature.permissions) {
10+
permissions.push(...feature.permissions.filter((perm) => !permissions.includes(perm)))
11+
}
12+
}
13+
}
14+
15+
manifest.optional_permissions = permissions.filter((perm) => !checkUrl(perm))
16+
manifest.optional_host_permissions = permissions.filter((perm) => checkUrl(perm))
17+
fs.writeFileSync("./manifest.json", JSON.stringify(manifest, null, 2), 'utf8');
18+
19+
function checkUrl(perm) {
20+
try {
21+
new URL(perm);
22+
return true;
23+
} catch (_) {
24+
return false;
25+
}
26+
}

extras/background.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,28 @@ chrome.runtime.onMessageExternal.addListener(async function (
692692
});
693693
}
694694
if (msg === "returnToTab") {
695-
await chrome.tabs.update(sender.tab.id, {active: true})
695+
await chrome.tabs.update(sender.tab.id, { active: true });
696+
}
697+
if (msg.source === "message-api") {
698+
if (msg.message?.startsWith("request-perms")) {
699+
let perms = msg.content;
700+
701+
chrome.permissions.request({ permissions: perms }, async (granted) => {
702+
let isComplete = !!granted;
703+
704+
await chrome.scripting.executeScript({
705+
args: [isComplete, msg.uuid],
706+
target: { tabId: sender.tab.id },
707+
func: sendPermsResponse,
708+
world: "MAIN",
709+
});
710+
function sendPermsResponse(completed, uuid) {
711+
ScratchTools.MESSAGES.find((el) => el.uuid === uuid).resolve(
712+
completed
713+
);
714+
}
715+
});
716+
}
696717
}
697718
if (typeof msg === "object") {
698719
if (msg.message === "storageSet") {

0 commit comments

Comments
 (0)