Skip to content

Commit

Permalink
Modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rgantzos committed Aug 5, 2023
1 parent fb19af4 commit 3cdb42c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
6 changes: 6 additions & 0 deletions api/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ScratchTools.modules.forEach(async function (script) {
var fun = await import(script.file);
fun.default({
feature: new Feature(script.feature),
});
});
114 changes: 77 additions & 37 deletions extras/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let cachedStyles;
async function cache() {
cachedStorage = (await chrome.storage.sync.get("features"))?.features || "";
cachedStyles = await getEnabledStyles();
cachedScripts = await getModules();
return true;
}
cache();
Expand Down Expand Up @@ -391,33 +392,7 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) {
files: [`/api/spaces.js`],
world: "MAIN",
});
ScratchTools.console.log("Injected protect spaces API.");
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: [`/extras/protect-mention.js`],
world: "MAIN",
});
ScratchTools.console.log("Injected protect mention script.");
await chrome.scripting.executeScript({
args: [chrome.runtime.id],
target: { tabId: tabId },
func: injectExtensionId,
world: "MAIN",
});
ScratchTools.console.log("Injected extension ID.");
function injectExtensionId(id) {
ScratchTools.id = id;
}
await chrome.scripting.executeScript({
args: [chrome.runtime.getURL("/extras/icons/icon128.png")],
target: { tabId: tabId },
func: injectExtensionIcon,
world: "MAIN",
});
ScratchTools.console.log("Injected extension icon.");
function injectExtensionIcon(icon) {
ScratchTools.icons = { main: icon };
}
ScratchTools.console.log("Injected spaces API.");
var newFullData = [];
for (var i in data) {
var feature = data[i];
Expand Down Expand Up @@ -461,6 +436,47 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) {
function getFeaturesForAPI(dataFeatures) {
ScratchTools.Features.data = dataFeatures;
}
await chrome.scripting.executeScript({
args: [cachedScripts],
target: { tabId: tabId },
func: injectCachedModules,
world: "MAIN",
});
function injectCachedModules(data) {
ScratchTools.modules = data;
}
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: [`/api/module.js`],
world: "MAIN",
});
ScratchTools.console.log("Injected module API.");
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: [`/extras/protect-mention.js`],
world: "MAIN",
});
ScratchTools.console.log("Injected protect mention script.");
await chrome.scripting.executeScript({
args: [chrome.runtime.id],
target: { tabId: tabId },
func: injectExtensionId,
world: "MAIN",
});
ScratchTools.console.log("Injected extension ID.");
function injectExtensionId(id) {
ScratchTools.id = id;
}
await chrome.scripting.executeScript({
args: [chrome.runtime.getURL("/extras/icons/icon128.png")],
target: { tabId: tabId },
func: injectExtensionIcon,
world: "MAIN",
});
ScratchTools.console.log("Injected extension icon.");
function injectExtensionIcon(icon) {
ScratchTools.icons = { main: icon };
}
addData();
injectStyles(tabId);
for (var i in data) {
Expand Down Expand Up @@ -573,16 +589,17 @@ chrome.tabs.onUpdated.addListener(async function (tabId, info) {
await fetch(`/features/${data[el].id}/data.json`)
).json();
console.log(data[el].id);
newData.scripts?.forEach(function (script) {
console.log(script.file);
if (new URL(tab.url).pathname.match(script.runOn)) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: [`/features/${data[el]["id"]}/${script.file}`],
world: newData.world?.toUpperCase() || "MAIN",
});
}
});
newData.scripts
?.filter((el) => !el.module)
?.forEach(function (script) {
if (new URL(tab.url).pathname.match(script.runOn)) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: [`/features/${data[el]["id"]}/${script.file}`],
world: newData.world?.toUpperCase() || "MAIN",
});
}
});
} else {
chrome.scripting.executeScript({
target: { tabId: tabId },
Expand Down Expand Up @@ -668,6 +685,29 @@ async function getEnabledStyles() {
}
return allStyles;
}
async function getModules() {
var allScripts = [];
var data = (await (await fetch(`/features/features.json`)).json()).filter(
(el) => el.version === 2 && cachedStorage.includes(el.id)
);
for (var i in data) {
var feature = data[i];
var scripts =
(
await (await fetch(`/features/${feature.id}/data.json`)).json()
).scripts?.filter((el) => el.module) || [];
if (scripts) {
for (var i2 in scripts) {
scripts[i2].feature = feature;
scripts[i2].file = chrome.runtime.getURL(
`/features/${feature.id}/${scripts[i2].file}`
);
allScripts.push(scripts[i2]);
}
}
}
return allScripts;
}
chrome.runtime.onMessage.addListener(async function (
msg,
sender,
Expand Down
2 changes: 1 addition & 1 deletion features/isonline/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": ["Website"],
"tags": ["New", "Featured"],
"styles": [{ "file": "style.css", "runOn": "/users/*" }],
"scripts": [{ "file": "script.js", "runOn": "/users/*" }],
"scripts": [{ "file": "script.js", "runOn": "/users/*", "module": true }],
"additionalAgreements": "By enabling this feature, you understand that other users will be able to see if you are online or not.",
"translations": ["en", "tr", "de", "es", "fr", "ru"],
"similar": ["display-name", "emoji-status"]
Expand Down
5 changes: 2 additions & 3 deletions features/isonline/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function checkUser() {
export default async function({ feature }) {
var isOnlineFeature = new Feature({ id: "isonline" })
if (document.querySelector(".ste-isonline")) {
document.querySelector(".ste-isonline").remove();
Expand Down Expand Up @@ -35,5 +35,4 @@ async function checkUser() {
order: 1,
});
}
}
checkUser();
}

0 comments on commit 3cdb42c

Please sign in to comment.