From 08c563143d7fd1ae868e90a950cbd51ea46fe279 Mon Sep 17 00:00:00 2001 From: liuweiqing Date: Tue, 7 May 2024 13:48:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0local=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E8=AF=BB=E5=8F=96=E6=96=B9=E4=BE=BF=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 1 + README_zh.md | 6 +++++- pteer.js | 23 ++++++++++++++++------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.env b/.env index 903ed959..48131c41 100644 --- a/.env +++ b/.env @@ -2,3 +2,4 @@ USERNAMES=hahaha2,hahaha1,ha1,ha2,DrKBoogieWoogie,lsp,lsb #888存在问题 PASSWORDS=BfdSGt}F4!5pLHt,BfdSGt}F4!5pLHt,iTs9Wx4+3Eavkhk,W$RhQUVn5E?BBBq,]"w%2Qd%M8Z6-SS,HU5B6Ee]kRB%Qj',uYE)46Ej8phu$#u WEBSITE=https://linux.do # 需要阅读的网站,支持后面那些:https://meta.discourse.org, https://meta.appinn.net/, https://community.openai.com/ +ENVIRONMENT=production \ No newline at end of file diff --git a/README_zh.md b/README_zh.md index ba6e1d75..35eca029 100644 --- a/README_zh.md +++ b/README_zh.md @@ -1,6 +1,6 @@ ## 使用方法一:油猴脚本 -油猴脚本代码在 index_passage_list 中 +油猴脚本代码在 index_passage_list 中,建议在使用前将浏览器页面缩小,这样子可以一次读更多的回复 油猴:https://greasyfork.org/en/scripts/489464-auto-read ## 使用方法二:puppeteer 无头运行 @@ -27,6 +27,10 @@ sudo apt-get install -y wget unzip fontconfig locales gconf-service libasound2 l ``` +使用方法2.1:puppeteer 有头运行(有浏览器界面) + +在目录新建.env.local,添加ENVIRONMENT=dev,就可以有头运行 + ## 使用方法三:GitHub Action 每天 0 点阅读 (可自行修改启动时间和持续时间,代码.github\workflows\cron_read.yaml) diff --git a/pteer.js b/pteer.js index 28529877..49c394d3 100644 --- a/pteer.js +++ b/pteer.js @@ -2,7 +2,17 @@ const fs = require("fs"); const path = require("path"); const puppeteer = require("puppeteer"); -require("dotenv").config(); +const dotenv = require("dotenv"); + +// Load the default .env file +dotenv.config(); +if (fs.existsSync(".env.local")) { + console.log("Using .env.local file to supply config environment variables"); + const envConfig = dotenv.parse(fs.readFileSync(".env.local")); + for (const k in envConfig) { + process.env[k] = envConfig[k]; + } +} // 从环境变量解析用户名和密码 const usernames = process.env.USERNAMES.split(","); const passwords = process.env.PASSWORDS.split(","); @@ -40,7 +50,7 @@ function delayClick(time) { async function launchBrowserForUser(username, password) { try { const browser = await puppeteer.launch({ - headless: true, + headless: process.env.ENVIRONMENT !== "dev", // 当ENVIRONMENT不是'dev'时启用无头模式 args: ["--no-sandbox", "--disable-setuid-sandbox"], //linux需要 defaultViewport: { width: 1280, @@ -138,12 +148,11 @@ async function launchBrowserForUser(username, password) { const [scriptToEval] = args; eval(scriptToEval); }, externalScript); - // 添加一个监听器来监听每次页面加载完成的事件 - // page.on("load", async () => { - // await page.evaluate(externalScript); - // }); - await page.goto("https://linux.do/t/topic/13716/110"); + page.on("load", async () => { + // await page.evaluate(externalScript); //因为这个是在页面加载好之后执行的,而脚本是在页面加载好时刻来判断是否要执行,由于已经加载好了,脚本就不会起作用 + }); + await page.goto("https://linux.do/t/topic/13716/190"); } catch (err) { console.log(err); }