2020-12-09 01:27:36 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-12-09 01:27:36 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const url = require('url');
|
|
|
|
const path = require('path');
|
|
|
|
const {JSZip} = require('./util');
|
|
|
|
const {VM} = require('./vm');
|
|
|
|
require('fake-indexeddb/auto');
|
|
|
|
|
|
|
|
const chrome = {
|
|
|
|
runtime: {
|
|
|
|
getURL: (path2) => {
|
|
|
|
return url.pathToFileURL(path.join(__dirname, '..', 'ext', path2.replace(/^\//, ''))).href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
async function fetch(url2) {
|
2021-07-03 02:46:38 +00:00
|
|
|
const extDir = path.join(__dirname, '..', 'ext');
|
|
|
|
let filePath;
|
|
|
|
try {
|
|
|
|
filePath = url.fileURLToPath(url2);
|
|
|
|
} catch (e) {
|
|
|
|
filePath = path.resolve(extDir, url2.replace(/^[/\\]/, ''));
|
|
|
|
}
|
2020-12-09 01:27:36 +00:00
|
|
|
await Promise.resolve();
|
|
|
|
const content = fs.readFileSync(filePath, {encoding: null});
|
|
|
|
return {
|
|
|
|
ok: true,
|
|
|
|
status: 200,
|
|
|
|
statusText: 'OK',
|
|
|
|
text: async () => Promise.resolve(content.toString('utf8')),
|
|
|
|
json: async () => Promise.resolve(JSON.parse(content.toString('utf8')))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-14 22:41:15 +00:00
|
|
|
function atob(data) {
|
|
|
|
return Buffer.from(data, 'base64').toString('ascii');
|
|
|
|
}
|
|
|
|
|
2020-12-09 01:27:36 +00:00
|
|
|
class DatabaseVM extends VM {
|
2021-06-26 21:05:59 +00:00
|
|
|
constructor(globals={}) {
|
|
|
|
super(Object.assign({
|
2020-12-09 01:27:36 +00:00
|
|
|
chrome,
|
|
|
|
fetch,
|
|
|
|
indexedDB: global.indexedDB,
|
|
|
|
IDBKeyRange: global.IDBKeyRange,
|
2021-03-14 22:41:15 +00:00
|
|
|
JSZip,
|
|
|
|
atob
|
2021-06-26 21:05:59 +00:00
|
|
|
}, globals));
|
2020-12-09 01:27:36 +00:00
|
|
|
this.context.window = this.context;
|
|
|
|
this.indexedDB = global.indexedDB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 16:30:31 +00:00
|
|
|
class DatabaseVMDictionaryImporterMediaLoader {
|
2021-09-04 02:33:58 +00:00
|
|
|
async getImageDetails(content) {
|
2021-07-31 16:30:31 +00:00
|
|
|
// Placeholder values
|
2021-09-04 02:33:58 +00:00
|
|
|
return {content, width: 100, height: 100};
|
2021-07-31 16:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 01:27:36 +00:00
|
|
|
module.exports = {
|
2021-07-31 16:30:31 +00:00
|
|
|
DatabaseVM,
|
|
|
|
DatabaseVMDictionaryImporterMediaLoader
|
2020-12-09 01:27:36 +00:00
|
|
|
};
|