fix login data exception
This commit is contained in:
parent
e521f3b028
commit
b9c7b52a4d
1
cookie.json
Normal file
1
cookie.json
Normal file
@ -0,0 +1 @@
|
||||
[{"name":"JSESSIONID","value":"9E55F1720F56A71F1C1DDE22ECE125EA","domain":"hlk.meiguanjia.net","path":"/","expires":-1,"size":42,"httpOnly":true,"secure":false,"session":true,"priority":"Medium","sameParty":false,"sourceScheme":"Secure"}]
|
||||
3384
indexedDB.json
Normal file
3384
indexedDB.json
Normal file
File diff suppressed because it is too large
Load Diff
1
localStorage.json
Normal file
1
localStorage.json
Normal file
File diff suppressed because one or more lines are too long
188
sethlk.spec.js
188
sethlk.spec.js
@ -1,120 +1,150 @@
|
||||
import { launch } from 'puppeteer';
|
||||
import { readFileSync } from 'fs';
|
||||
import puppeteer from 'puppeteer';
|
||||
import { read, readFileSync } from 'fs';
|
||||
|
||||
(async () => {
|
||||
// 读取 JSON 文件
|
||||
const jsonData = JSON.parse(readFileSync('allData.json', 'utf8'));
|
||||
const jsonData = JSON.parse(readFileSync('indexedDB.json', 'utf8'));
|
||||
|
||||
const browser = await launch({ headless: false });
|
||||
const page = await browser.newPage();
|
||||
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(`${i}: ${msg.args()[i]}`);
|
||||
console.log(`${msg.args()[i]}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 在页面上下文中插入数据
|
||||
await page.evaluate(async (jsonData) => {
|
||||
console.log('传递到浏览器端的 JSON 数据:', jsonData); // 打印传递的数据,调试用
|
||||
// await page.evaluate((jsonData) => {
|
||||
// // 打开 IndexedDB 数据库
|
||||
// const openRequest = indexedDB.open('hlk_touch_test_14920', 2);
|
||||
|
||||
// 遍历 jsonData 数组
|
||||
for (const dbData of jsonData) {
|
||||
const dbName = dbData.name; // 获取数据库名称
|
||||
const stores = dbData.stores; // 获取数据库中的对象存储
|
||||
// // 处理数据库版本升级
|
||||
// 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');
|
||||
// }
|
||||
// };
|
||||
|
||||
console.log(`正在创建数据库:${dbName}`); // 确认 dbName 是否正确
|
||||
// openRequest.onsuccess = (event) => {
|
||||
// const db = event.target.result;
|
||||
// const transaction = db.transaction(['keyvaluepairs'], 'readwrite');
|
||||
// const objectStore = transaction.objectStore('keyvaluepairs'); // 使用硬编码的存储名
|
||||
// console.log(jsonData[0].value);
|
||||
|
||||
if (!dbName) {
|
||||
console.error('数据库名称不存在!');
|
||||
return;
|
||||
}
|
||||
// console.log('jsonData.length', jsonData.length);
|
||||
|
||||
// 打开数据库(如果数据库不存在,会创建它)
|
||||
const request = indexedDB.open(dbName, 1);
|
||||
// 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);
|
||||
// };
|
||||
// }
|
||||
|
||||
// 数据库创建或升级时触发
|
||||
request.onupgradeneeded = function (event) {
|
||||
// // 事务错误处理
|
||||
// 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;
|
||||
|
||||
console.log(`数据库 ${dbName} 创建或升级中...`);
|
||||
|
||||
// 动态创建对象存储
|
||||
for (const storeName in stores) {
|
||||
if (!db.objectStoreNames.contains(storeName)) {
|
||||
console.log(`创建对象存储:${storeName}`);
|
||||
db.createObjectStore(storeName, { keyPath: 'id', autoIncrement: true });
|
||||
// 创建对象存储,如果不存在的话
|
||||
if (!db.objectStoreNames.contains('keyvaluepairs')) {
|
||||
db.createObjectStore('keyvaluepairs');
|
||||
}
|
||||
if (!db.objectStoreNames.contains('local-forage-detect-blob-support')) {
|
||||
db.createObjectStore('local-forage-detect-blob-support');
|
||||
}
|
||||
};
|
||||
|
||||
// 数据库成功打开时触发
|
||||
request.onsuccess = function (event) {
|
||||
openRequest.onsuccess = (event) => {
|
||||
const db = event.target.result;
|
||||
console.log(`数据库 ${dbName} 打开成功`);
|
||||
const transaction = db.transaction(['keyvaluepairs'], 'readwrite');
|
||||
const objectStore = transaction.objectStore('keyvaluepairs'); // 使用硬编码的存储名
|
||||
console.log(jsonData[0].value);
|
||||
|
||||
// 开始一个事务
|
||||
const transaction = db.transaction(Object.keys(stores), 'readwrite');
|
||||
console.log('jsonData.length', jsonData.length);
|
||||
|
||||
// 对每个对象存储插入数据
|
||||
for (const storeName in stores) {
|
||||
const store = transaction.objectStore(storeName);
|
||||
const data = stores[storeName];
|
||||
let completedOperations = 0;
|
||||
const totalOperations = jsonData.length;
|
||||
|
||||
console.log(`插入数据到 ${storeName} 对象存储`);
|
||||
for (let i = 0; i < jsonData.length; i++) {
|
||||
const addObject = objectStore.put(jsonData[i].value, jsonData[i].key);
|
||||
|
||||
// 递归处理多层嵌套的对象
|
||||
insertNestedData(store, data);
|
||||
addObject.onsuccess = () => {
|
||||
console.log('数据添加成功', jsonData[i].key);
|
||||
completedOperations++;
|
||||
// 如果所有操作完成,调用 resolve
|
||||
if (completedOperations === totalOperations) {
|
||||
resolve('所有数据写入完成');
|
||||
}
|
||||
|
||||
// 事务完成时触发
|
||||
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);
|
||||
addObject.onerror = (event) => {
|
||||
console.error('数据添加失败:', event.target.error);
|
||||
reject('数据添加失败');
|
||||
};
|
||||
}
|
||||
|
||||
// 递归插入数据的函数,处理多层嵌套
|
||||
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);
|
||||
}
|
||||
// 事务错误处理
|
||||
transaction.onerror = (event) => {
|
||||
console.error('事务失败:', event.target.error);
|
||||
reject('事务失败');
|
||||
};
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
openRequest.onerror = (event) => {
|
||||
console.error('数据库打开失败:', event.target.error);
|
||||
reject('数据库打开失败');
|
||||
};
|
||||
});
|
||||
}
|
||||
}, jsonData); // 将 jsonData 传递到浏览器上下文中
|
||||
|
||||
// 等待一段时间,确保数据写入完成
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
console.log('end');
|
||||
|
||||
await page.goto('https://hlk.meiguanjia.net');
|
||||
|
||||
// await
|
||||
|
||||
// await browser.close();
|
||||
})();
|
||||
|
||||
@ -70,20 +70,22 @@ import { resolve } from 'path';
|
||||
const transaction = db.transaction(db.objectStoreNames, 'readonly');
|
||||
|
||||
const objectStore = transaction.objectStore(db.objectStoreNames[0]);
|
||||
// resolve('success');
|
||||
const cursorRequest = objectStore.openCursor();
|
||||
|
||||
const allData = [];
|
||||
cursorRequest.onsuccess = (event) => {
|
||||
const cursor = event.target.result;
|
||||
if (cursor) {
|
||||
console.log(cursor.key);
|
||||
|
||||
// console.log(cursor.key);
|
||||
// console.log(JSON.stringify(value));
|
||||
const key = cursor.key;
|
||||
const value = cursor.value;
|
||||
console.log(JSON.stringify(value));
|
||||
|
||||
allData.push({ key, value });
|
||||
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve('success');
|
||||
resolve(allData);
|
||||
}
|
||||
};
|
||||
};
|
||||
@ -93,10 +95,17 @@ import { resolve } from 'path';
|
||||
};
|
||||
});
|
||||
});
|
||||
console.log(result);
|
||||
// console.log(result);
|
||||
// const cookie = await page.cookies();
|
||||
// fs.writeFileSync('./cookie.json', JSON.stringify(cookie));
|
||||
const localStorageData = await page.evaluate(() => {
|
||||
return JSON.stringify(localStorage); // 将 localStorage 转换为对象
|
||||
});
|
||||
fs.writeFileSync('./localStorage.json', localStorageData);
|
||||
fs.writeFileSync('./indexedDB.json', JSON.stringify(result));
|
||||
}
|
||||
|
||||
// await browser.close();
|
||||
await browser.close();
|
||||
})().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user