initReadWriteFunction添加是否可以进行文件读写的验证
This commit is contained in:
parent
1408fbb88d
commit
5dbf9ef2fb
32
index.html
32
index.html
|
@ -18,12 +18,20 @@
|
|||
</script>
|
||||
<script>
|
||||
if (location.href.startsWith('http') && typeof window.initReadWriteFunction != 'function' && !window.require && !window.__dirname) {
|
||||
window.initReadWriteFunction = function(game) {
|
||||
/*game.download = function() {
|
||||
// 暂不实现
|
||||
};*/
|
||||
window.initReadWriteFunction = async function(game) {
|
||||
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}`)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
|
@ -35,7 +43,7 @@
|
|||
.catch(error);
|
||||
};
|
||||
|
||||
game.readFile = function (fileName, callback = () => {}, error = () => {}) {
|
||||
game.readFile = function (fileName, callback = () => { }, error = () => { }) {
|
||||
fetch(`./readFile?fileName=${fileName}`)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
|
@ -47,7 +55,7 @@
|
|||
.catch(error);
|
||||
};
|
||||
|
||||
game.readFileAsText = function (fileName, callback = () => {}, error = () => {}) {
|
||||
game.readFileAsText = function (fileName, callback = () => { }, error = () => { }) {
|
||||
fetch(`./readFileAsText?fileName=${fileName}`)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
|
@ -100,7 +108,7 @@
|
|||
});
|
||||
};
|
||||
|
||||
game.removeFile = function (fileName, callback = () => {}) {
|
||||
game.removeFile = function (fileName, callback = () => { }) {
|
||||
fetch(`./removeFile?fileName=${fileName}`)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
|
@ -111,7 +119,7 @@
|
|||
.catch(error);
|
||||
};
|
||||
|
||||
game.getFileList = function (dir, callback = () => {}) {
|
||||
game.getFileList = function (dir, callback = () => { }) {
|
||||
fetch(`./getFileList?dir=${dir}`)
|
||||
.then(response => {
|
||||
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;
|
||||
if (file) {
|
||||
pathArray = pathArray.slice(0, -1);
|
||||
}
|
||||
game.createDir(pathArray.join("/"), callback, console.error);
|
||||
};
|
||||
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
|
|
@ -68,8 +68,8 @@ declare interface Window {
|
|||
get: Get;
|
||||
ai: AI;
|
||||
}
|
||||
|
||||
initReadWriteFunction?(game: Game): void;
|
||||
/** 为其他自定义平台提供文件读写函数赋值的一种方式 */
|
||||
initReadWriteFunction?(game: Game): Promise<void>;
|
||||
|
||||
bannedKeyWords: string[];
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ export async function boot() {
|
|||
else if (!Reflect.has(lib, 'device')) {
|
||||
Reflect.set(lib, 'path', (await import('../library/path.js')).default);
|
||||
//为其他自定义平台提供文件读写函数赋值的一种方式。
|
||||
//但这种方式只能修改game的文件读写函数。
|
||||
//但这种方式只允许修改game的文件读写函数。
|
||||
if (typeof window.initReadWriteFunction == 'function') {
|
||||
const g = {};
|
||||
const ReadWriteFunctionName = ['download', 'readFile', 'readFileAsText', 'writeFile', 'removeFile', 'getFileList', 'ensureDirectory', 'createDir'];
|
||||
|
@ -123,7 +123,9 @@ export async function boot() {
|
|||
});
|
||||
});
|
||||
// @ts-ignore
|
||||
window.initReadWriteFunction(g);
|
||||
await window.initReadWriteFunction(g).catch(e => {
|
||||
console.error('文件读写函数初始化失败:', e);
|
||||
});
|
||||
}
|
||||
window.onbeforeunload = function () {
|
||||
if (config.get('confirm_exit') && !_status.reloading) {
|
||||
|
|
Loading…
Reference in New Issue