121 lines
4.1 KiB
JavaScript
121 lines
4.1 KiB
JavaScript
import { launch } from 'puppeteer';
|
||
import { readFileSync } from 'fs';
|
||
|
||
(async () => {
|
||
// 读取 JSON 文件
|
||
const jsonData = JSON.parse(readFileSync('allData.json', 'utf8'));
|
||
|
||
const browser = await launch({ headless: false });
|
||
const page = await browser.newPage();
|
||
|
||
// 打开目标网页
|
||
await page.goto('https://hlk.meiguanjia.net'); // 替换为你的目标 URL
|
||
|
||
// console.log(JSON.stringify(jsonData, null, 2));
|
||
|
||
page.on('console', (msg) => {
|
||
for (let i = 0; i < msg.args().length; ++i) {
|
||
console.log(`${i}: ${msg.args()[i]}`);
|
||
}
|
||
});
|
||
|
||
// 在页面上下文中插入数据
|
||
await page.evaluate(async (jsonData) => {
|
||
console.log('传递到浏览器端的 JSON 数据:', jsonData); // 打印传递的数据,调试用
|
||
|
||
// 遍历 jsonData 数组
|
||
for (const dbData of jsonData) {
|
||
const dbName = dbData.name; // 获取数据库名称
|
||
const stores = dbData.stores; // 获取数据库中的对象存储
|
||
|
||
console.log(`正在创建数据库:${dbName}`); // 确认 dbName 是否正确
|
||
|
||
if (!dbName) {
|
||
console.error('数据库名称不存在!');
|
||
return;
|
||
}
|
||
|
||
// 打开数据库(如果数据库不存在,会创建它)
|
||
const request = indexedDB.open(dbName, 1);
|
||
|
||
// 数据库创建或升级时触发
|
||
request.onupgradeneeded = function (event) {
|
||
const db = event.target.result;
|
||
|
||
console.log(`数据库 ${dbName} 创建或升级中...`);
|
||
|
||
// 动态创建对象存储
|
||
for (const storeName in stores) {
|
||
if (!db.objectStoreNames.contains(storeName)) {
|
||
console.log(`创建对象存储:${storeName}`);
|
||
db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true });
|
||
}
|
||
}
|
||
};
|
||
|
||
// 数据库成功打开时触发
|
||
request.onsuccess = function (event) {
|
||
const db = event.target.result;
|
||
console.log(`数据库 ${dbName} 打开成功`);
|
||
|
||
// 开始一个事务
|
||
const transaction = db.transaction(Object.keys(stores), 'readwrite');
|
||
|
||
// 对每个对象存储插入数据
|
||
for (const storeName in stores) {
|
||
const store = transaction.objectStore(storeName);
|
||
const data = stores[storeName];
|
||
|
||
console.log(`插入数据到 ${storeName} 对象存储`);
|
||
|
||
// 递归处理多层嵌套的对象
|
||
insertNestedData(store, data);
|
||
}
|
||
|
||
// 事务完成时触发
|
||
transaction.oncomplete = function () {
|
||
console.log(`${dbName} 数据写入完成`);
|
||
};
|
||
|
||
// 事务错误时触发
|
||
transaction.onerror = function (event) {
|
||
console.error(`${dbName} 数据写入失败:`, event.target.error);
|
||
};
|
||
};
|
||
|
||
// 打开数据库失败时触发
|
||
request.onerror = function (event) {
|
||
console.error(`打开数据库 ${dbName} 失败:`, event.target.error);
|
||
};
|
||
}
|
||
|
||
// 递归插入数据的函数,处理多层嵌套
|
||
function insertNestedData(store, data) {
|
||
data.forEach((item) => {
|
||
if (typeof item === 'object' && !Array.isArray(item)) {
|
||
// 如果是对象且不是数组,递归处理
|
||
for (const key in item) {
|
||
// 生成一个包含 id 的新对象
|
||
const nestedData = item[key];
|
||
const id = key || Date.now(); // 使用键作为 ID,或者时间戳作为备用
|
||
store.put({ id, ...nestedData }); // 插入数据
|
||
console.log(`插入数据:${id}`, nestedData);
|
||
// 如果有更深层的嵌套,递归调用
|
||
if (typeof nestedData === 'object' && !Array.isArray(nestedData)) {
|
||
insertNestedData(store, [nestedData]); // 深层嵌套递归
|
||
}
|
||
}
|
||
} else {
|
||
// 如果是普通数据(如数组项)
|
||
store.put({ id: item });
|
||
console.log(`插入数据:`, item);
|
||
}
|
||
});
|
||
}
|
||
}, jsonData); // 将 jsonData 传递到浏览器上下文中
|
||
|
||
// 等待一段时间,确保数据写入完成
|
||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||
// await browser.close();
|
||
})();
|