initReadWriteFunction添加是否可以进行文件读写的验证

This commit is contained in:
nonameShijian 2024-01-19 23:57:05 +08:00
parent 1408fbb88d
commit 5dbf9ef2fb
3 changed files with 116 additions and 102 deletions

View File

@ -18,12 +18,20 @@
</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);
game.createDir = function (dir, success = () => {}, error = () => {}) { function callback() {
game.createDir = function (dir, success = () => { }, error = () => { }) {
fetch(`./createDir?dir=${dir}`) fetch(`./createDir?dir=${dir}`)
.then(response => { .then(response => {
return response.json(); return response.json();
@ -35,7 +43,7 @@
.catch(error); .catch(error);
}; };
game.readFile = function (fileName, callback = () => {}, error = () => {}) { game.readFile = function (fileName, callback = () => { }, error = () => { }) {
fetch(`./readFile?fileName=${fileName}`) fetch(`./readFile?fileName=${fileName}`)
.then(response => { .then(response => {
return response.json(); return response.json();
@ -47,7 +55,7 @@
.catch(error); .catch(error);
}; };
game.readFileAsText = function (fileName, callback = () => {}, error = () => {}) { game.readFileAsText = function (fileName, callback = () => { }, error = () => { }) {
fetch(`./readFileAsText?fileName=${fileName}`) fetch(`./readFileAsText?fileName=${fileName}`)
.then(response => { .then(response => {
return response.json(); return response.json();
@ -100,7 +108,7 @@
}); });
}; };
game.removeFile = function (fileName, callback = () => {}) { game.removeFile = function (fileName, callback = () => { }) {
fetch(`./removeFile?fileName=${fileName}`) fetch(`./removeFile?fileName=${fileName}`)
.then(response => { .then(response => {
return response.json(); return response.json();
@ -111,7 +119,7 @@
.catch(error); .catch(error);
}; };
game.getFileList = function (dir, callback = () => {}) { game.getFileList = function (dir, callback = () => { }) {
fetch(`./getFileList?dir=${dir}`) fetch(`./getFileList?dir=${dir}`)
.then(response => { .then(response => {
return response.json(); return response.json();
@ -123,13 +131,17 @@
}); });
}; };
game.ensureDirectory = function (list, callback = () => {}, file = false) { game.ensureDirectory = function (list, callback = () => { }, file = false) {
let pathArray = typeof list == "string" ? list.split("/") : list; let pathArray = typeof list == "string" ? list.split("/") : list;
if (file) { if (file) {
pathArray = pathArray.slice(0, -1); pathArray = pathArray.slice(0, -1);
} }
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) {