Skip to content

Commit

Permalink
feat: 增加local变量的读取方便调试
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed May 7, 2024
1 parent 33652cc commit 08c5631
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 使用方法一:油猴脚本

油猴脚本代码在 index_passage_list 中
油猴脚本代码在 index_passage_list 中,建议在使用前将浏览器页面缩小,这样子可以一次读更多的回复
油猴:https://greasyfork.org/en/scripts/489464-auto-read

## 使用方法二:puppeteer 无头运行
Expand All @@ -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)
Expand Down
23 changes: 16 additions & 7 deletions pteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(",");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 08c5631

Please sign in to comment.