From 8bc4f5adf443b595074423904a95dfe2ad2218fc Mon Sep 17 00:00:00 2001 From: hayao Date: Tue, 31 Oct 2023 00:38:30 +0900 Subject: [PATCH] Update: Reload tab when configs are changed in popup --- src/components/SwitchItem.tsx | 2 ++ src/scripts/background.ts | 2 +- src/scripts/content_common.ts | 1 + src/utils/sendMsgToAllTab.ts | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/utils/sendMsgToAllTab.ts diff --git a/src/components/SwitchItem.tsx b/src/components/SwitchItem.tsx index ea5f02d..a38d4c6 100644 --- a/src/components/SwitchItem.tsx +++ b/src/components/SwitchItem.tsx @@ -6,6 +6,7 @@ import { ComponentColor } from "react-daisyui/dist/types"; import { WebSites } from "../class"; import IsTrue from "../utils/isTrue"; +import { sendMsgToAllTab } from "../utils/sendMsgToAllTab"; import { FrontConfig } from "./config"; import { Category } from "./type"; @@ -23,6 +24,7 @@ export function SwitchItem({ config, category, color }: { config: FrontConfig; c return (e: React.ChangeEvent) => { setEnabled(e.target.checked); WebSites[config.id].storage.set(category, e.target.checked.toString()); + sendMsgToAllTab("reload"); }; }, []); diff --git a/src/scripts/background.ts b/src/scripts/background.ts index 1ae6e86..5c91157 100644 --- a/src/scripts/background.ts +++ b/src/scripts/background.ts @@ -5,7 +5,7 @@ import OpenOptions from "../utils/openOptions"; chrome.runtime.onInstalled.addListener(async () => { const storage = new StorageTool("other"); const isInstalled = await storage.get("installed"); - console.log(isInstalled); + //console.log(isInstalled); if (isInstalled != undefined && isTrue(isInstalled)) return; OpenOptions("thanks").then(() => { storage.set("installed", "true"); diff --git a/src/scripts/content_common.ts b/src/scripts/content_common.ts index 53d4dd6..a6a2534 100644 --- a/src/scripts/content_common.ts +++ b/src/scripts/content_common.ts @@ -5,4 +5,5 @@ chrome.runtime.onMessage.addListener((message) => { if (message === "reload") { location.reload(); } + //return true; }); diff --git a/src/utils/sendMsgToAllTab.ts b/src/utils/sendMsgToAllTab.ts new file mode 100644 index 0000000..6e60ab7 --- /dev/null +++ b/src/utils/sendMsgToAllTab.ts @@ -0,0 +1,35 @@ +export const sendMsgToAllTab = (msg: T) => { + const urls = getUrlsFromManifest(); + chrome.tabs.query( + { + url: urls, + }, + (tabs) => { + tabs.forEach((tab) => { + try { + chrome.tabs.sendMessage(tab.id!, msg); + } catch (e) { + console.error(e); + } + }); + }, + ); +}; + +export const getUrlsFromManifest = () => { + const manifest = chrome.runtime.getManifest(); + const content_scripts = manifest.content_scripts; + if (!content_scripts) return []; + + // (string | undefined)[] => string[] + // なんで動くのか素でわからん + // asとの違いとか、そもそもなんでこれがないとダメなのか。普通に推論して欲しい。 + // //https://qiita.com/suin/items/cda9af4f4f1c53c05c6f + return content_scripts + .map((c) => { + return c.matches; + }) + + .flat() + .filter((c): c is string => typeof c === "string"); +};