Skip to content

Commit

Permalink
Update: Reload tab when configs are changed in popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Oct 30, 2023
1 parent 60e5b03 commit 8bc4f5a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/SwitchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -23,6 +24,7 @@ export function SwitchItem({ config, category, color }: { config: FrontConfig; c
return (e: React.ChangeEvent<HTMLInputElement>) => {
setEnabled(e.target.checked);
WebSites[config.id].storage.set(category, e.target.checked.toString());
sendMsgToAllTab<string>("reload");
};
}, []);

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions src/scripts/content_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ chrome.runtime.onMessage.addListener((message) => {
if (message === "reload") {
location.reload();
}
//return true;
});
35 changes: 35 additions & 0 deletions src/utils/sendMsgToAllTab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export const sendMsgToAllTab = <T>(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");
};

0 comments on commit 8bc4f5a

Please sign in to comment.