眾所周知的原因,國(guó)內(nèi) GitHub 的加載速度不太理想虑椎,而大多數(shù)開(kāi)發(fā)者會(huì)使用 Github 的 Github Page 功能搭建博客,因此博客的訪問(wèn)速度也會(huì)受到影響。為了解決訪問(wèn)速度的問(wèn)題阁危,我們將博客轉(zhuǎn)移到了 gitee 中( OSChina 旗下 git 平臺(tái))
1. 問(wèn)題:使用 hexo 部署到 gitee 后,博客頁(yè)面并沒(méi)有更新
- 原因: gitee page 只有付費(fèi)版才能自動(dòng)更新泥彤,免費(fèi)版只能手動(dòng)點(diǎn)擊 “設(shè)置” 中的更新按鈕
2. 自動(dòng)化解決方案
- 使用 puppeteer 操作瀏覽器進(jìn)行更新按鈕點(diǎn)擊欲芹。
3. 源碼如下:
// 此處安裝版本為 1.8.0
const puppeteer = require('puppeteer');
async function giteeUpdate() {
const browser = await puppeteer.launch({
// 此處可以使用 false 有頭模式進(jìn)行調(diào)試, 調(diào)試完注釋即可
headless: false,
});
const page = await browser.newPage();
await page.goto('https://gitee.com/login');
// 1. 選中賬號(hào)控件
let accountElements = await page.$x('//*[@id="user_login"]') // 此處使用 xpath 尋找控件,下同
// 2. 填入賬號(hào)
await accountElements[0].type('你的 gitee 賬戶')
// 3. 選中密碼控件
let pwdElements = await page.$x('//*[@id="user_password"]')
// 4. 填入密碼
await pwdElements[0].type('你的 gitee 密碼')
// 5. 點(diǎn)擊登錄
let loginButtons = await page.$x('//*[@id="new_user"]/div[2]/div/div/div[4]/input')
await loginButtons[0].click()
// 6. 等待登錄成功
await page.waitFor(1000)
await page.goto('你的 gitee page 更新按鈕頁(yè)面'); // 比如: https://gitee.com/yang0033/hexo-blog/pages
// 7.1. 監(jiān)聽(tīng)步驟 7 中觸發(fā)的確認(rèn)彈框吟吝,并點(diǎn)擊確認(rèn)
await page.on('dialog', async dialog => {
console.log('確認(rèn)更新')
dialog.accept();
})
// 7. 點(diǎn)擊更新按鈕菱父,并彈出確認(rèn)彈窗
let updateButtons = await page.$x('//*[@id="pages-branch"]/div[7]')
await updateButtons[0].click()
// 8. 輪詢并確認(rèn)是否更新完畢
while (true) {
await page.waitFor(2000)
try {
// 8.1 獲取更新?tīng)顟B(tài)標(biāo)簽
deploying = await page.$x('//*[@id="pages_deploying"]')
if (deploying.length > 0) {
console.log('更新中...')
} else {
console.log('更新完畢')
break;
}
} catch (error) {
break;
}
}
await page.waitFor(500);
// 10.更新完畢,關(guān)閉瀏覽器
browser.close();
}
giteeUpdate();