forked from lencx/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
229 lines (208 loc) · 6.7 KB
/
core.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @name core.js
* @version 0.1.2
* @url https://github.com/lencx/ChatGPT/tree/main/scripts/core.js
*/
function coreInit() {
const uid = () => window.crypto.getRandomValues(new Uint32Array(1))[0];
function transformCallback(callback = () => {}, once = false) {
const identifier = uid();
const prop = `_${identifier}`;
Object.defineProperty(window, prop, {
value: (result) => {
if (once) {
Reflect.deleteProperty(window, prop);
}
return callback(result);
},
writable: false,
configurable: true,
});
return identifier;
}
async function invoke(cmd, args) {
return new Promise((resolve, reject) => {
if (!window.__TAURI_POST_MESSAGE__) reject('__TAURI_POST_MESSAGE__ does not exist!');
const callback = transformCallback((e) => {
resolve(e);
Reflect.deleteProperty(window, `_${error}`);
}, true);
const error = transformCallback((e) => {
reject(e);
Reflect.deleteProperty(window, `_${callback}`);
}, true);
window.__TAURI_POST_MESSAGE__({
cmd,
callback,
error,
...args,
});
});
}
async function message(message) {
invoke('messageDialog', {
__tauriModule: 'Dialog',
message: {
cmd: 'messageDialog',
message: message.toString(),
title: null,
type: null,
buttonLabel: null,
},
});
}
window.uid = uid;
window.invoke = invoke;
window.message = message;
window.transformCallback = transformCallback;
async function init() {
if (__TAURI_METADATA__.__currentWindow.label === 'tray') {
document.getElementsByTagName('html')[0].style['font-size'] = '70%';
}
async function platform() {
return invoke('platform', {
__tauriModule: 'Os',
message: { cmd: 'platform' },
});
}
if (__TAURI_METADATA__.__currentWindow.label !== 'tray') {
const _platform = await platform();
const chatConf = (await invoke('get_app_conf')) || {};
if (/darwin/.test(_platform) && !chatConf.titlebar) {
const topStyleDom = document.createElement('style');
topStyleDom.innerHTML = `#chatgpt-app-window-top{position:fixed;top:0;z-index:999999999;width:100%;height:24px;background:transparent;cursor:grab;cursor:-webkit-grab;user-select:none;-webkit-user-select:none;}#chatgpt-app-window-top:active {cursor:grabbing;cursor:-webkit-grabbing;}`;
document.head.appendChild(topStyleDom);
const topDom = document.createElement('div');
topDom.id = 'chatgpt-app-window-top';
document.body.appendChild(topDom);
if (window.location.host === 'chat.openai.com') {
const intervalId = setInterval(function () {
const nav = document.body.querySelector('nav');
if (nav) {
nav.style.paddingTop = '25px';
clearInterval(intervalId);
}
}, 1000);
}
topDom.addEventListener('mousedown', () => invoke('drag_window'));
topDom.addEventListener('touchstart', () => invoke('drag_window'));
topDom.addEventListener('dblclick', () => invoke('fullscreen'));
}
}
document.addEventListener('click', (e) => {
const origin = e.target.closest('a');
if (!origin || !origin.target) return;
if (origin && origin.href && origin.target !== '_self') {
invoke('open_link', { url: origin.href });
}
});
// Fix Chinese input method "Enter" on Safari
document.addEventListener(
'keydown',
(e) => {
if (e.keyCode == 229) e.stopPropagation();
},
true,
);
if (window.location.host === 'chat.openai.com') {
window.__sync_prompts = async function () {
await invoke('sync_prompts', { time: Date.now() });
};
}
coreZoom();
window.__LoadingMask = LoadingMask;
}
function coreZoom() {
const styleDom = document.createElement('style');
styleDom.innerHTML = `
#ZoomTopTip {
display: none;
position: fixed;
top: 0;
right: 20px;
background: #2a2a2a;
color: #fafafa;
padding: 20px 15px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
font-size: 16px;
font-weight: bold;
z-index: 999999;
box-shadow: 0 2px 2px 2px #d8d8d8;
}
.ZoomTopTipAni {
transition: opacity 200ms, display 200ms;
display: none;
opacity: 0;
}
`;
document.head.append(styleDom);
const zoomTipDom = document.createElement('div');
zoomTipDom.id = 'ZoomTopTip';
document.body.appendChild(zoomTipDom);
function zoom(callback) {
if (window.zoomSetTimeout) clearTimeout(window.zoomSetTimeout);
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%';
const html = document.getElementsByTagName('html')[0];
const zoom = callback(htmlZoom);
html.style.zoom = zoom;
window.localStorage.setItem('htmlZoom', zoom);
zoomTipDom.innerHTML = zoom;
zoomTipDom.style.display = 'block';
zoomTipDom.classList.remove('ZoomTopTipAni');
window.zoomSetTimeout = setTimeout(() => {
zoomTipDom.classList.add('ZoomTopTipAni');
}, 2500);
}
function zoomDefault() {
const htmlZoom = window.localStorage.getItem('htmlZoom');
if (htmlZoom) {
document.getElementsByTagName('html')[0].style.zoom = htmlZoom;
}
}
function zoomIn() {
zoom((htmlZoom) => `${Math.min(parseInt(htmlZoom) + 10, 200)}%`);
}
function zoomOut() {
zoom((htmlZoom) => `${Math.max(parseInt(htmlZoom) - 10, 30)}%`);
}
function zoom0() {
zoom(() => `100%`);
}
zoomDefault();
window.__zoomIn = zoomIn;
window.__zoomOut = zoomOut;
window.__zoom0 = zoom0;
}
function LoadingMask(text = 'Loading...') {
// 创建遮罩元素
const loadingOverlay = document.createElement('div');
function startLoading() {
loadingOverlay.style = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.5);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;`;
loadingOverlay.innerHTML = `<div style="color:#fff;font-weight:bold">${text}</div>`;
document.body.style.overflow = 'hidden';
document.body.appendChild(loadingOverlay);
}
function stopLoading() {
document.body.style.overflow = null;
loadingOverlay.remove();
}
return { startLoading, stopLoading };
}
init();
}
if (document.readyState === 'complete' || document.readyState === 'interactive') {
coreInit();
} else {
document.addEventListener('DOMContentLoaded', coreInit);
}