1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00
Unofficial_ChatGPT_Client/src-tauri/src/scripts/core.js

195 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-01-07 23:28:54 -05:00
// *** Core Script - IPC ***
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
});
});
}
2023-02-01 10:48:35 -05:00
async function message(message) {
invoke('messageDialog', {
__tauriModule: 'Dialog',
message: {
cmd: 'messageDialog',
message: message.toString(),
title: null,
type: null,
buttonLabel: null
}
});
}
2023-01-07 23:28:54 -05:00
window.uid = uid;
window.invoke = invoke;
2023-02-01 10:48:35 -05:00
window.message = message;
2023-01-07 23:28:54 -05:00
window.transformCallback = transformCallback;
async function init() {
2023-01-07 23:28:54 -05:00
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' }
});
}
2023-01-25 06:09:22 -05:00
if (__TAURI_METADATA__.__currentWindow.label !== 'tray') {
const _platform = await platform();
2023-01-26 11:23:05 -05:00
const chatConf = await invoke('get_app_conf') || {};
2023-01-25 06:09:22 -05:00
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);
2023-01-07 23:28:54 -05:00
2023-01-25 06:09:22 -05:00
if (window.location.host === 'chat.openai.com') {
const nav = document.body.querySelector('nav');
if (nav) {
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}`;
document.head.appendChild(navStyleDom);
}
2023-01-24 11:38:36 -05:00
}
2023-01-25 06:09:22 -05:00
topDom.addEventListener("mousedown", () => invoke("drag_window"));
topDom.addEventListener("touchstart", () => invoke("drag_window"));
topDom.addEventListener("dblclick", () => invoke("fullscreen"));
}
2023-01-07 23:28:54 -05:00
}
document.addEventListener("click", (e) => {
const origin = e.target.closest("a");
2023-01-08 04:04:49 -05:00
if (!origin || !origin.target) return;
2023-01-07 23:28:54 -05:00
if (origin && origin.href && origin.target !== '_self') {
invoke('open_link', { url: origin.href });
}
});
// Fix Chinese input method "Enter" on Safari
2023-02-14 03:18:52 -05:00
document.addEventListener("keydown", (e) => {
if(e.keyCode == 229) e.stopPropagation();
}, true)
2023-01-25 06:09:22 -05:00
if (window.location.host === 'chat.openai.com') {
window.__sync_prompts = async function() {
await invoke('sync_prompts', { time: Date.now() });
}
}
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);
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%`);
2023-01-07 23:28:54 -05:00
}
2023-01-25 06:09:22 -05:00
zoomDefault();
window.__zoomIn = zoomIn;
window.__zoomOut = zoomOut;
window.__zoom0 = zoom0;
2023-04-12 09:15:49 -04:00
window.__clearCache = () => {
window.localStorage.clear();
window.sessionStorage.clear();
window.applicationCache && window.applicationCache.update();
window.location.reload();
}
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
init();
} else {
document.addEventListener("DOMContentLoaded", init);
}