众所周知,国内 GitHub 的加载速度不太理想,而大多数开发者会使用 Github 的 Github Page 功能搭建博客,因此博客的访问速度也会受到影响。为了解决访问速度的问题,我们将博客转移到了 gitee 中( OSChina 旗下 git 平台)
1. 问题:使用 hexo 部署到 gitee 后,博客页面并没有更新
- 原因: gitee page 只有付费版才能自动更新,免费版只能手动点击 “设置” 中的更新按钮
2. 自动化解决方案
- 使用 puppeteer 操作浏览器进行更新按钮点击。
3. 源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| const puppeteer = require('puppeteer');
async function giteeUpdate() { const browser = await puppeteer.launch({ headless: false, }); const page = await browser.newPage(); await page.goto('https://gitee.com/login'); let accountElements = await page.$x('//*[@id="user_login"]') await accountElements[0].type('你的 gitee 账户') let pwdElements = await page.$x('//*[@id="user_password"]') await pwdElements[0].type('你的 gitee 密码') let loginButtons = await page.$x('//*[@id="new_user"]/div[2]/div/div/div[4]/input') await loginButtons[0].click() await page.waitFor(1000) await page.goto('你的 gitee page 更新按钮页面'); await page.on('dialog', async dialog => { console.log('确认更新') dialog.accept(); }) let updateButtons = await page.$x('//*[@id="pages-branch"]/div[7]') await updateButtons[0].click() while (true) { await page.waitFor(2000) try { deploying = await page.$x('//*[@id="pages_deploying"]') if (deploying.length > 0) { console.log('更新中...') } else { console.log('更新完毕') break; } } catch (error) { break; } } await page.waitFor(500); browser.close(); }
giteeUpdate();
|