Merge pull request #959 from nonameShijian/PR-Branch

worker加载完成后再加载entry.js, 扩展的enable为void 0的时候,改为true
This commit is contained in:
Spmario233 2024-02-16 20:09:04 +08:00 committed by GitHub
commit 33c7439149
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 64 additions and 48 deletions

View File

@ -77,7 +77,7 @@ new Promise(resolve => {
if (!(result = userAgent.match(regex))) return ["other", NaN, NaN, NaN]
if (result[1] != "safari") {
const [major, minor, patch] = result[2].split(".")
// @ts-ignore
// @ts-ignore
return [result[1], parseInt(major), parseInt(minor), parseInt(patch)]
}
result = userAgent.match(/version\/(\d+(?:\.\d+)+).*safari/)
@ -158,39 +158,46 @@ new Promise(resolve => {
module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
};
}
// 使serviceWorker加载完成后再加载entry.js
const loadEntryJs = () => {
const script = document.createElement('script')
script.type = "module";
script.src = `${assetURL}game/entry.js`;
script.async = true;
script.onerror = event => {
console.error(event);
const message = `您使用的浏览器或《无名杀》客户端加载内容失败!\n请检查是否缺少游戏文件!隔版本更新请下载完整包而不是离线包!\n目前使用的浏览器UA信息为\n${userAgent}\n若您使用的客户端为自带内核的旧版“兼容版”,请及时更新客户端版本!\n若您使用的客户端为手机端的非兼容版《无名杀》请尝试更新手机的WebView内核或者更换为1.8.2版本及以上的兼容版!\n若您是直接使用浏览器加载index.html进行游戏请改为运行文件夹内的“noname-server.exe”或使用VSCode等工具启动Live Server以动态服务器的方式启动《无名杀》\n若您使用的是苹果端请至少将Safari升级至14.5.0`;
console.error(message);
alert(message);
exit();
}
document.head.appendChild(script);
};
if (location.protocol.startsWith('http') && 'serviceWorker' in navigator) {
let scope = window.location.protocol + '//' + window.location.host + '/';
navigator.serviceWorker.getRegistrations().then(registrations => {
let findServiceWorker = registrations.find(registration => {
return registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`;
})
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);
navigator.serviceWorker.getRegistrations()
.then(async registrations => {
let findServiceWorker = registrations.find(registration => {
return registration && registration.active && registration.active.scriptURL == `${scope}service-worker.js`;
});
registration.update();
// console.log(`set scope: ${scope}, service worker instance:`, registration);
}).catch(e => {
console.log('serviceWorker加载失败: ', e);
});
});
try {
const registration_1 = await navigator.serviceWorker.register(`${scope}service-worker.js`, {
updateViaCache: "all",
scope,
});
// 初次加载worker需要重新启动一次
if (!findServiceWorker) location.reload();
navigator.serviceWorker.addEventListener('message', e => {
console.log(e);
});
registration_1.update();
} catch (e_1) {
console.log('serviceWorker加载失败: ', e_1);
}
}).finally(loadEntryJs);
} else {
loadEntryJs();
}
const script = document.createElement('script')
script.type = "module";
script.src = `${assetURL}game/entry.js`;
script.async = true;
script.onerror = event => {
console.error(event);
const message = `您使用的浏览器或《无名杀》客户端加载内容失败!\n请检查是否缺少游戏文件!隔版本更新请下载完整包而不是离线包!\n目前使用的浏览器UA信息为\n${userAgent}\n若您使用的客户端为自带内核的旧版“兼容版”,请及时更新客户端版本!\n若您使用的客户端为手机端的非兼容版《无名杀》请尝试更新手机的WebView内核或者更换为1.8.2版本及以上的兼容版!\n若您是直接使用浏览器加载index.html进行游戏请改为运行文件夹内的“noname-server.exe”或使用VSCode等工具启动Live Server以动态服务器的方式启动《无名杀》\n若您使用的是苹果端请至少将Safari升级至14.5.0`;
console.error(message);
alert(message);
exit();
}
document.head.appendChild(script);
}
});

View File

@ -4640,6 +4640,9 @@ export class Game extends Uninstantable {
* @param { string } extensionName
*/
static hasExtension(extensionName) {
if (typeof lib.config[`extension_${extensionName}_enable`] != 'boolean') {
game.saveExtensionConfig(extensionName, 'enable', true);
}
return this.hasExtensionInstalled(extensionName) && lib.config[`extension_${extensionName}_enable`];
}
/**

View File

@ -1157,20 +1157,20 @@ export const otherMenu = function (connectMenu) {
* @type { (value:string)=>any }
*/
const fun = (new Function('window', `
const _status=window._status;
const lib=window.lib;
const game=window.game;
const ui=window.ui;
const get=window.get;
const ai=window.ai;
const cheat=window.lib.cheat;
//使用正则匹配绝大多数的普通obj对象避免解析成代码块。
const reg=${/^\{([^{}]+:\s*([^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\)))(?:,\s*([^{}]+:\s*(?:[^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\))))*\}$/};
return function(value){
"use strict";
return eval(reg.test(value)?('('+value+')'):value);
}
`))(proxyWindow);
const _status=window._status;
const lib=window.lib;
const game=window.game;
const ui=window.ui;
const get=window.get;
const ai=window.ai;
const cheat=window.lib.cheat;
//使用正则匹配绝大多数的普通obj对象避免解析成代码块。
const reg=${/^\{([^{}]+:\s*([^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\)))(?:,\s*([^{}]+:\s*(?:[^\s,]*|'[^']*'|"[^"]*"|\{[^}]*\}|\[[^\]]*\]|null|undefined|([a-zA-Z$_][a-zA-Z0-9$_]*\s*:\s*)?[a-zA-Z$_][a-zA-Z0-9$_]*\(\))))*\}$/};
return function(value){
"use strict";
return eval(reg.test(value)?('('+value+')'):value);
}
`))(proxyWindow);
const runCommand = () => {
if (text2.value && !['up', 'down'].includes(text2.value)) {
logindex = -1;

View File

@ -5,12 +5,12 @@ var ts;
importScripts('./game/typescript.js');
// @ts-ignore
if (typeof ts != 'undefined') {
console.log(ts);
console.log(`ts loaded`);
} else {
console.log(`ts undefined`);
}
console.log('version 2.1');
console.log('serviceWorker version 2.2');
self.addEventListener("install", (event) => {
// The promise that skipWaiting() returns can be safely ignored.
@ -18,6 +18,12 @@ self.addEventListener("install", (event) => {
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
// 当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 claim() 方法会立即控制这些页面
// @ts-ignore
event.waitUntil(clients.claim());
});
self.addEventListener('message', event => {
console.log(event.data);
});