From 7ff09af35e92259599cf6f0e4b4666bf1cec170a Mon Sep 17 00:00:00 2001 From: hayao Date: Mon, 16 Oct 2023 14:47:53 +0900 Subject: [PATCH] Fix: Moodle undefined this variable --- src/class/GundaiWebsite/index.ts | 8 +++--- src/class/Moodle/DisableRainbow.ts | 14 ++++------ src/class/Moodle/EnableRainbow.ts | 44 ++++++++++++++++++++---------- src/class/Moodle/index.ts | 28 +++++++++++++++---- 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/src/class/GundaiWebsite/index.ts b/src/class/GundaiWebsite/index.ts index f6184fa..95a35bd 100644 --- a/src/class/GundaiWebsite/index.ts +++ b/src/class/GundaiWebsite/index.ts @@ -4,13 +4,13 @@ // GundaiWebSiteはゲーミング化するウェブサイトを定義したクラス // 型変数とAdditionalInfoプロパティによって任意の情報を追加できる export default class GundaiWebSite { - EnableRainbow?: () => void; - DisableRainbow?: () => void; constructor(enable?: () => void, disable?: () => void) { - this.EnableRainbow = enable; - this.DisableRainbow = disable; + if (enable) this.EnableRainbow = enable; + if (disable) this.DisableRainbow = disable; this.AdditionalInfo = {} as T; } + EnableRainbow() {} + DisableRainbow() {} AdditionalInfo: T; } diff --git a/src/class/Moodle/DisableRainbow.ts b/src/class/Moodle/DisableRainbow.ts index 99089e9..771c916 100644 --- a/src/class/Moodle/DisableRainbow.ts +++ b/src/class/Moodle/DisableRainbow.ts @@ -1,16 +1,12 @@ import { RemoveRainbowBg } from "../../utils/AddClass"; import changeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML"; -import GundaiWebSite from "../GundaiWebsite"; -import { MoodleAdditionalInfo } from "./type"; -export default function DisableRainbowFunc(this: GundaiWebSite) { - changeQueryInnerHTML("#instance-320-header", "現在のログイン人数"); +export const DisableRainbowBg = () => { RemoveRainbowBg(".navbar", ".addinghtml"); RemoveRainbowBg(".page-header-headings h1"); RemoveRainbowBg("a"); +}; - const headerText = this.AdditionalInfo.headerText; - document.querySelectorAll(".page-header-headings h1").forEach((e) => { - if (e.innerHTML.includes(headerText)) e.innerHTML.replace(headerText, ""); - }); -} +export const ReplaceImagesToDefault = () => { + changeQueryInnerHTML("#instance-320-header", "現在のログイン人数"); +}; diff --git a/src/class/Moodle/EnableRainbow.ts b/src/class/Moodle/EnableRainbow.ts index 1102b0f..b063955 100644 --- a/src/class/Moodle/EnableRainbow.ts +++ b/src/class/Moodle/EnableRainbow.ts @@ -3,17 +3,24 @@ import changeQueryInnerHTML from "../../utils/ChangeQueryInnerHTML"; import GundaiWebSite from "../GundaiWebsite"; import { MoodleAdditionalInfo } from "./type"; -export default function EnableRainbowFunc(this: GundaiWebSite) { - const headerText = this.AdditionalInfo.headerText; - document.querySelectorAll(".page-header-headings h1").forEach((e) => { - if (!e.innerHTML.includes(headerText)) e.innerHTML = e.innerHTML + headerText; - }); - - changeQueryInnerHTML("#instance-320-header", "現在のプレイ人数"); - AddRainbowBg(".navbar", ".addinghtml"); +export const EnableRainbowText = () => { AddRainbowText(".page-header-headings h1"); AddRainbowText("a"); +}; + +export const EnableRainbowBg = () => { + AddRainbowBg(".navbar", ".addinghtml"); +}; + +export const InjectLink = () => { + const supportSection = document.querySelector(".footer-support-link"); + if (supportSection) + supportSection.innerHTML = + supportSection.innerHTML + + `Gaming Edition開発者に連絡する`; +}; +export const ReplaceImagesToGamimg = () => { document.querySelectorAll(".logo").forEach((e) => { if (e.getAttribute("src")) e.setAttribute("src", chrome.runtime.getURL("assets/GULMS.png")); }); @@ -21,10 +28,19 @@ export default function EnableRainbowFunc(this: GundaiWebSite { if (e.getAttribute("src")) e.setAttribute("src", chrome.runtime.getURL("assets/partyparrot.gif")); }); +}; - const supportSection = document.querySelector(".footer-support-link"); - if (supportSection) - supportSection.innerHTML = - supportSection.innerHTML + - `Gaming Edition開発者に連絡する`; -} +export const ReplaceTextToGaimgFunc = (moodle: GundaiWebSite) => { + return () => { + const headerText = moodle.AdditionalInfo.headerText; + + document.querySelectorAll(".page-header-headings h1").forEach((e) => { + if (!e.innerHTML.includes(headerText)) e.innerHTML = e.innerHTML + headerText; + }); + document.querySelectorAll(".page-header-headings h1").forEach((e) => { + if (!e.innerHTML.includes(headerText)) e.innerHTML = e.innerHTML + headerText; + }); + + changeQueryInnerHTML("#instance-320-header", "現在のプレイ人数"); + }; +}; diff --git a/src/class/Moodle/index.ts b/src/class/Moodle/index.ts index c4da91f..9d11a28 100644 --- a/src/class/Moodle/index.ts +++ b/src/class/Moodle/index.ts @@ -1,11 +1,29 @@ // 情報の信頼性は社会の構築において重要な要素である // 個人の信頼性は家庭の構築において重要な要素である import GundaiWebSite from "../GundaiWebsite"; -import DisableRainbow from "./DisableRainbow"; -import EnableRainbow from "./EnableRainbow"; +import { DisableRainbowBg, ReplaceImagesToDefault } from "./DisableRainbow"; +import { EnableRainbowBg, EnableRainbowText, InjectLink, ReplaceImagesToGamimg, ReplaceTextToGaimgFunc } from "./EnableRainbow"; import { MoodleAdditionalInfo } from "./type"; export const Moodle = new GundaiWebSite(); -Moodle.AdditionalInfo.headerText = " Gaming Edition🎮"; -Moodle.EnableRainbow = EnableRainbow; -Moodle.DisableRainbow = DisableRainbow; +Moodle.AdditionalInfo = { + headerText: " Gaming Edition🎮", +}; + +Moodle.EnableRainbow = function () { + EnableRainbowBg(); + EnableRainbowText(); + InjectLink(); + ReplaceImagesToGamimg(); + ReplaceTextToGaimgFunc(this)(); +}; + +Moodle.DisableRainbow = function () { + DisableRainbowBg(); + ReplaceImagesToDefault(); + + const headerText = this.AdditionalInfo.headerText; + document.querySelectorAll(".page-header-headings h1").forEach((e) => { + if (e.innerHTML.includes(headerText)) e.innerHTML.replace(headerText, ""); + }); +};