From 50ce47eaac5de09924ad7e3611f017d03afa6857 Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Wed, 7 Feb 2024 23:43:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0http=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=90=8E=E5=BB=B6=E8=BF=9F=E9=87=8D=E5=90=AF?= =?UTF-8?q?=EF=BC=8C=E5=88=9D=E6=AC=A1=E5=8A=A0=E8=BD=BDserviceWorker?= =?UTF-8?q?=E9=87=8D=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/entry.js | 4 +++- game/game.js | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/game/entry.js b/game/entry.js index beeeb229c..85e7f50a8 100644 --- a/game/entry.js +++ b/game/entry.js @@ -62,7 +62,9 @@ boot().then(() => { const thisWindow = remote.getCurrentWindow(); thisWindow.loadURL(url); } else { - location.href = url; + setTimeout(() => { + location.href = url; + }, 1000); } } } diff --git a/game/game.js b/game/game.js index 5dc94eeb4..0ecc5ebf3 100644 --- a/game/game.js +++ b/game/game.js @@ -136,17 +136,27 @@ new Promise(resolve => { } if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) { let scope = window.location.protocol + '//' + window.location.host + '/'; - navigator.serviceWorker.register(`${scope}service-worker.js`, { - updateViaCache: "all", - scope, - }).then(registration => { - navigator.serviceWorker.addEventListener('message', e => { - console.log(e); + navigator.serviceWorker.getRegistrations().then(registrations => { + let findServiceWorker = false; + for (let registration of registrations) { + if (registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`) { + findServiceWorker = true; + } + } + navigator.serviceWorker.register(`${scope}service-worker.js`, { + updateViaCache: "all", + scope, + }).then(registration => { + // 初次加载worker,需要重新启动一次 + if (!findServiceWorker) location.reload(); + navigator.serviceWorker.addEventListener('message', e => { + console.log(e); + }); + registration.update(); + // console.log(`set scope: ${scope}, service worker instance:`, registration); + }).catch(e => { + console.log('serviceWorker加载失败: ', e); }); - registration.update(); - // console.log(`set scope: ${scope}, service worker instance:`, registration); - }).catch(e => { - console.log('serviceWorker加载失败: ', e); }); } const script = document.createElement('script') From 355f0cc5fdd08a189b658b0a3256a0c756f1cc0e Mon Sep 17 00:00:00 2001 From: nonameShijian <2954700422@qq.com> Date: Thu, 8 Feb 2024 00:49:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8xhr=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=97=B6=E5=8A=A0=E5=85=A5=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=A0=81=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- noname/library/init/index.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/noname/library/init/index.js b/noname/library/init/index.js index 7bdb28491..28ffeb500 100644 --- a/noname/library/init/index.js +++ b/noname/library/init/index.js @@ -263,6 +263,11 @@ export class LibInit extends Uninstantable { const xmlHttpRequest = new XMLHttpRequest(); let data; xmlHttpRequest.addEventListener("load", () => { + if (![0, 200].includes(xmlHttpRequest.status)) { + // @ts-ignore + if (typeof onError == 'function') onError(new Error(oReq.statusText || oReq.status)); + return; + } data = xmlHttpRequest.responseText; if (!data) { if (typeof onError == 'function') onError(new Error(`${scriptSource}加载失败!`)); @@ -303,7 +308,14 @@ export class LibInit extends Uninstantable { sScriptURL = url + str; } const oReq = new XMLHttpRequest(); - if (typeof onload == 'function') oReq.addEventListener("load", onload); + if (typeof onload == 'function') oReq.addEventListener("load", result => { + if (![0, 200].includes(oReq.status)) { + // @ts-ignore + if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status)); + return; + } + onload(result); + }); if (typeof onerror == 'function') oReq.addEventListener("error", onerror); oReq.open("GET", sScriptURL); oReq.send(); @@ -330,7 +342,14 @@ export class LibInit extends Uninstantable { sScriptURL = url + str; } const oReq = new XMLHttpRequest(); - if (typeof onload == 'function') oReq.addEventListener("load", onload); + if (typeof onload == 'function') oReq.addEventListener("load", result => { + if (![0, 200].includes(oReq.status)) { + // @ts-ignore + if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status)); + return; + } + onload(result); + }); if (typeof onerror == 'function') oReq.addEventListener("error", onerror); oReq.open("GET", sScriptURL, false); oReq.send(); @@ -340,6 +359,11 @@ export class LibInit extends Uninstantable { static json(url, onload, onerror) { const oReq = new XMLHttpRequest(); if (typeof onload == 'function') oReq.addEventListener("load", () => { + if (![0, 200].includes(oReq.status)) { + // @ts-ignore + if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status)); + return; + } let result; try { result = JSON.parse(oReq.responseText); @@ -368,6 +392,11 @@ export class LibInit extends Uninstantable { } const oReq = new XMLHttpRequest(); if (typeof onload == 'function') oReq.addEventListener("load", () => { + if (![0, 200].includes(oReq.status)) { + // @ts-ignore + if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status)); + return; + } let result; try { result = JSON.parse(oReq.responseText);