Skip to content

Commit

Permalink
feat: add login in puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
hidaviddong committed Jul 19, 2024
1 parent cfa4399 commit 036c096
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EMAIL=
PASSWORD=
3 changes: 3 additions & 0 deletions .github/workflows/puppeteer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.env
25 changes: 19 additions & 6 deletions scripts/puppeteer.js
Original file line number Diff line number Diff line change
@@ -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"]');
Expand All @@ -33,4 +46,4 @@ try {
console.error('操作超时或发生错误:', error);
}

await browser.close();
await browser.close();

0 comments on commit 036c096

Please sign in to comment.