为未来的http协议提前做兼容

This commit is contained in:
nonameShijian 2024-01-24 17:46:05 +08:00
parent 5c43b7fd0e
commit feeea1fe77
3 changed files with 11 additions and 10 deletions

View File

@ -5,7 +5,7 @@ new Promise(resolve => {
if ('__core-js_shared__' in window) resolve(null); if ('__core-js_shared__' in window) resolve(null);
else { else {
const nonameInitialized = localStorage.getItem('noname_inited'); const nonameInitialized = localStorage.getItem('noname_inited');
const assetURL = typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized; const assetURL = location.protocol.startsWith('http') ? '' : (typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized);
const coreJSBundle = document.createElement('script'); const coreJSBundle = document.createElement('script');
coreJSBundle.onerror = coreJSBundle.onload = resolve; coreJSBundle.onerror = coreJSBundle.onload = resolve;
coreJSBundle.src = `${assetURL}game/core-js-bundle.js`; coreJSBundle.src = `${assetURL}game/core-js-bundle.js`;
@ -13,7 +13,7 @@ new Promise(resolve => {
} }
}).then(() => { }).then(() => {
const nonameInitialized = localStorage.getItem('noname_inited'); const nonameInitialized = localStorage.getItem('noname_inited');
const assetURL = typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized; const assetURL = location.protocol.startsWith('http') ? '' : (typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized);
const userAgent = navigator.userAgent.toLowerCase(); const userAgent = navigator.userAgent.toLowerCase();
const exit = () => { const exit = () => {

View File

@ -4,6 +4,7 @@ import { Library as lib } from '../library/index.js';
import { Game as game } from '../game/index.js'; import { Game as game } from '../game/index.js';
import { status as _status } from '../status/index.js'; import { status as _status } from '../status/index.js';
import { UI as ui } from '../ui/index.js'; import { UI as ui } from '../ui/index.js';
import { nonameInitialized } from '../util/index.js';
export async function cordovaReady() { export async function cordovaReady() {
lib.path = (await import('../library/path.js')).default; lib.path = (await import('../library/path.js')).default;
@ -62,7 +63,7 @@ export async function cordovaReady() {
url = get.url(dev) + url; url = get.url(dev) + url;
} }
var fileTransfer = new FileTransfer(); var fileTransfer = new FileTransfer();
folder = lib.assetURL + folder; folder = nonameInitialized + folder;
if (onprogress) { if (onprogress) {
fileTransfer.onprogress = function (progressEvent) { fileTransfer.onprogress = function (progressEvent) {
onprogress(progressEvent.loaded, progressEvent.total); onprogress(progressEvent.loaded, progressEvent.total);
@ -79,7 +80,7 @@ export async function cordovaReady() {
}, onerror); }, onerror);
}; };
game.readFile = function (filename, callback, onerror) { game.readFile = function (filename, callback, onerror) {
window.resolveLocalFileSystemURL(lib.assetURL, function (entry) { window.resolveLocalFileSystemURL(nonameInitialized, function (entry) {
entry.getFile(filename, {}, function (fileEntry) { entry.getFile(filename, {}, function (fileEntry) {
fileEntry.file(function (fileToLoad) { fileEntry.file(function (fileToLoad) {
var fileReader = new FileReader(); var fileReader = new FileReader();
@ -92,7 +93,7 @@ export async function cordovaReady() {
}, onerror); }, onerror);
}; };
game.readFileAsText = function (filename, callback, onerror) { game.readFileAsText = function (filename, callback, onerror) {
window.resolveLocalFileSystemURL(lib.assetURL, function (entry) { window.resolveLocalFileSystemURL(nonameInitialized, function (entry) {
entry.getFile(filename, {}, function (fileEntry) { entry.getFile(filename, {}, function (fileEntry) {
fileEntry.file(function (fileToLoad) { fileEntry.file(function (fileToLoad) {
var fileReader = new FileReader(); var fileReader = new FileReader();
@ -114,7 +115,7 @@ export async function cordovaReady() {
fileReader.readAsArrayBuffer(data, "UTF-8"); fileReader.readAsArrayBuffer(data, "UTF-8");
} }
else { else {
window.resolveLocalFileSystemURL(lib.assetURL + path, function (entry) { window.resolveLocalFileSystemURL(nonameInitialized + path, function (entry) {
entry.getFile(name, { create: true }, function (fileEntry) { entry.getFile(name, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) { fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = callback; fileWriter.onwriteend = callback;
@ -126,7 +127,7 @@ export async function cordovaReady() {
}); });
}; };
game.removeFile = function (dir, callback) { game.removeFile = function (dir, callback) {
window.resolveLocalFileSystemURL(lib.assetURL, function (entry) { window.resolveLocalFileSystemURL(nonameInitialized, function (entry) {
entry.getFile(dir, {}, function (fileEntry) { entry.getFile(dir, {}, function (fileEntry) {
fileEntry.remove(); fileEntry.remove();
if (callback) callback(); if (callback) callback();
@ -135,7 +136,7 @@ export async function cordovaReady() {
}; };
game.getFileList = (dir, success, failure) => { game.getFileList = (dir, success, failure) => {
var files = [], folders = []; var files = [], folders = [];
window.resolveLocalFileSystemURL(lib.assetURL + dir, entry => { window.resolveLocalFileSystemURL(nonameInitialized + dir, entry => {
var dirReader = entry.createReader(); var dirReader = entry.createReader();
var entries = []; var entries = [];
var readEntries = () => { var readEntries = () => {
@ -174,7 +175,7 @@ export async function cordovaReady() {
create: true create: true
}, resolve))).then(directoryEntry => access(directoryEntry, directory, createDirectory)); }, resolve))).then(directoryEntry => access(directoryEntry, directory, createDirectory));
}; };
return new Promise((resolve, reject) => window.resolveLocalFileSystemURL(lib.assetURL, rootEntry => { return new Promise((resolve, reject) => window.resolveLocalFileSystemURL(nonameInitialized, rootEntry => {
const createDirectory = () => { const createDirectory = () => {
if (directoryList.length) access(rootEntry, directoryList.pop().split('/').reverse(), createDirectory); if (directoryList.length) access(rootEntry, directoryList.pop().split('/').reverse(), createDirectory);
if (typeof callback == 'function') callback(); if (typeof callback == 'function') callback();

View File

@ -1,5 +1,5 @@
export const nonameInitialized = localStorage.getItem('noname_inited'); export const nonameInitialized = localStorage.getItem('noname_inited');
export const assetURL = typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized; export const assetURL = location.protocol.startsWith('http') ? '' : (typeof nonameInitialized != 'string' || nonameInitialized == 'nodejs' ? '' : nonameInitialized);
export const GeneratorFunction = (function* () { }).constructor; export const GeneratorFunction = (function* () { }).constructor;
export const AsyncFunction = (async function () { }).constructor; export const AsyncFunction = (async function () { }).constructor;
export const userAgent = navigator.userAgent.toLowerCase(); export const userAgent = navigator.userAgent.toLowerCase();