From 3cdb42c60231f95dc75cc60f405aa215f1c1f9c8 Mon Sep 17 00:00:00 2001 From: rgantzos Date: Sat, 5 Aug 2023 12:43:36 -0700 Subject: [PATCH] Modules --- api/module.js | 6 ++ extras/background.js | 114 ++++++++++++++++++++++++------------ features/isonline/data.json | 2 +- features/isonline/script.js | 5 +- 4 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 api/module.js diff --git a/api/module.js b/api/module.js new file mode 100644 index 00000000..6b55ee17 --- /dev/null +++ b/api/module.js @@ -0,0 +1,6 @@ +ScratchTools.modules.forEach(async function (script) { + var fun = await import(script.file); + fun.default({ + feature: new Feature(script.feature), + }); +}); diff --git a/extras/background.js b/extras/background.js index d07aed4e..06e1ac2c 100644 --- a/extras/background.js +++ b/extras/background.js @@ -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(); @@ -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]; @@ -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) { @@ -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 }, @@ -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, diff --git a/features/isonline/data.json b/features/isonline/data.json index c6c2ddeb..961bfc6b 100644 --- a/features/isonline/data.json +++ b/features/isonline/data.json @@ -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"] diff --git a/features/isonline/script.js b/features/isonline/script.js index 6dd9329c..84664402 100644 --- a/features/isonline/script.js +++ b/features/isonline/script.js @@ -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(); @@ -35,5 +35,4 @@ async function checkUser() { order: 1, }); } -} -checkUser(); +} \ No newline at end of file