From 036c0963d0dbbb55f50026a422407922d8e68caf Mon Sep 17 00:00:00 2001 From: DongHY1 Date: Fri, 19 Jul 2024 12:55:04 +0800 Subject: [PATCH] feat: add login in puppeteer --- .env example | 2 ++ .github/workflows/puppeteer.yml | 3 +++ .gitignore | 2 ++ scripts/puppeteer.js | 25 +++++++++++++++++++------ 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 .env example diff --git a/.env example b/.env example new file mode 100644 index 0000000..24bb877 --- /dev/null +++ b/.env example @@ -0,0 +1,2 @@ +EMAIL= +PASSWORD= \ No newline at end of file diff --git a/.github/workflows/puppeteer.yml b/.github/workflows/puppeteer.yml index cb6028f..b067364 100644 --- a/.github/workflows/puppeteer.yml +++ b/.github/workflows/puppeteer.yml @@ -25,6 +25,9 @@ jobs: run: if [ -f public/cv.pdf ]; then rm -f public/cv.pdf; fi - name: Run Puppeteer script + env: + EMAIL: ${{ secrets.EMAIL }} + PASSWORD: ${{ secrets.PASSWORD }} run: deno run -A scripts/puppeteer.js - name: Commit and push changes diff --git a/.gitignore b/.gitignore index a547bf3..3b0b403 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ dist-ssr *.njsproj *.sln *.sw? + +.env \ No newline at end of file diff --git a/scripts/puppeteer.js b/scripts/puppeteer.js index b7f5e94..f3aa5a5 100644 --- a/scripts/puppeteer.js +++ b/scripts/puppeteer.js @@ -1,26 +1,39 @@ +import { config } from "https://deno.land/x/dotenv/mod.ts"; import puppeteer from "https://deno.land/x/puppeteer@16.2.0/mod.ts"; - const browser = await puppeteer.launch({ - headless: true, // 设置为false以便观察浏览器动作,调试时很有用 + headless: false, // 设置为false以便观察浏览器动作,调试时很有用 args: ['--disable-web-security'] }); -const page = await browser.newPage(); +const env = config(); +const email = env.EMAIL; +const password = env.PASSWORD; +const page = await browser.newPage(); await page.setViewport({ width: 1280, height: 800 }); const client = await page.target().createCDPSession(); const downloadPath = "./public"; + await client.send('Page.setDownloadBehavior', { behavior: 'allow', downloadPath: downloadPath }); -await page.goto("https://typst.app/project/rIWFtNvpmsgHgh18iqpk2a"); +await page.goto("https://typst.app/project/r3N_OX7ukxFZcoaITyqR2p"); try { + await page.waitForTimeout(5000) + await page.waitForSelector('#email'); + await page.evaluate((email, password) => { + document.querySelector('#email').value = email; + document.querySelector('#password').value = password; + document.querySelector('#target > form > div.submit-row > input[type=submit]').click(); + }, email, password); + console.log('登录成功') + await page.waitForTimeout(10000) // 等待带有aria-label="Quick export PDF"的按钮出现,最多等待10秒钟 - await page.waitForSelector('button[aria-label="Quick export PDF"]', { timeout: 20000 }); + await page.waitForSelector('button[aria-label="Quick export PDF"]', { timeout: 10000 }); console.log('页面加载完毕') // 找到并点击带有aria-label="Quick export PDF"的按钮 const quickExportPDFButton = await page.$('button[aria-label="Quick export PDF"]'); @@ -33,4 +46,4 @@ try { console.error('操作超时或发生错误:', error); } -await browser.close(); +await browser.close(); \ No newline at end of file