-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
112 lines (99 loc) · 4 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React, {useEffect, useRef, useState} from 'react';
import './App.module.css';
import '../../assets/main.css'
import {Home} from "@/entrypoints/content/home.tsx";
import {SettingsPage} from "@/entrypoints/content/settings.tsx";
import Sidebar, {SidebarType} from "@/entrypoints/sidebar.tsx";
import {browser} from "wxt/browser";
import ExtMessage, {MessageType} from "@/entrypoints/types.ts";
import Header from "@/entrypoints/content/header.tsx";
import {useTranslation} from "react-i18next";
import {useTheme} from "@/components/theme-provider.tsx";
import { createRoot } from 'react-dom/client';
export default () => {
const [showContent, setShowContent] = useState(true);
const [showButton, setShowButton] = useState(false)
const [showCard, setShowCard] = useState(false)
const [sidebarType, setSidebarType] = useState<SidebarType>(SidebarType.home);
const [headTitle, setHeadTitle] = useState("home")
const [buttonStyle, setButtonStyle] = useState<any>();
const [cardStyle, setCardStyle] = useState<any>();
const cardRef = useRef<HTMLDivElement>(null);
const {i18n} = useTranslation();
const {theme, toggleTheme} = useTheme();
async function initI18n() {
let data = await browser.storage.local.get('i18n');
if (data.i18n) {
await i18n.changeLanguage(data.i18n)
}
}
const containerBox = () => {
const container = document.createElement('div');
const root = createRoot(container);
root.render(
<div className={`h-[300px] w-full bg-slate-50 dark:bg-slate-900 text-black dark:text-white`}>
<p className="text-red-500 text-center text-2xl">container box</p>
</div>
);
return container;
}
function injectTailwind() {
const nameElement = document.querySelector(".name");
if (nameElement) {
const textGroupParent = nameElement.parentElement;
if (textGroupParent) {
textGroupParent.insertBefore(containerBox(), textGroupParent.firstChild);
}
}
}
function domLoaded() {
console.log("dom loaded")
injectTailwind();
}
useEffect(() => {
if (document.readyState === "complete") {
// load event has already fired, run your code or function here
console.log("dom complete")
domLoaded();
} else {
// load event hasn't fired, listen for it
window.addEventListener('load', () => {
// your code here
console.log("content load:")
console.log(window.location.href)
domLoaded();
});
}
browser.runtime.onMessage.addListener((message: ExtMessage, sender, sendResponse) => {
console.log('content:')
console.log(message)
if (message.messageType == MessageType.clickExtIcon) {
setShowContent(true);
} else if (message.messageType == MessageType.changeLocale) {
i18n.changeLanguage(message.content)
} else if (message.messageType == MessageType.changeTheme) {
toggleTheme(message.content)
}
});
initI18n();
}, []);
return (
<div className={theme}>
{showContent && <div
className="fixed top-0 right-0 h-screen w-[400px] bg-background z-[1000000000000] rounded-l-xl shadow-2xl">
<Header headTitle={headTitle}/>
<Sidebar closeContent={() => {
setShowContent(false)
}} sideNav={(sidebarType: SidebarType) => {
setSidebarType(sidebarType)
setHeadTitle(sidebarType)
}}/>
<main className="mr-14 grid gap-4 p-4">
{sidebarType === SidebarType.home && <Home/>}
{sidebarType === SidebarType.settings && <SettingsPage/>}
</main>
</div>
}
</div>
)
};