From ea90444b94c7717fe9eddee2f6ba793335d72034 Mon Sep 17 00:00:00 2001 From: jianjian Date: Tue, 23 May 2023 19:59:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/server.js | 88 +++++++++++++++-- web/DrawImg.html | 14 +++ web/Write.html | 14 +++ web/css/DrawImg.css | 99 +++++++++++++++++++ web/css/Wtite.css | 98 ++++++++++++++++++ web/css/bing.css | 2 +- web/js/DrawImg.js | 4 + web/js/NewBingGoGo.js | 17 +--- web/js/Wtite.js | 2 + web/js/module/ChatRecord/ChatRecord.js | 2 +- .../module/aToos/{DateFormat.js => AToos.js} | 17 +++- 11 files changed, 330 insertions(+), 27 deletions(-) rename web/js/module/aToos/{DateFormat.js => AToos.js} (61%) diff --git a/server/server.js b/server/server.js index 1751a08..12aeacf 100644 --- a/server/server.js +++ b/server/server.js @@ -1,3 +1,6 @@ +let serverConfig = {};//用于预加载,来加速页面打开 +let serverConfigOk = false; + self.addEventListener("fetch",function(event) { if (event.request.url.startsWith(chrome.runtime.getURL(''))){ event.respondWith(handleRequest(event.request)) @@ -6,6 +9,72 @@ self.addEventListener("fetch",function(event) { // console.log(event); }); + +/** + * 当链接更新时重新预加载 + * */ +chrome.storage.onChanged.addListener(async (c,l) =>{ + if(l!=="local"){ + return; + } + if(!c.GoGoUrl){ + return; + } + loadServerConfig().then() +}) +/** + * 加载服务器配置 + * */ +async function loadServerConfig(){ + if(serverConfigOk==='loading'){ + return; + } + serverConfigOk = 'loading' + //先从本地加载 + try { + serverConfig = await (await fetch(chrome.runtime.getURL('/web/resource/config.json'))).json(); + }catch (error){ + console.warn(error); + } + + //去魔法链接服务加 + while (true){ + let conf; + let mUrl + try { + mUrl = uRLTrue(await getMagicUrl()); + let re = await fetch(mUrl+'/web/resource/config.json'); + if(re && re.ok){ + conf = await re.json(); + } + }catch (error){ + console.warn(error); + } + if(!mUrl){ // 如果没有魔法链接则加载失败 + serverConfigOk = false; + return; + } + try { + if(mUrl!==uRLTrue(await getMagicUrl())){//如果魔法链接变化了则重新加载 + continue; + } + if (!conf){//没有配置则加载失败 + serverConfigOk = false; + return; + }else {//否则加载成功 + serverConfig = conf; + serverConfigOk = true; + return; + } + }catch (error){//错误了则加载失败 + console.warn(error); + serverConfigOk = false; + return; + } + } +} +loadServerConfig().then(); + async function handleRequest(request){ let url = new URL(request.url); @@ -25,17 +94,16 @@ async function handleRequest(request){ //server配置请求 if(url.pathname==='/web/resource/config.json'){ - let re; - try { - let mUrl = uRLTrue(await getMagicUrl()) ; - re = await fetch(mUrl+'/web/resource/config.json'); - }catch (error){ - console.warn(re); + if(!serverConfigOk){ + loadServerConfig().then(); } - if(!re || !re.ok){ - re = await fetch(chrome.runtime.getURL('/web/resource/config.json')); - } - return re + return new Response(JSON.stringify(serverConfig),{ + status: 200, + statusText: 'ok', + headers: { + "content-type": "application/json" + } + }); } //web请求 diff --git a/web/DrawImg.html b/web/DrawImg.html index 7792437..bc1cafa 100644 --- a/web/DrawImg.html +++ b/web/DrawImg.html @@ -8,6 +8,20 @@ + +
+
+
+
+
+
+
+
+
+
+
+
+