Skip to content

Commit

Permalink
Rename: Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayao0819 committed Oct 26, 2023
1 parent 2d7af08 commit 2e2ef82
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/class/Kyomu/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 教務は学生の未来を担い、ゲーミングは学生の人生に彩りを加える
// この二つのシステムを統合することで、学生の人生をより豊かにすることができる
import ChangeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML";
import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { GundaiWebSite } from "../UnivWebsite";

export const Kyomu = new GundaiWebSite("kyomu");
Expand Down Expand Up @@ -29,14 +29,14 @@ Kyomu.enableRainbow = function () {
this.rainbowBg.apply(".top_title_header", ".top_now_title");

// トップページのウェルカムメッセージ
ChangeQueryInnerHTML("#loginHeader_lblWelcome_1_lbl", "よおこそ、群馬大学ゲーミング教務システムへ");
ChangeQueryInnerHTML(
changeQueryInnerHTML("#loginHeader_lblWelcome_1_lbl", "よおこそ、群馬大学ゲーミング教務システムへ");
changeQueryInnerHTML(
"#loginHeader_lblWelcome_2_lbl",
"このサイトでは、プレーヤーの群馬大学での𝑮𝒂𝒎𝒊𝒏𝒈 𝑳𝒊𝒇𝒆 𝑺𝒕𝒚𝒍𝒆をいい感じにします",
);

// トップページのお知らせ
ChangeQueryInnerHTML("#AllAnnualList_LblTitle", "ゲーミング ライフ スタイル");
changeQueryInnerHTML("#AllAnnualList_LblTitle", "ゲーミング ライフ スタイル");

// 受信メッセージ一覧
this.rainbowBg.apply("#ctl00_phContents_ctlMesReceive_gridMes tr");
Expand Down
4 changes: 2 additions & 2 deletions src/class/Moodle/DisableRainbow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ChangeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML";
import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { Moodle } from "./type";

export const DisableRainbowBg = (moodle: Moodle) => {
Expand All @@ -8,5 +8,5 @@ export const DisableRainbowBg = (moodle: Moodle) => {
};

export const ReplaceImagesToDefault = () => {
ChangeQueryInnerHTML("#instance-320-header", "現在のログイン人数");
changeQueryInnerHTML("#instance-320-header", "現在のログイン人数");
};
2 changes: 1 addition & 1 deletion src/class/Moodle/EnableRainbow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import changeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML";
import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { GundaiWebSite } from "../UnivWebsite";
import { Moodle } from "./type";
import { MoodleAdditionalInfo } from "./type";
Expand Down
2 changes: 1 addition & 1 deletion src/class/MyLibrary/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ChangeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML";
import ChangeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { GundaiWebSite } from "../UnivWebsite";

export const MyLibrary = new GundaiWebSite("mylibrary");
Expand Down
2 changes: 1 addition & 1 deletion src/class/SSO/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 情報は自身の資産である ログインとはその証明である
// 古代において古墳は富の象徴とされた ログインは現代の古墳であると言える

import changeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML";
import changeQueryInnerHTML from "../../utils/changeQueryInnerHTML";
import { GundaiWebSite } from "../UnivWebsite";

export const SSO = new GundaiWebSite("sso");
Expand Down
2 changes: 1 addition & 1 deletion src/class/StorageTool/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import IsTrue from "../../utils/IsTrue";
import IsTrue from "../../utils/isTrue";

export class StorageTool {
id: string;
Expand Down
7 changes: 4 additions & 3 deletions src/class/UnivWebsite/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 無駄とは人生である。無駄を極めよ。無駄を愛せ。無駄を生きろ。
// 無駄を以て物を成す者は、無駄を以て物を滅ぼす者に勝る。x

import { AddClass, Elements, RemoveClass } from "../../utils/AddClass";
import { addClass, Elements, removeClass } from "../../utils/addClass";
import { StorageTool } from "../StorageTool";
//import Storage from "../../utils/Storage";

Expand All @@ -19,6 +19,7 @@ export class UnivWebsite<T> {

rainbowText: Rainbow;
rainbowBg: Rainbow;

rainbowTextShadow: Rainbow;
rainbowBgShadow: Rainbow;

Expand Down Expand Up @@ -72,9 +73,9 @@ export class Rainbow {
}

apply(...elements: Elements[]) {
AddClass(elements, [this.base, ...this.classes]);
addClass(elements, [this.base, ...this.classes]);
}
remove(...elements: Elements[]) {
RemoveClass(elements, [this.base, ...this.classes]);
removeClass(elements, [this.base, ...this.classes]);
}
}
2 changes: 1 addition & 1 deletion src/components/SwitchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useCallback, useEffect, useState } from "react";

import { WebSites } from "../class";
import { FrontConfig } from "../popup/config";
import IsTrue from "../utils/IsTrue";
import IsTrue from "../utils/isTrue";

export function SwitchItem({ config, className }: { config: FrontConfig; className?: string }) {
const [enabled, setEnabled] = useState(false);
Expand Down
2 changes: 1 addition & 1 deletion src/options/pages/Top.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useState } from "react";
import { StorageTool } from "../../class/StorageTool";
import Heading from "../../components/Heading";
import { Switches } from "../../components/Switches";
import IsTrue from "../../utils/IsTrue";
import IsTrue from "../../utils/isTrue";

export default function Top() {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/popup/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import OpenOptions from "../../utils/OpenOptions";
import OpenOptions from "../../utils/openOptions";

export default function Footer() {
return (
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/background.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { StorageTool } from "../class/StorageTool";
import IsTrue from "../utils/IsTrue";
import OpenOptions from "../utils/OpenOptions";
import isTrue from "../utils/isTrue";
import OpenOptions from "../utils/openOptions";

chrome.runtime.onInstalled.addListener(async () => {
const storage = new StorageTool("other");
const isInstalled = await storage.get("installed");
console.log(isInstalled);
if (isInstalled != undefined && IsTrue(isInstalled)) return;
if (isInstalled != undefined && isTrue(isInstalled)) return;
OpenOptions("thanks").then(() => {
storage.set("installed", "true");
});
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/content_kyomu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "/styles/kyomu.css";

import { Kyomu } from "../class";
import LoadGamingWebsite from "../utils/LoadGamingWebsite";
import LoadGamingWebsite from "../utils/loadGamingWebsite";

LoadGamingWebsite(Kyomu);
2 changes: 1 addition & 1 deletion src/scripts/content_media.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "/styles/media.css";

import { Media } from "../class";
import loadGamingWebsite from "../utils/LoadGamingWebsite";
import loadGamingWebsite from "../utils/loadGamingWebsite";

loadGamingWebsite(Media);
2 changes: 1 addition & 1 deletion src/scripts/content_moodle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "/styles/moodle.scss";

import { Moodle } from "../class";
import loadGamingWebsite from "../utils/LoadGamingWebsite";
import loadGamingWebsite from "../utils/loadGamingWebsite";

//window.addEventListener("load", async () => await runFuncIfEnabled(Moodle.EnableRainbow));
loadGamingWebsite(Moodle);
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/content_mylib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "/styles/mylibrary.css";

import { MyLibrary } from "../class";
import LoadGamingWebsite from "../utils/LoadGamingWebsite";
import LoadGamingWebsite from "../utils/loadGamingWebsite";

LoadGamingWebsite(MyLibrary);
2 changes: 1 addition & 1 deletion src/scripts/content_sso.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "/styles/sso.css";

import { SSO } from "../class";
import loadGamingWebsite from "../utils/LoadGamingWebsite";
import loadGamingWebsite from "../utils/loadGamingWebsite";

//window.addEventListener("load", async () => await runFuncIfEnabled(SSO.EnableRainbow));

Expand Down
71 changes: 71 additions & 0 deletions src/utils/addClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export type Elements = HTMLElement | NodeListOf<HTMLElement> | string;

export function addClass(elements: Elements[], classNames: string[]) {
elements.forEach((element) => {
if (typeof element === "string") {
const elements = document.querySelectorAll(element);
elements.forEach((element) => {
//console.log("adding classes to", element);
element.classList.add(...classNames);
});
} else {
let list: HTMLElement[];
if (element instanceof HTMLElement) list = [element];
else list = Array.from(element);

list.forEach((element) => {
//console.log("adding classes to", element);
element.classList.add(...classNames);
});
}
});
}

export function removeClass(elements: Elements[], classNames: string[]) {
elements.forEach((element) => {
if (typeof element === "string") {
const elements = document.querySelectorAll(element);
elements.forEach((element) => {
element.classList.remove(...classNames);
});
} else {
let list: HTMLElement[];
if (element instanceof HTMLElement) list = [element];
else list = Array.from(element);

list.forEach((element) => {
element.classList.remove(...classNames);
});
}
});
}

/*
export function AddRainbowBg(...queries: string[]) {
AddClass(queries, ["rainbow-bg"]);
}
export function AddRainbowBgWithShadow(...queries: string[]) {
AddClass(queries, ["rainbow-bg", "rainbow-bg-shadow"]);
}
export function AddRainbowText(...queries: string[]) {
AddClass(queries, ["rainbow-text"]);
}
export function AddRainbowTextWithShadow(...queries: string[]) {
AddClass(queries, ["rainbow-text", "rainbow-text-shadow"]);
}
export function RemoveRainbowBg(...queries: string[]) {
RemoveClass(queries, ["rainbow-bg"]);
}
export function RemoveRainbowBgWithShadow(...queries: string[]) {
RemoveClass(queries, ["rainbow-bg", "rainbow-bg-shadow"]);
}
export function RemoveRainbowText(...queries: string[]) {
RemoveClass(queries, ["rainbow-text", "rainbow-text-shadow"]);
}
*/
6 changes: 6 additions & 0 deletions src/utils/changeQueryInnerHTML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function changeQueryInnerHTML(query: string, innerHTML: string) {
const elements = document.querySelectorAll(query);
elements.forEach((element) => {
element.innerHTML = innerHTML;
});
}
4 changes: 4 additions & 0 deletions src/utils/isTrue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function isTrue(value: any) {
return value === true || value === "true" || value === undefined;
}
15 changes: 15 additions & 0 deletions src/utils/loadGamingWebsite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { UnivWebsite } from "../class/UnivWebsite";
import isTrue from "./isTrue";
//import RunFuncIfEnabled from "./RunFuncIfEnabled";

// ウィンドウが読み込まれたらGundaiWebsiteのEnableRainbowを実行する
export default function loadGamingWebsite(website: UnivWebsite<unknown>) {
window.addEventListener("load", async () => {
const isEnabled = await website.storage.get("enabled");
if (isTrue(isEnabled)) {
website.enable();
} else {
website.disable();
}
});
}
7 changes: 7 additions & 0 deletions src/utils/openOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type OpenOptionsArgs = "home" | "about" | "thanks" | undefined;

export default function openOptions(args: OpenOptionsArgs) {
if (!args || args === "home") return chrome.tabs.create({ url: "options.html" });

return chrome.tabs.create({ url: `options.html#/${args}` });
}

0 comments on commit 2e2ef82

Please sign in to comment.