diff --git a/src/background/events/injects/st_detect_inject.ts b/src/background/events/injects/st_detect_inject.ts new file mode 100644 index 0000000..e7957e1 --- /dev/null +++ b/src/background/events/injects/st_detect_inject.ts @@ -0,0 +1,10 @@ +import { Module } from "../../../types/module" + +export default { + action: async (base) => { + await chrome.scripting.executeScript({ + target: { tabId: base.tab_id }, + files: ["detect_schooltape.js"], + }) + }, +} diff --git a/src/background/events/on_update.ts b/src/background/events/on_update.ts index a02ce50..7e8e383 100644 --- a/src/background/events/on_update.ts +++ b/src/background/events/on_update.ts @@ -8,6 +8,7 @@ import box_of_books from "./injects/box_of_books" import launcher_shortcut from "./injects/launcher_shortcut" import news_tracker_fetch from "./injects/news_tracker" import news_tracker_display from "./injects/display_history" +import detect_schooltape from "./injects/st_detect_inject" const INJECTS = [ dark_theme_css, @@ -17,6 +18,7 @@ const INJECTS = [ launcher_shortcut, news_tracker_fetch, news_tracker_display, + detect_schooltape ] export default async function on_update(tab_id: number, _: any, tab: chrome.tabs.Tab) { diff --git a/src/background/set_default_settings.ts b/src/background/set_default_settings.ts index 3d1bbec..25f0a95 100644 --- a/src/background/set_default_settings.ts +++ b/src/background/set_default_settings.ts @@ -29,3 +29,17 @@ export async function init_settings(): Promise { await poll_feed() } + +export async function set_setting(key: K, value: Settings[K]): Promise { + const current_settings = await chrome.storage.sync.get("settings") + if (!current_settings.settings) { + console.error("Settings not initialized yet.") + return + } + + const new_settings = { + ...current_settings.settings, + [key]: value, + } + await chrome.storage.sync.set({ settings: new_settings }) +} diff --git a/src/content/auto_detection/detect_schooltape.ts b/src/content/auto_detection/detect_schooltape.ts index 890d3aa..0c9ee7b 100644 --- a/src/content/auto_detection/detect_schooltape.ts +++ b/src/content/auto_detection/detect_schooltape.ts @@ -1,3 +1,5 @@ +import { set_setting } from "../../background/set_default_settings" + async function main() { // create marker const marker = document.createElement("div") @@ -8,17 +10,36 @@ async function main() { const observer = new MutationObserver(mutations => { for (let mutation of mutations) { if (mutation.type === "childList") { - // check for updated nodes + // check for added nodes if (mutation.addedNodes.length > 0) { - Array.from(mutation.addedNodes).some(node => node) + Array.from(mutation.addedNodes).forEach(async node => { + if ( + node.nodeType === Node.ELEMENT_NODE && + (node as Element).getAttribute("data-schooltape") === "stylesheet-themes" + ) { + console.log("[Schooltape Detection] Detected Schooltape stylesheet added to the page.") + await set_setting("schooltape_compatibility", true) + } + }) + } + // check for removed nodes + if (mutation.removedNodes.length > 0) { + Array.from(mutation.removedNodes).forEach(async node => { + if ( + node.nodeType === Node.ELEMENT_NODE && + (node as Element).getAttribute("data-schooltape") === "stylesheet-themes" + ) { + console.log("[Schooltape Detection] Detected Schooltape stylesheet removed from the page.") + await set_setting("schooltape_compatibility", false) + } + }) } } } }) + observer.observe(document.head, { childList: true, subtree: true }) } -//8=========D - const marker_elem = document.querySelector("[data-ultrabox-schooltape-marker]") if (!marker_elem) { main() diff --git a/webpack.config.cjs b/webpack.config.cjs index effc87b..35c3189 100644 --- a/webpack.config.cjs +++ b/webpack.config.cjs @@ -25,6 +25,7 @@ module.exports = (env, argv) => { history_puller: "./src/content/modules/post_history_tracking/puller.ts", detect_domain: "./src/content/auto_detection/detect_domain.ts", detect_rss_feed: "./src/content/auto_detection/detect_rss_feed.ts", + detect_schooltape: "./src/content/auto_detection/detect_schooltape.ts", }, output: {