ChatGPT/scripts/core.js

202 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-05-18 07:32:32 +00:00
/**
* @name core.js
* @version 0.1.0
* @url https://github.com/lencx/ChatGPT/tree/main/scripts/core.js
*/
2023-01-08 04:28:54 +00:00
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);
}
2023-05-18 07:32:32 +00:00
return callback(result);
2023-01-08 04:28:54 +00:00
},
writable: false,
configurable: true,
2023-05-18 07:32:32 +00:00
});
2023-01-08 04:28:54 +00:00
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}`);
2023-05-18 07:32:32 +00:00
}, true);
2023-01-08 04:28:54 +00:00
const error = transformCallback((e) => {
reject(e);
Reflect.deleteProperty(window, `_${callback}`);
2023-05-18 07:32:32 +00:00
}, true);
2023-01-08 04:28:54 +00:00
window.__TAURI_POST_MESSAGE__({
cmd,
callback,
error,
2023-05-18 07:32:32 +00:00
...args,
2023-01-08 04:28:54 +00:00
});
});
}
2023-02-01 15:48:35 +00:00
async function message(message) {
invoke('messageDialog', {
__tauriModule: 'Dialog',
message: {
cmd: 'messageDialog',
message: message.toString(),
title: null,
type: null,
2023-05-18 07:32:32 +00:00
buttonLabel: null,
},
2023-02-01 15:48:35 +00:00
});
}
2023-01-08 04:28:54 +00:00
window.uid = uid;
window.invoke = invoke;
2023-02-01 15:48:35 +00:00
window.message = message;
2023-01-08 04:28:54 +00:00
window.transformCallback = transformCallback;
async function init() {
2023-01-08 04:28:54 +00:00
if (__TAURI_METADATA__.__currentWindow.label === 'tray') {
document.getElementsByTagName('html')[0].style['font-size'] = '70%';
}
async function platform() {
return invoke('platform', {
__tauriModule: 'Os',
2023-05-18 07:32:32 +00:00
message: { cmd: 'platform' },
2023-01-08 04:28:54 +00:00
});
}
2023-01-25 11:09:22 +00:00
if (__TAURI_METADATA__.__currentWindow.label !== 'tray') {
const _platform = await platform();
2023-05-18 07:32:32 +00:00
const chatConf = (await invoke('get_app_conf')) || {};
2023-01-25 11:09:22 +00:00
if (/darwin/.test(_platform) && !chatConf.titlebar) {
2023-05-18 07:32:32 +00:00
const topStyleDom = document.createElement('style');
2023-01-25 11:09:22 +00:00
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);
2023-05-18 07:32:32 +00:00
const topDom = document.createElement('div');
topDom.id = 'chatgpt-app-window-top';
2023-01-25 11:09:22 +00:00
document.body.appendChild(topDom);
2023-01-08 04:28:54 +00:00
2023-01-25 11:09:22 +00:00
if (window.location.host === 'chat.openai.com') {
const nav = document.body.querySelector('nav');
if (nav) {
2023-05-18 07:32:32 +00:00
const currentPaddingTop = parseInt(
window
.getComputedStyle(document.querySelector('nav'), null)
.getPropertyValue('padding-top')
.replace('px', ''),
10,
);
const navStyleDom = document.createElement('style');
navStyleDom.innerHTML = `nav{padding-top:${
currentPaddingTop + topDom.clientHeight
}px !important}`;
2023-01-25 11:09:22 +00:00
document.head.appendChild(navStyleDom);
}
2023-01-24 16:38:36 +00:00
}
2023-05-18 07:32:32 +00:00
topDom.addEventListener('mousedown', () => invoke('drag_window'));
topDom.addEventListener('touchstart', () => invoke('drag_window'));
topDom.addEventListener('dblclick', () => invoke('fullscreen'));
2023-01-25 11:09:22 +00:00
}
2023-01-08 04:28:54 +00:00
}
2023-05-18 07:32:32 +00:00
document.addEventListener('click', (e) => {
const origin = e.target.closest('a');
2023-01-08 09:04:49 +00:00
if (!origin || !origin.target) return;
2023-01-08 04:28:54 +00:00
if (origin && origin.href && origin.target !== '_self') {
invoke('open_link', { url: origin.href });
}
});
// Fix Chinese input method "Enter" on Safari
2023-05-18 07:32:32 +00:00
document.addEventListener(
'keydown',
(e) => {
if (e.keyCode == 229) e.stopPropagation();
},
true,
);
2023-01-25 11:09:22 +00:00
if (window.location.host === 'chat.openai.com') {
2023-05-18 07:32:32 +00:00
window.__sync_prompts = async function () {
2023-01-25 11:09:22 +00:00
await invoke('sync_prompts', { time: Date.now() });
2023-05-18 07:32:32 +00:00
};
2023-01-25 11:09:22 +00:00
}
coreZoom();
}
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);
2023-05-18 07:32:32 +00:00
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%';
const html = document.getElementsByTagName('html')[0];
2023-01-25 11:09:22 +00:00
const zoom = callback(htmlZoom);
html.style.zoom = zoom;
2023-05-18 07:32:32 +00:00
window.localStorage.setItem('htmlZoom', zoom);
2023-01-25 11:09:22 +00:00
zoomTipDom.innerHTML = zoom;
zoomTipDom.style.display = 'block';
zoomTipDom.classList.remove('ZoomTopTipAni');
window.zoomSetTimeout = setTimeout(() => {
zoomTipDom.classList.add('ZoomTopTipAni');
}, 2500);
}
function zoomDefault() {
2023-05-18 07:32:32 +00:00
const htmlZoom = window.localStorage.getItem('htmlZoom');
2023-01-25 11:09:22 +00:00
if (htmlZoom) {
2023-05-18 07:32:32 +00:00
document.getElementsByTagName('html')[0].style.zoom = htmlZoom;
2023-01-25 11:09:22 +00:00
}
}
function zoomIn() {
zoom((htmlZoom) => `${Math.min(parseInt(htmlZoom) + 10, 200)}%`);
}
function zoomOut() {
zoom((htmlZoom) => `${Math.max(parseInt(htmlZoom) - 10, 30)}%`);
}
function zoom0() {
zoom(() => `100%`);
2023-01-08 04:28:54 +00:00
}
2023-01-25 11:09:22 +00:00
zoomDefault();
window.__zoomIn = zoomIn;
window.__zoomOut = zoomOut;
window.__zoom0 = zoom0;
}
2023-05-18 07:32:32 +00:00
if (document.readyState === 'complete' || document.readyState === 'interactive') {
init();
} else {
2023-05-18 07:32:32 +00:00
document.addEventListener('DOMContentLoaded', init);
}