151 lines
5.0 KiB
JavaScript
151 lines
5.0 KiB
JavaScript
import puppeteer from 'puppeteer';
|
|
import { read, readFileSync } from 'fs';
|
|
|
|
(async () => {
|
|
// 读取 JSON 文件
|
|
const jsonData = JSON.parse(readFileSync('indexedDB.json', 'utf8'));
|
|
|
|
const browser = await puppeteer.launch({ headless: false });
|
|
|
|
// 获取当前的页面,通常是浏览器启动后的默认页面
|
|
const [page] = await browser.pages();
|
|
|
|
// 打开目标网页
|
|
await page.goto('https://hlk.meiguanjia.net'); // 替换为你的目标 URL
|
|
|
|
const localStorageData = JSON.parse(readFileSync('localStorage.json', 'utf8'));
|
|
await page.evaluate((data) => {
|
|
for (const key in data) {
|
|
localStorage.setItem(key, data[key]);
|
|
}
|
|
}, localStorageData);
|
|
console.log('set localStorage success');
|
|
|
|
// const cookie = JSON.parse(readFileSync('cookie.json', 'utf8'));
|
|
// await page.setCookie(...cookie);
|
|
// console.log('cookie', cookie);
|
|
|
|
// console.log(JSON.stringify(jsonData, null, 2));
|
|
|
|
page.on('console', (msg) => {
|
|
for (let i = 0; i < msg.args().length; ++i) {
|
|
console.log(`${msg.args()[i]}`);
|
|
}
|
|
});
|
|
|
|
// await page.evaluate((jsonData) => {
|
|
// // 打开 IndexedDB 数据库
|
|
// const openRequest = indexedDB.open('hlk_touch_test_14920', 2);
|
|
|
|
// // 处理数据库版本升级
|
|
// openRequest.onupgradeneeded = (event) => {
|
|
// const db = event.target.result;
|
|
// // 创建对象存储,如果不存在的话
|
|
// if (!db.objectStoreNames.contains('keyvaluepairs')) {
|
|
// db.createObjectStore('keyvaluepairs');
|
|
// }
|
|
// if (!db.objectStoreNames.contains('local-forage-detect-blob-support')) {
|
|
// db.createObjectStore('local-forage-detect-blob-support');
|
|
// }
|
|
// };
|
|
|
|
// openRequest.onsuccess = (event) => {
|
|
// const db = event.target.result;
|
|
// const transaction = db.transaction(['keyvaluepairs'], 'readwrite');
|
|
// const objectStore = transaction.objectStore('keyvaluepairs'); // 使用硬编码的存储名
|
|
// console.log(jsonData[0].value);
|
|
|
|
// console.log('jsonData.length', jsonData.length);
|
|
|
|
// for (let i = 0; i < jsonData.length; i++) {
|
|
// const addObject = objectStore.put(jsonData[i].value, jsonData[i].key);
|
|
// addObject.onsuccess = (event) => {
|
|
// console.log('数据添加成功');
|
|
// };
|
|
// addObject.onerror = (event) => {
|
|
// console.error('数据添加失败:', event.target.error);
|
|
// };
|
|
// }
|
|
|
|
// // 事务错误处理
|
|
// transaction.onerror = (event) => {
|
|
// console.error('事务失败:', event.target.error);
|
|
// };
|
|
// };
|
|
|
|
// // 错误处理
|
|
// openRequest.onerror = (event) => {
|
|
// console.error('数据库打开失败:', event.target.error);
|
|
// };
|
|
// }, jsonData); // 将 jsonData 传递到浏览器上下文中
|
|
|
|
await page.evaluate((jsonData) => {
|
|
return new Promise((resolve, reject) => {
|
|
// 打开 IndexedDB 数据库
|
|
const openRequest = indexedDB.open('hlk_touch_test_14920', 2);
|
|
|
|
// 处理数据库版本升级
|
|
openRequest.onupgradeneeded = (event) => {
|
|
const db = event.target.result;
|
|
// 创建对象存储,如果不存在的话
|
|
if (!db.objectStoreNames.contains('keyvaluepairs')) {
|
|
db.createObjectStore('keyvaluepairs');
|
|
}
|
|
if (!db.objectStoreNames.contains('local-forage-detect-blob-support')) {
|
|
db.createObjectStore('local-forage-detect-blob-support');
|
|
}
|
|
};
|
|
|
|
openRequest.onsuccess = (event) => {
|
|
const db = event.target.result;
|
|
const transaction = db.transaction(['keyvaluepairs'], 'readwrite');
|
|
const objectStore = transaction.objectStore('keyvaluepairs'); // 使用硬编码的存储名
|
|
console.log(jsonData[0].value);
|
|
|
|
console.log('jsonData.length', jsonData.length);
|
|
|
|
let completedOperations = 0;
|
|
const totalOperations = jsonData.length;
|
|
|
|
for (let i = 0; i < jsonData.length; i++) {
|
|
const addObject = objectStore.put(jsonData[i].value, jsonData[i].key);
|
|
|
|
addObject.onsuccess = () => {
|
|
console.log('数据添加成功', jsonData[i].key);
|
|
completedOperations++;
|
|
// 如果所有操作完成,调用 resolve
|
|
if (completedOperations === totalOperations) {
|
|
resolve('所有数据写入完成');
|
|
}
|
|
};
|
|
|
|
addObject.onerror = (event) => {
|
|
console.error('数据添加失败:', event.target.error);
|
|
reject('数据添加失败');
|
|
};
|
|
}
|
|
|
|
// 事务错误处理
|
|
transaction.onerror = (event) => {
|
|
console.error('事务失败:', event.target.error);
|
|
reject('事务失败');
|
|
};
|
|
};
|
|
|
|
// 错误处理
|
|
openRequest.onerror = (event) => {
|
|
console.error('数据库打开失败:', event.target.error);
|
|
reject('数据库打开失败');
|
|
};
|
|
});
|
|
}, jsonData); // 将 jsonData 传递到浏览器上下文中
|
|
|
|
console.log('end');
|
|
|
|
await page.goto('https://hlk.meiguanjia.net');
|
|
|
|
// await
|
|
|
|
// await browser.close();
|
|
})();
|