Skip to content

Commit

Permalink
Update: Supports firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Nov 9, 2023
1 parent 72a8ddd commit 3a179ad
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/node": "^20.8.10",
"@types/react": "^18.2.36",
"@types/react-dom": "^18.2.14",
"@types/webextension-polyfill": "^0.10.6",
"@types/webpack": "^5.28.4",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
Expand Down Expand Up @@ -71,6 +72,7 @@
"ts-node": "^10.9.1",
"tsconfig-paths-webpack-plugin": "^4.1.0",
"typescript": "^5.2.2",
"webextension-polyfill": "^0.10.0",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-remove-empty-scripts": "^1.0.4"
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/class/Moodle/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Browser from "webextension-polyfill";

import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";

export const injectLink = () => {
Expand All @@ -10,7 +12,7 @@ export const injectLink = () => {

export const replaceLMSLogo = () => {
document.querySelectorAll(".logo").forEach((e) => {
if (e.getAttribute("src")) e.setAttribute("src", chrome.runtime.getURL("assets/GULMS.png"));
if (e.getAttribute("src")) e.setAttribute("src", Browser.runtime.getURL("assets/GULMS.png"));
});
};

Expand Down
4 changes: 3 additions & 1 deletion src/class/Moodle/enableRainbow.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Browser from "webextension-polyfill";

import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { RainbowApplicator } from "../ClassApplicator";
import { GundaiWebSite } from "../UnivWebsite";
Expand Down Expand Up @@ -26,7 +28,7 @@ const textAndBg = (rainbow: RainbowApplicator) => {
};
const replaceIconToGamimg = () => {
document.querySelectorAll("img.userpicture").forEach((e) => {
if (e.getAttribute("src")) e.setAttribute("src", chrome.runtime.getURL("assets/partyparrot.gif"));
if (e.getAttribute("src")) e.setAttribute("src", Browser.runtime.getURL("assets/partyparrot.gif"));
});
};
const replaceTextToGaimg = (moodle: GundaiWebSite<MoodleAdditionalInfo>) => {
Expand Down
6 changes: 4 additions & 2 deletions src/class/StorageTool/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 最近が物忘れが激しい。どうしてだろうか。
// 人間は嫌なことやつらいこと、思い出さないようなことはすぐに忘れるようにできているらしい。
// となると、最近はつらいことが多いのかもしれない。例えばchrome.storage.sync.get()の返り値がany型であることとか。
// なんか非同期処理が動かないとき闇雲にasync awaitをつけるのがつらい。

import Browser from "webextension-polyfill";

import IsTrue from "../../utils/isTrue";

Expand All @@ -13,7 +15,7 @@ export default class StorageTool {
}

static getChromeStorage() {
return chrome.storage ? chrome.storage.sync : null;
return Browser.storage ? Browser.storage.sync : null;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
5 changes: 3 additions & 2 deletions src/popup/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DetailedHTMLProps, useEffect, useState } from "react";
import { Tooltip } from "react-daisyui";
import { ComponentColor } from "react-daisyui/dist/types";
import { FaGamepad, FaMoon } from "react-icons/fa";
import browser from "webextension-polyfill";

import { SwitchItem } from "../../components/SwitchItem";
import { WebsiteConfig, Websites } from "../../data/websites";
Expand All @@ -13,8 +14,8 @@ export default function Main(props: MainProps) {

useEffect(() => {
try {
if (chrome.tabs === undefined) return;
chrome.tabs.query({ active: true, currentWindow: true }, (e) => {
if (browser.tabs === undefined) return;
browser.tabs.query({ active: true, currentWindow: true }).then((e) => {
const url = e[0].url;
if (url?.startsWith(`chrome-extension://${chrome.runtime.id}/options.html`)) {
setShowSwitchClass(false);
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/background.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import browser from "webextension-polyfill";

import StorageTool from "@/class/StorageTool";
import isTrue from "@/utils/isTrue";
import OpenOptions from "@/utils/openOptions";

chrome.runtime.onInstalled.addListener(async () => {
browser.runtime.onInstalled.addListener(async () => {
const storage = new StorageTool("other");
const isInstalled = await storage.get("installed");
//console.log(isInstalled);
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/content_common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "@/styles/gaming.scss";
import "@/styles/darkmode.scss";

chrome.runtime.onMessage.addListener((message) => {
import browser from "webextension-polyfill";

browser.runtime.onMessage.addListener((message) => {
if (message === "reload") {
location.reload();
}
Expand Down
7 changes: 4 additions & 3 deletions src/utils/openOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import browser from "webextension-polyfill";
type OpenOptionsArgs = "home" | "about" | "thanks" | undefined;

export default function openOptions(args: OpenOptionsArgs) {
if (!chrome || !chrome.tabs) {
if (!browser || !browser.tabs) {
console.error("chrome.tabs is not defined");
return;
}

if (!args || args === "home") return chrome.tabs.create({ url: "options.html" });
if (!args || args === "home") return browser.tabs.create({ url: "options.html" });

return chrome.tabs.create({ url: `options.html#/${args}` });
return browser.tabs.create({ url: `options.html#/${args}` });
}
16 changes: 8 additions & 8 deletions src/utils/sendMsgToAllTab.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import browser from "webextension-polyfill";
export const sendMsgToAllTab = <T>(msg: T) => {
if (!isServiceWorkerScript()) return;
const urls = getUrlsFromManifest();
chrome.tabs.query(
{
browser.tabs
.query({
url: urls,
},
(tabs) => {
})
.then((tabs) => {
tabs.forEach((tab) => {
try {
chrome.tabs.sendMessage(tab.id!, msg);
browser.tabs.sendMessage(tab.id!, msg);
} catch (e) {
console.error(e);
}
});
},
);
});
};

export const isServiceWorkerScript = () => {
return location.protocol === "chrome-extension:";
};

export const getUrlsFromManifest = () => {
const manifest = chrome.runtime.getManifest();
const manifest = browser.runtime.getManifest();
const content_scripts = manifest.content_scripts;
if (!content_scripts) return [];

Expand Down

0 comments on commit 3a179ad

Please sign in to comment.