Merge pull request #817 from nonameShijian/PR-Branch

initReadWriteFunction添加是否可以进行文件读写的验证
This commit is contained in:
Spmario233 2024-01-19 23:59:43 +08:00 committed by GitHub
commit 2c5576d7f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 116 additions and 102 deletions

View File

@ -18,11 +18,19 @@
</script> </script>
<script> <script>
if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) { if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) {
window.initReadWriteFunction = function(game) { window.initReadWriteFunction = async function(game) {
/*game.download = function() { return new Promise((resolve, reject) => {
// 暂不实现 fetch(`./readFile?fileName=noname.js`)
};*/ .then(response => {
return response.json();
})
.then(result => {
if (result && result.success) callback();
else reject(result.errorMsg)
})
.catch(reject);
function callback() {
game.createDir = function (dir, success = () => { }, error = () => { }) { game.createDir = function (dir, success = () => { }, error = () => { }) {
fetch(`./createDir?dir=${dir}`) fetch(`./createDir?dir=${dir}`)
.then(response => { .then(response => {
@ -130,6 +138,10 @@
} }
game.createDir(pathArray.join("/"), callback, console.error); game.createDir(pathArray.join("/"), callback, console.error);
}; };
resolve();
}
});
}; };
} }
</script> </script>

View File

@ -68,8 +68,8 @@ declare interface Window {
get: Get; get: Get;
ai: AI; ai: AI;
} }
/** 为其他自定义平台提供文件读写函数赋值的一种方式 */
initReadWriteFunction?(game: Game): void; initReadWriteFunction?(game: Game): Promise<void>;
bannedKeyWords: string[]; bannedKeyWords: string[];
} }

View File

@ -106,7 +106,7 @@ export async function boot() {
else if (!Reflect.has(lib, 'device')) { else if (!Reflect.has(lib, 'device')) {
Reflect.set(lib, 'path', (await import('../library/path.js')).default); Reflect.set(lib, 'path', (await import('../library/path.js')).default);
//为其他自定义平台提供文件读写函数赋值的一种方式。 //为其他自定义平台提供文件读写函数赋值的一种方式。
//但这种方式只修改game的文件读写函数。 //但这种方式只允许修改game的文件读写函数。
if (typeof window.initReadWriteFunction == 'function') { if (typeof window.initReadWriteFunction == 'function') {
const g = {}; const g = {};
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir']; const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir'];
@ -123,7 +123,9 @@ export async function boot() {
}); });
}); });
// @ts-ignore // @ts-ignore
window.initReadWriteFunction(g); await window.initReadWriteFunction(g).catch(e => {
console.error('文件读写函数初始化失败:', e);
});
} }
window.onbeforeunload = function () { window.onbeforeunload = function () {
if (config.get('confirm_exit') && !_status.reloading) { if (config.get('confirm_exit') && !_status.reloading) {