返回 AiToEarn
index.tsx
1 import {
2 ForwardedRef,
3 forwardRef,
4 memo,
5 useEffect,
6 useRef,
7 useState,
8 } from 'react';
9 import { generateUUID } from '../../utils';
10 import styles from './webView.module.scss';
11 import { Spin } from 'antd';
12 import { ICookieParams } from '../../../electron/main/account/BrowserWindow/browserWindow';
13 import { AccountInfo } from '../../views/account/comment';
14 import { PlatType } from '../../../commont/AccountEnum';
15
16 export interface IWebViewRef {}
17
18 export interface IWebViewProps {
19 url: string;
20 cookieParams?: ICookieParams;
21 // 是否开启沙盒化模式
22 partition?: boolean | string;
23 account?: AccountInfo;
24 allowpopups?: boolean;
25 }
26
27 const WebView = memo(
28 forwardRef(
29 (
30 { url, cookieParams, partition, account, allowpopups }: IWebViewProps,
31 ref: ForwardedRef<IWebViewRef>,
32 ) => {
33 const webviewRef = useRef<HTMLWebViewElement>(null);
34 // webView id
35 const [webViewId, setWebViewId] = useState(-1);
36 const [loading, setLoading] = useState(true);
37 // 隔离ID
38 const partitionId = useRef(generateUUID());
39
40 useEffect(() => {
41 console.log(cookieParams);
42 console.log(JSON.stringify(cookieParams));
43 setLoading(true);
44
45 webviewRef.current?.addEventListener('dom-ready', async (e) => {
46 // 每个平台localStorage添加
47 if (account) {
48 let jsCode;
49 if (account?.type === PlatType.Douyin) {
50 jsCode = `
51 localStorage.setItem('douyin_web_hide_guide', '1');
52 localStorage.setItem('user_info', '{"uid":"${account.account}","nickname":"${account.nickname}","avatarUrl":"${account.avatar}"}');
53 localStorage.setItem('useShortcut2', '{"Wed Mar 12 2025":false}');
54 `;
55 }
56 // @ts-ignore
57 webviewRef.current!.executeJavaScript(jsCode);
58 }
59
60 // @ts-ignore
61 if (webviewRef.current?.getURL() === 'about:blank') {
62 // @ts-ignore
63 const id = webviewRef.current!.getWebContentsId();
64 setWebViewId(id);
65
66 // @ts-ignore
67 await window.ipcRenderer.invoke('ICP_ACCOUNT_CREATE_BROWSER_VIEW', {
68 webViewId: id,
69 cookieParams,
70 });
71 }
72 setLoading(false);
73 });
74
75 return () => {
76 window.ipcRenderer.invoke(
77 'ICP_ACCOUNT_DESTROY_BROWSER_VIEW',
78 webViewId,
79 );
80 };
81 }, []);
82
83 return (
84 url && (
85 <div className={styles.webview}>
86 <Spin spinning={loading} tip="加载中...">
87 <webview
88 // @ts-ignore
89 disablewebsecurity={'true'}
90 ref={webviewRef}
91 // @ts-ignore
92 allowpopups={allowpopups ? 'true' : undefined}
93 webpreferences="sandbox"
94 src={loading ? 'about:blank' : url}
95 style={{ width: '100%', height: '100%' }}
96 partition={
97 typeof partition === 'boolean'
98 ? partition
99 ? partitionId.current
100 : undefined
101 : partition
102 }
103 useragent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
104 ></webview>
105 </Spin>
106 </div>
107 )
108 );
109 },
110 ),
111 );
112 WebView.displayName = 'WebView';
113
114 export default WebView;
115
115 lines Plain Text