From 1d4797ece3d6cfe9576e1189020867b89a0a4dde Mon Sep 17 00:00:00 2001 From: LingandRX <56020800+LingandRX@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:11:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20HLK=20=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E6=95=B0=E6=8D=AE=E6=8B=89=E5=8F=96=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=20-=20=E6=96=B0=E5=A2=9E=20HLK=20=E5=B9=B3=E5=8F=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=92=8C=E6=95=B0=E6=8D=AE=E6=8B=89=E5=8F=96=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=20-=20=E4=BD=BF=E7=94=A8=20Puppeteer=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E8=87=AA=E5=8A=A8=E5=8C=96=E7=99=BB=E5=BD=95=E5=92=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8A=93=E5=8F=96=20-=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=89=93=E5=8D=B0=20IndexedDB=20=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hlk.spec.js | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 hlk.spec.js diff --git a/hlk.spec.js b/hlk.spec.js new file mode 100644 index 0000000..03096ea --- /dev/null +++ b/hlk.spec.js @@ -0,0 +1,133 @@ +import { launch, Locator } from 'puppeteer'; // v23.0.0 or later + +(async () => { + const browser = await launch({ + headless: false, + }); + const page = await browser.newPage(); + const timeout = 5000; + page.setDefaultTimeout(timeout); + + { + const targetPage = page; + await targetPage.goto('https://hlk.meiguanjia.net/#/login'); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('input[placeholder="请输入您的手机号码')]) + .setTimeout(timeout) + .click(); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('input[placeholder="请输入您的手机号码')]) + .setTimeout(timeout) + .fill('17770720274'); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('input[placeholder="请输入登录密码')]) + .setTimeout(timeout) + .click(); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('input[placeholder="请输入登录密码')]) + .setTimeout(timeout) + .fill('a123456'); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('#agreement')]) + .setTimeout(timeout) + .click(); + } + { + const targetPage = page; + await Locator.race([targetPage.locator('.login-form-button')]) + .setTimeout(timeout) + .click(); + await new Promise((resolve) => setTimeout(resolve, 3000)); + } + { + const targetPage = page; + + targetPage.on('console', (msg) => { + for (let i = 0; i < msg.args().length; ++i) { + console.log(`${i}: ${msg.args()[i]}`); + } + }); + + const allData = await targetPage.evaluate(() => { + return new Promise((resolve, reject) => { + // 获取所有数据库列表 + indexedDB + .databases() + .then((databases) => { + const allDatabasesData = []; + + // 遍历所有数据库 + const dbPromises = databases.map((db) => { + return new Promise((dbResolve, dbReject) => { + const request = indexedDB.open(db.name); + + request.onsuccess = () => { + const database = request.result; + const transaction = database.transaction(database.objectStoreNames, 'readonly'); + + const dbData = { name: db.name, stores: {} }; + + // 遍历数据库中的每个对象存储 + for (let storeName of database.objectStoreNames) { + const storeData = []; + const store = transaction.objectStore(storeName); + const cursorRequest = store.openCursor(); + + cursorRequest.onsuccess = (event) => { + const cursor = event.target.result; + if (cursor) { + storeData.push(cursor.value); + cursor.continue(); + } else { + // 当遍历完所有数据时,将数据保存到 dbData 中 + dbData.stores[storeName] = storeData; + if ( + Object.keys(dbData.stores).length === database.objectStoreNames.length + ) { + // 完成一个数据库的所有对象存储数据 + dbResolve(dbData); + } + } + }; + + cursorRequest.onerror = (error) => { + dbReject(error); + }; + } + }; + + request.onerror = (error) => { + dbReject(error); + }; + }); + }); + + // 等待所有数据库的数据获取完成 + Promise.all(dbPromises) + .then((results) => { + resolve(results); + }) + .catch(reject); + }) + .catch(reject); + }); + }); + + console.log(allData[0].stores.keyvaluepairs); // 打印所有数据库的数据 + } + + // await browser.close(); +})().catch((err) => { + console.error(err); + process.exit(1); +});