openai-inbox 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +168 -0
- package/bin/cli.js +2 -0
- package/package.json +36 -0
- package/public/index.html +240 -0
- package/server.js +68 -0
- package/sources/_runner.js +73 -0
- package/sources/hackernews.js +33 -0
- package/sources/index.js +18 -0
- package/sources/reddit.js +31 -0
- package/sources/zhihu.js +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# 📬 open-inbox
|
|
2
|
+
|
|
3
|
+
基于 [opencli](https://github.com/jackwener/OpenCLI) 的多源消息收件箱。Node 后端 spawn `opencli` 子进程拉取数据,前端左源右消息布局,手动刷新获取最新。
|
|
4
|
+
|
|
5
|
+
当前接入:
|
|
6
|
+
- **Hacker News** (top / new / best / ask / show / jobs) — 免登录,公开 API
|
|
7
|
+
- **Reddit** (hot / popular / frontpage) — 免登录,首次会启动 Chromium
|
|
8
|
+
- **知乎** (热榜 / 推荐) — ⚠️ 当前返回空,见下文「已知问题」
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 一、首次准备(只做一次)
|
|
13
|
+
|
|
14
|
+
### 1. 装 opencli
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install -g opencli
|
|
18
|
+
opencli --version # 应输出 1.8.3 或更高
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### 2. 装项目依赖
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
cd ~\Desktop\opencli-inbox
|
|
25
|
+
npm install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> Reddit 走浏览器自动化,**首次**调用会拉起 Chromium,后续会复用 session。HN 直接走公开 API,不需要浏览器。
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 二、启动 / 停止
|
|
33
|
+
|
|
34
|
+
### 前台启动(开发常用,关终端即停)
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
cd ~\Desktop\opencli-inbox
|
|
38
|
+
npm start
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
看到 `📬 Inbox running at http://localhost:3000` 就说明起来了,浏览器打开该地址。**停止**:终端按 `Ctrl + C`。
|
|
42
|
+
|
|
43
|
+
### 后台启动(关终端不退,日志写文件)
|
|
44
|
+
|
|
45
|
+
```cmd
|
|
46
|
+
:: 启动(默认端口 3000)
|
|
47
|
+
npm run start:bg
|
|
48
|
+
|
|
49
|
+
:: 查看日志
|
|
50
|
+
npm run logs
|
|
51
|
+
:: 或:type logs\server.log
|
|
52
|
+
|
|
53
|
+
:: 停止
|
|
54
|
+
npm run stop:bg
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
也可以直接调脚本:`scripts\start-bg.cmd` / `scripts\stop-bg.cmd`(推荐 cmd / PowerShell 调用,Git Bash 可用 `cmd //c scripts/start-bg.cmd`)。
|
|
58
|
+
|
|
59
|
+
工作机制:
|
|
60
|
+
- `start-bg.cmd` 用 `start /b` 把 `node server.js` detach 到后台,日志输出到 `logs\server.log`,PID 写进 `.server.pid`
|
|
61
|
+
- `stop-bg.cmd` 优先用 `.server.pid` 杀;找不到再回退到「占用 PORT 的进程」
|
|
62
|
+
|
|
63
|
+
### 自定义端口
|
|
64
|
+
|
|
65
|
+
```cmd
|
|
66
|
+
:: 前台
|
|
67
|
+
set PORT=4000 && npm start
|
|
68
|
+
|
|
69
|
+
:: 后台
|
|
70
|
+
set PORT=4000 && npm run start:bg
|
|
71
|
+
set PORT=4000 && npm run stop:bg :: 停止时端口要保持一致
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 三、知乎需要登录
|
|
77
|
+
|
|
78
|
+
知乎热榜/推荐都要求登录,首次进入「知乎」tab 通常会看到 `0 条 · 该源暂无数据`。
|
|
79
|
+
|
|
80
|
+
操作步骤:
|
|
81
|
+
1. 点空数据态下的 **「打开 知乎 登录页」** 按钮
|
|
82
|
+
2. opencli 会唤起浏览器(前台可见)并跳到知乎登录页 — **这是 opencli 自身的登录机制**,不是另起一个浏览器
|
|
83
|
+
3. 在浏览器完成登录(扫码 / 密码均可)
|
|
84
|
+
4. 登录成功后**关闭那个浏览器标签**,回到 inbox 点 **↻ 刷新**
|
|
85
|
+
|
|
86
|
+
opencli 用 `--site-session persistent` 保留 cookies,后续 `npm start` 不需要重新登录,除非 cookies 过期。
|
|
87
|
+
|
|
88
|
+
如果一直拿不到数据,排查:
|
|
89
|
+
```bash
|
|
90
|
+
opencli zhihu hot -f json --window foreground # 直接跑一次,看浏览器里发生了什么
|
|
91
|
+
opencli profile list # 看 session 状态
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## 四、使用
|
|
95
|
+
|
|
96
|
+
- 左侧点 **Hacker News** 或 **Reddit** 切换源
|
|
97
|
+
- 顶部下拉切换 feed (top / hot / …)
|
|
98
|
+
- 点 **↻ 刷新** 重新拉数据(每次刷新都会重新 spawn opencli)
|
|
99
|
+
- 状态栏显示「条数 · 时间 · 耗时」
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## 五、常见问题
|
|
104
|
+
|
|
105
|
+
### 启动报 `EADDRINUSE: address already in use :::3000`
|
|
106
|
+
端口被占,可能上一个服务没关。两种解法:
|
|
107
|
+
```bash
|
|
108
|
+
# A. 换端口
|
|
109
|
+
PORT=4000 npm start
|
|
110
|
+
|
|
111
|
+
# B. 杀掉占用端口的进程 (Windows)
|
|
112
|
+
netstat -ano | findstr :3000
|
|
113
|
+
taskkill /PID <上一步看到的PID> /F
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 刷新时弹出 `Connect Timeout Error`
|
|
117
|
+
opencli 调用外部 API 偶发网络抖动,**再点一次刷新**通常就好。HN 的源在 `hacker-news.firebaseio.com`,Reddit 走的是真实浏览器,都需要能访问外网。
|
|
118
|
+
|
|
119
|
+
### Reddit 第一次特别慢 / 卡住
|
|
120
|
+
首次会自动下载并启动 Chromium,可能要 30 秒以上。后续会快很多。如果一直卡:
|
|
121
|
+
```bash
|
|
122
|
+
opencli reddit hot -f json # 单独跑一次,看真实报错
|
|
123
|
+
opencli doctor # 检查浏览器桥连接
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 想加新数据源
|
|
127
|
+
1. 在 `sources/` 下新建 `xxx.js`,实现:
|
|
128
|
+
```js
|
|
129
|
+
export default {
|
|
130
|
+
id: 'xxx',
|
|
131
|
+
label: '显示名',
|
|
132
|
+
icon: 'X',
|
|
133
|
+
feeds: [{ value: 'hot', label: 'Hot' }],
|
|
134
|
+
defaultFeed: 'hot',
|
|
135
|
+
async fetch(feed) {
|
|
136
|
+
const out = await runOpencli(['xxx', feed, '-f', 'json']);
|
|
137
|
+
return extractJson(out).map(it => ({
|
|
138
|
+
id: String(it.id), title: it.title, url: it.url,
|
|
139
|
+
score: it.score, comments: it.comments, author: it.author,
|
|
140
|
+
}));
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
```
|
|
144
|
+
2. 在 `sources/index.js` import 并放进 `sources` 数组,完工。
|
|
145
|
+
|
|
146
|
+
先用 `opencli <name> --help` 确认该 adapter 的命令名和是否支持 `-f json`。
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 六、目录结构
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
open-inbox/
|
|
154
|
+
├── server.js # Express 路由
|
|
155
|
+
├── package.json
|
|
156
|
+
├── sources/
|
|
157
|
+
│ ├── _runner.js # spawn opencli + 容错 JSON 解析
|
|
158
|
+
│ ├── index.js # 源注册表
|
|
159
|
+
│ ├── hackernews.js # HN adapter
|
|
160
|
+
│ └── reddit.js # Reddit adapter
|
|
161
|
+
└── public/
|
|
162
|
+
└── index.html # 前端单页
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
API:
|
|
166
|
+
- `GET /api/sources` — 列出所有源(含 `supportsLogin` 标记)
|
|
167
|
+
- `GET /api/sources/:id/messages?feed=<feed>` — 拉某源某 feed 的消息
|
|
168
|
+
- `POST /api/sources/:id/login` — 触发该源的登录流程(目前只有知乎实现)
|
package/bin/cli.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "openai-inbox",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Multi-source message inbox powered by opencli (Hacker News / Reddit / 知乎).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"open-inbox": "./bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin/",
|
|
11
|
+
"server.js",
|
|
12
|
+
"sources/",
|
|
13
|
+
"public/",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"opencli",
|
|
21
|
+
"inbox",
|
|
22
|
+
"hackernews",
|
|
23
|
+
"reddit",
|
|
24
|
+
"zhihu"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"start": "node server.js",
|
|
29
|
+
"start:bg": "scripts\\start-bg.cmd",
|
|
30
|
+
"stop:bg": "scripts\\stop-bg.cmd",
|
|
31
|
+
"logs": "type logs\\server.log"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"express": "^4.19.2"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
6
|
+
<title>Inbox · powered by opencli</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
color-scheme: light dark;
|
|
10
|
+
--border: rgba(127,127,127,.25);
|
|
11
|
+
--muted: #888;
|
|
12
|
+
--bg-active: rgba(99, 102, 241, .12);
|
|
13
|
+
--accent: #6366f1;
|
|
14
|
+
}
|
|
15
|
+
* { box-sizing: border-box; }
|
|
16
|
+
html, body { height: 100%; margin: 0; }
|
|
17
|
+
body {
|
|
18
|
+
font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", sans-serif;
|
|
19
|
+
display: grid;
|
|
20
|
+
grid-template-columns: 220px 1fr;
|
|
21
|
+
grid-template-rows: 100vh;
|
|
22
|
+
}
|
|
23
|
+
/* ---------- sidebar ---------- */
|
|
24
|
+
aside {
|
|
25
|
+
border-right: 1px solid var(--border);
|
|
26
|
+
padding: 1rem .8rem;
|
|
27
|
+
overflow-y: auto;
|
|
28
|
+
}
|
|
29
|
+
aside h2 {
|
|
30
|
+
font-size: .75rem; text-transform: uppercase; letter-spacing: .08em;
|
|
31
|
+
color: var(--muted); margin: 0 0 .6rem .4rem;
|
|
32
|
+
}
|
|
33
|
+
.src {
|
|
34
|
+
display: flex; align-items: center; gap: .6rem;
|
|
35
|
+
padding: .55rem .6rem; border-radius: 8px; cursor: pointer;
|
|
36
|
+
user-select: none;
|
|
37
|
+
}
|
|
38
|
+
.src:hover { background: rgba(127,127,127,.08); }
|
|
39
|
+
.src.active { background: var(--bg-active); color: var(--accent); font-weight: 600; }
|
|
40
|
+
.src .icon {
|
|
41
|
+
width: 26px; height: 26px; border-radius: 6px;
|
|
42
|
+
display: grid; place-items: center;
|
|
43
|
+
background: rgba(127,127,127,.15); font-weight: 700; font-size: .8rem;
|
|
44
|
+
}
|
|
45
|
+
.src.active .icon { background: var(--accent); color: white; }
|
|
46
|
+
|
|
47
|
+
/* ---------- main ---------- */
|
|
48
|
+
main { display: flex; flex-direction: column; overflow: hidden; }
|
|
49
|
+
header {
|
|
50
|
+
display: flex; align-items: center; gap: .8rem; flex-wrap: wrap;
|
|
51
|
+
padding: .9rem 1.2rem; border-bottom: 1px solid var(--border);
|
|
52
|
+
}
|
|
53
|
+
header h1 { margin: 0; font-size: 1.1rem; }
|
|
54
|
+
select, button {
|
|
55
|
+
padding: .35rem .7rem; font-size: .9rem;
|
|
56
|
+
border-radius: 6px; border: 1px solid var(--border);
|
|
57
|
+
background: transparent; color: inherit; cursor: pointer;
|
|
58
|
+
}
|
|
59
|
+
button:hover { background: rgba(127,127,127,.1); }
|
|
60
|
+
button.primary { background: var(--accent); color: white; border-color: var(--accent); }
|
|
61
|
+
button.primary:hover { filter: brightness(1.1); }
|
|
62
|
+
#status { color: var(--muted); font-size: .8rem; margin-left: auto; }
|
|
63
|
+
#status.err { color: #d33; }
|
|
64
|
+
|
|
65
|
+
#list { flex: 1; overflow-y: auto; padding: 0 1.2rem 2rem; }
|
|
66
|
+
.msg {
|
|
67
|
+
display: grid; grid-template-columns: 2.4rem 1fr; gap: .7rem;
|
|
68
|
+
padding: .8rem 0; border-bottom: 1px solid var(--border);
|
|
69
|
+
}
|
|
70
|
+
.rank { color: var(--muted); font-variant-numeric: tabular-nums; padding-top: .15rem; font-size: .9rem; }
|
|
71
|
+
.title { font-weight: 600; line-height: 1.35; }
|
|
72
|
+
.title a { color: inherit; text-decoration: none; }
|
|
73
|
+
.title a:hover { text-decoration: underline; color: var(--accent); }
|
|
74
|
+
.meta { color: var(--muted); font-size: .8rem; margin-top: .25rem; }
|
|
75
|
+
.meta a { color: inherit; }
|
|
76
|
+
.empty { color: var(--muted); padding: 2rem 0; text-align: center; }
|
|
77
|
+
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin 1s linear infinite; vertical-align: middle; }
|
|
78
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
79
|
+
|
|
80
|
+
@media (max-width: 640px) {
|
|
81
|
+
body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
|
|
82
|
+
aside { border-right: none; border-bottom: 1px solid var(--border); padding: .6rem; }
|
|
83
|
+
aside h2 { display: none; }
|
|
84
|
+
aside ul { display: flex; gap: .4rem; overflow-x: auto; padding: 0; margin: 0; list-style: none; }
|
|
85
|
+
.src { white-space: nowrap; }
|
|
86
|
+
}
|
|
87
|
+
</style>
|
|
88
|
+
</head>
|
|
89
|
+
<body>
|
|
90
|
+
<aside>
|
|
91
|
+
<h2>Sources</h2>
|
|
92
|
+
<ul id="sources" style="list-style:none;padding:0;margin:0"></ul>
|
|
93
|
+
</aside>
|
|
94
|
+
<main>
|
|
95
|
+
<header>
|
|
96
|
+
<h1 id="title">📬 Inbox</h1>
|
|
97
|
+
<select id="feed"></select>
|
|
98
|
+
<button id="refresh" class="primary">↻ 刷新</button>
|
|
99
|
+
<span id="status">初始化…</span>
|
|
100
|
+
</header>
|
|
101
|
+
<div id="list"><div class="empty">选择左侧数据源</div></div>
|
|
102
|
+
</main>
|
|
103
|
+
|
|
104
|
+
<script>
|
|
105
|
+
const $ = (id) => document.getElementById(id);
|
|
106
|
+
const esc = (s) => String(s ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
107
|
+
|
|
108
|
+
let sources = [];
|
|
109
|
+
let currentSource = null;
|
|
110
|
+
|
|
111
|
+
async function init() {
|
|
112
|
+
const r = await fetch('/api/sources');
|
|
113
|
+
const data = await r.json();
|
|
114
|
+
sources = data.sources;
|
|
115
|
+
renderSources();
|
|
116
|
+
if (sources.length) selectSource(sources[0].id);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function renderSources() {
|
|
120
|
+
$('sources').innerHTML = sources.map(s => `
|
|
121
|
+
<li class="src ${currentSource?.id === s.id ? 'active' : ''}" data-id="${esc(s.id)}">
|
|
122
|
+
<span class="icon">${esc(s.icon || s.label[0])}</span>
|
|
123
|
+
<span>${esc(s.label)}</span>
|
|
124
|
+
</li>`).join('');
|
|
125
|
+
$('sources').querySelectorAll('.src').forEach(el => {
|
|
126
|
+
el.onclick = () => selectSource(el.dataset.id);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function selectSource(id) {
|
|
131
|
+
currentSource = sources.find(s => s.id === id);
|
|
132
|
+
if (!currentSource) return;
|
|
133
|
+
$('title').textContent = `📬 ${currentSource.label}`;
|
|
134
|
+
$('feed').innerHTML = currentSource.feeds
|
|
135
|
+
.map(f => `<option value="${esc(f.value)}">${esc(f.label)}</option>`).join('');
|
|
136
|
+
$('feed').value = currentSource.defaultFeed;
|
|
137
|
+
renderSources();
|
|
138
|
+
load();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function load() {
|
|
142
|
+
if (!currentSource) return;
|
|
143
|
+
const feed = $('feed').value;
|
|
144
|
+
$('status').classList.remove('err');
|
|
145
|
+
$('status').innerHTML = `<span class="spinner"></span> 加载中…`;
|
|
146
|
+
$('list').innerHTML = `<div class="empty"><span class="spinner"></span> 拉取 ${esc(currentSource.label)} ${esc(feed)}…</div>`;
|
|
147
|
+
try {
|
|
148
|
+
const r = await fetch(`/api/sources/${encodeURIComponent(currentSource.id)}/messages?feed=${encodeURIComponent(feed)}`);
|
|
149
|
+
const data = await r.json();
|
|
150
|
+
if (!data.ok) throw new Error(data.error);
|
|
151
|
+
$('status').textContent = `${data.messages.length} 条 · ${new Date(data.fetchedAt).toLocaleTimeString()} · ${data.tookMs}ms`;
|
|
152
|
+
if (!data.messages.length) {
|
|
153
|
+
const loginBtn = currentSource.supportsLogin
|
|
154
|
+
? `<button id="loginBtn" class="primary" style="margin-top:1rem">打开 ${esc(currentSource.label)} 登录页</button>`
|
|
155
|
+
: '';
|
|
156
|
+
$('list').innerHTML = `<div class="empty">
|
|
157
|
+
该源暂无数据(可能需要登录或被反爬拦截)<br>${loginBtn}
|
|
158
|
+
</div>`;
|
|
159
|
+
const btn = document.getElementById('loginBtn');
|
|
160
|
+
if (btn) btn.onclick = doLogin;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const ext = !!currentSource.externalOpen;
|
|
164
|
+
const renderLink = (url, text) => {
|
|
165
|
+
if (!url) return esc(text);
|
|
166
|
+
if (ext) {
|
|
167
|
+
return `<a href="#" class="ext-link" data-url="${esc(url)}" title="在 opencli 浏览器打开">${esc(text)} ↗</a>`;
|
|
168
|
+
}
|
|
169
|
+
return `<a href="${esc(url)}" target="_blank" rel="noopener">${esc(text)}</a>`;
|
|
170
|
+
};
|
|
171
|
+
$('list').innerHTML = data.messages.map((m, i) => {
|
|
172
|
+
const metaParts = (m.meta || [])
|
|
173
|
+
.map(x => `${esc(x.icon)} ${esc(x.value)}`)
|
|
174
|
+
.concat(m.author ? [`by ${esc(m.author)}`] : [])
|
|
175
|
+
.concat(m.discussUrl ? [renderLink(m.discussUrl, 'discuss')] : []);
|
|
176
|
+
return `
|
|
177
|
+
<div class="msg">
|
|
178
|
+
<div class="rank">${esc(m.extra?.rank ?? i + 1)}</div>
|
|
179
|
+
<div>
|
|
180
|
+
<div class="title">${renderLink(m.url, m.title)}</div>
|
|
181
|
+
<div class="meta">${metaParts.join(' · ')}</div>
|
|
182
|
+
</div>
|
|
183
|
+
</div>`;
|
|
184
|
+
}).join('');
|
|
185
|
+
if (ext) bindExtLinks();
|
|
186
|
+
} catch (e) {
|
|
187
|
+
$('status').textContent = '错误: ' + e.message;
|
|
188
|
+
$('status').classList.add('err');
|
|
189
|
+
$('list').innerHTML = `<div class="empty" style="color:#d33">⚠ ${esc(e.message)}</div>`;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function doLogin() {
|
|
194
|
+
if (!currentSource) return;
|
|
195
|
+
const btn = document.getElementById('loginBtn');
|
|
196
|
+
if (btn) { btn.disabled = true; btn.textContent = '正在唤起浏览器…'; }
|
|
197
|
+
try {
|
|
198
|
+
const r = await fetch(`/api/sources/${encodeURIComponent(currentSource.id)}/login`, { method: 'POST' });
|
|
199
|
+
const data = await r.json();
|
|
200
|
+
if (!data.ok) throw new Error(data.error);
|
|
201
|
+
$('status').textContent = data.message || '已唤起浏览器,登录后点刷新';
|
|
202
|
+
if (btn) { btn.textContent = '已唤起,请在浏览器登录'; }
|
|
203
|
+
} catch (e) {
|
|
204
|
+
$('status').textContent = '登录失败: ' + e.message;
|
|
205
|
+
$('status').classList.add('err');
|
|
206
|
+
if (btn) { btn.disabled = false; btn.textContent = '重试登录'; }
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function bindExtLinks() {
|
|
211
|
+
document.querySelectorAll('.ext-link').forEach(a => {
|
|
212
|
+
a.onclick = async (e) => {
|
|
213
|
+
e.preventDefault();
|
|
214
|
+
if (!currentSource) return;
|
|
215
|
+
const url = a.dataset.url;
|
|
216
|
+
const original = a.textContent;
|
|
217
|
+
a.textContent = '打开中…';
|
|
218
|
+
try {
|
|
219
|
+
const r = await fetch(
|
|
220
|
+
`/api/sources/${encodeURIComponent(currentSource.id)}/open?url=${encodeURIComponent(url)}`,
|
|
221
|
+
{ method: 'POST' },
|
|
222
|
+
);
|
|
223
|
+
const data = await r.json();
|
|
224
|
+
if (!data.ok) throw new Error(data.error);
|
|
225
|
+
a.textContent = original;
|
|
226
|
+
} catch (err) {
|
|
227
|
+
a.textContent = original;
|
|
228
|
+
$('status').textContent = '打开失败: ' + err.message;
|
|
229
|
+
$('status').classList.add('err');
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
$('refresh').onclick = load;
|
|
236
|
+
$('feed').onchange = load;
|
|
237
|
+
init();
|
|
238
|
+
</script>
|
|
239
|
+
</body>
|
|
240
|
+
</html>
|
package/server.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { listSources, getSource } from './sources/index.js';
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const app = express();
|
|
8
|
+
|
|
9
|
+
app.get('/api/sources', (_req, res) => {
|
|
10
|
+
res.json({ ok: true, sources: listSources() });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
app.post('/api/sources/:id/open', async (req, res) => {
|
|
14
|
+
const source = getSource(req.params.id);
|
|
15
|
+
if (!source) return res.status(404).json({ ok: false, error: `unknown source ${req.params.id}` });
|
|
16
|
+
if (typeof source.openUrl !== 'function') {
|
|
17
|
+
return res.status(400).json({ ok: false, error: `${source.id} does not support external open` });
|
|
18
|
+
}
|
|
19
|
+
const url = String(req.query.url || '');
|
|
20
|
+
if (!url) return res.status(400).json({ ok: false, error: 'url required' });
|
|
21
|
+
try {
|
|
22
|
+
await source.openUrl(url);
|
|
23
|
+
res.json({ ok: true });
|
|
24
|
+
} catch (e) {
|
|
25
|
+
res.status(500).json({ ok: false, error: String(e.message || e) });
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
app.post('/api/sources/:id/login', async (req, res) => {
|
|
30
|
+
const source = getSource(req.params.id);
|
|
31
|
+
if (!source) return res.status(404).json({ ok: false, error: `unknown source ${req.params.id}` });
|
|
32
|
+
if (typeof source.login !== 'function') {
|
|
33
|
+
return res.status(400).json({ ok: false, error: `${source.id} does not support login` });
|
|
34
|
+
}
|
|
35
|
+
// Fire-and-forget: opencli will block waiting for the user to finish logging
|
|
36
|
+
// in. We don't want the HTTP request to hang for 5 minutes — kick it off and
|
|
37
|
+
// return immediately. Errors are logged server-side.
|
|
38
|
+
source.login().catch((e) => console.error(`[${source.id}] login flow:`, e.message));
|
|
39
|
+
res.json({ ok: true, message: '已唤起浏览器,请在弹出的页面完成登录,然后回到本页点刷新' });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
app.get('/api/sources/:id/messages', async (req, res) => {
|
|
43
|
+
const source = getSource(req.params.id);
|
|
44
|
+
if (!source) return res.status(404).json({ ok: false, error: `unknown source ${req.params.id}` });
|
|
45
|
+
const feed = (req.query.feed || source.defaultFeed).toString();
|
|
46
|
+
if (!source.feeds.find((f) => f.value === feed)) {
|
|
47
|
+
return res.status(400).json({ ok: false, error: `unknown feed ${feed} for ${source.id}` });
|
|
48
|
+
}
|
|
49
|
+
const t0 = Date.now();
|
|
50
|
+
try {
|
|
51
|
+
const messages = await source.fetch(feed);
|
|
52
|
+
res.json({
|
|
53
|
+
ok: true,
|
|
54
|
+
sourceId: source.id,
|
|
55
|
+
feed,
|
|
56
|
+
fetchedAt: new Date().toISOString(),
|
|
57
|
+
tookMs: Date.now() - t0,
|
|
58
|
+
messages,
|
|
59
|
+
});
|
|
60
|
+
} catch (e) {
|
|
61
|
+
res.status(500).json({ ok: false, sourceId: source.id, feed, error: String(e.message || e) });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.use(express.static(path.join(__dirname, 'public')));
|
|
66
|
+
|
|
67
|
+
const PORT = process.env.PORT || 3000;
|
|
68
|
+
app.listen(PORT, () => console.log(`📬 Inbox running at http://localhost:${PORT}`));
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
// Force a Chrome window to the foreground on Windows. opencli's --window
|
|
4
|
+
// foreground only marks the tab as active inside Chrome — Win10/11 block
|
|
5
|
+
// SetForegroundWindow when the calling process isn't the focus owner, so the
|
|
6
|
+
// window stays buried behind whatever the user is doing. We use
|
|
7
|
+
// AttachThreadInput to share input state with the foreground thread, which
|
|
8
|
+
// lifts that restriction. No-op on non-Windows.
|
|
9
|
+
export function bringChromeToFront() {
|
|
10
|
+
if (process.platform !== 'win32') return Promise.resolve();
|
|
11
|
+
const ps = `
|
|
12
|
+
$sig = @"
|
|
13
|
+
using System;
|
|
14
|
+
using System.Diagnostics;
|
|
15
|
+
using System.Runtime.InteropServices;
|
|
16
|
+
public class W {
|
|
17
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h);
|
|
18
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int n);
|
|
19
|
+
[DllImport("user32.dll")] public static extern bool BringWindowToTop(IntPtr h);
|
|
20
|
+
[DllImport("user32.dll")] public static extern bool AttachThreadInput(uint a, uint b, bool c);
|
|
21
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint p);
|
|
22
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
23
|
+
[DllImport("kernel32.dll")] public static extern uint GetCurrentThreadId();
|
|
24
|
+
}
|
|
25
|
+
"@
|
|
26
|
+
Add-Type -TypeDefinition $sig -ErrorAction SilentlyContinue
|
|
27
|
+
$proc = Get-Process chrome -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1
|
|
28
|
+
if ($proc) {
|
|
29
|
+
$h = $proc.MainWindowHandle
|
|
30
|
+
$fg = [W]::GetForegroundWindow()
|
|
31
|
+
$cur = [W]::GetCurrentThreadId()
|
|
32
|
+
$fgTid = [W]::GetWindowThreadProcessId($fg, [ref]([uint32]0))
|
|
33
|
+
[W]::AttachThreadInput($fgTid, $cur, $true) | Out-Null
|
|
34
|
+
[W]::ShowWindow($h, 9) | Out-Null
|
|
35
|
+
[W]::BringWindowToTop($h) | Out-Null
|
|
36
|
+
[W]::SetForegroundWindow($h) | Out-Null
|
|
37
|
+
[W]::AttachThreadInput($fgTid, $cur, $false) | Out-Null
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
const child = spawn('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', ps], { windowsHide: true });
|
|
42
|
+
child.on('close', () => resolve());
|
|
43
|
+
child.on('error', () => resolve());
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function runOpencli(args, { timeoutMs = 60_000 } = {}) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const child = spawn('opencli', args, { shell: process.platform === 'win32' });
|
|
50
|
+
let stdout = '';
|
|
51
|
+
let stderr = '';
|
|
52
|
+
const timer = setTimeout(() => {
|
|
53
|
+
child.kill();
|
|
54
|
+
reject(new Error(`opencli ${args.join(' ')} timed out after ${timeoutMs}ms`));
|
|
55
|
+
}, timeoutMs);
|
|
56
|
+
child.stdout.on('data', (d) => (stdout += d));
|
|
57
|
+
child.stderr.on('data', (d) => (stderr += d));
|
|
58
|
+
child.on('error', (e) => { clearTimeout(timer); reject(e); });
|
|
59
|
+
child.on('close', (code) => {
|
|
60
|
+
clearTimeout(timer);
|
|
61
|
+
if (code !== 0) return reject(new Error(stderr.trim() || `opencli exit ${code}`));
|
|
62
|
+
resolve(stdout);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// opencli sometimes prints "⚠ Batch fetch failed..." warnings before the JSON
|
|
68
|
+
// payload. Find the first '[' or '{' and parse from there.
|
|
69
|
+
export function extractJson(raw) {
|
|
70
|
+
const start = raw.search(/[\[{]/);
|
|
71
|
+
if (start < 0) throw new Error('no JSON in opencli output: ' + raw.slice(0, 200));
|
|
72
|
+
return JSON.parse(raw.slice(start));
|
|
73
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { runOpencli, extractJson } from './_runner.js';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
id: 'hackernews',
|
|
5
|
+
label: 'Hacker News',
|
|
6
|
+
icon: 'Y',
|
|
7
|
+
feeds: [
|
|
8
|
+
{ value: 'top', label: 'Top' },
|
|
9
|
+
{ value: 'new', label: 'New' },
|
|
10
|
+
{ value: 'best', label: 'Best' },
|
|
11
|
+
{ value: 'ask', label: 'Ask HN' },
|
|
12
|
+
{ value: 'show', label: 'Show HN' },
|
|
13
|
+
{ value: 'jobs', label: 'Jobs' },
|
|
14
|
+
],
|
|
15
|
+
defaultFeed: 'top',
|
|
16
|
+
|
|
17
|
+
async fetch(feed = 'top') {
|
|
18
|
+
const out = await runOpencli(['hackernews', feed, '-f', 'json']);
|
|
19
|
+
const items = extractJson(out);
|
|
20
|
+
return items.map((it) => ({
|
|
21
|
+
id: String(it.id),
|
|
22
|
+
title: it.title,
|
|
23
|
+
url: it.url || `https://news.ycombinator.com/item?id=${it.id}`,
|
|
24
|
+
author: it.author ?? '',
|
|
25
|
+
discussUrl: `https://news.ycombinator.com/item?id=${it.id}`,
|
|
26
|
+
meta: [
|
|
27
|
+
{ icon: '▲', value: it.score ?? 0 },
|
|
28
|
+
{ icon: '💬', value: it.comments ?? 0 },
|
|
29
|
+
],
|
|
30
|
+
extra: { rank: it.rank },
|
|
31
|
+
}));
|
|
32
|
+
},
|
|
33
|
+
};
|
package/sources/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import hackernews from './hackernews.js';
|
|
2
|
+
import reddit from './reddit.js';
|
|
3
|
+
import zhihu from './zhihu.js';
|
|
4
|
+
|
|
5
|
+
const sources = [hackernews, reddit, zhihu];
|
|
6
|
+
const byId = new Map(sources.map((s) => [s.id, s]));
|
|
7
|
+
|
|
8
|
+
export function listSources() {
|
|
9
|
+
return sources.map(({ id, label, icon, feeds, defaultFeed, supportsLogin, externalOpen }) => ({
|
|
10
|
+
id, label, icon, feeds, defaultFeed,
|
|
11
|
+
supportsLogin: !!supportsLogin,
|
|
12
|
+
externalOpen: !!externalOpen,
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getSource(id) {
|
|
17
|
+
return byId.get(id);
|
|
18
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { runOpencli, extractJson } from './_runner.js';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
id: 'reddit',
|
|
5
|
+
label: 'Reddit',
|
|
6
|
+
icon: 'R',
|
|
7
|
+
feeds: [
|
|
8
|
+
{ value: 'hot', label: 'Hot' },
|
|
9
|
+
{ value: 'popular', label: 'Popular' },
|
|
10
|
+
{ value: 'frontpage', label: 'Frontpage' },
|
|
11
|
+
],
|
|
12
|
+
defaultFeed: 'hot',
|
|
13
|
+
|
|
14
|
+
async fetch(feed = 'hot') {
|
|
15
|
+
const out = await runOpencli(['reddit', feed, '-f', 'json'], { timeoutMs: 120_000 });
|
|
16
|
+
const items = extractJson(out);
|
|
17
|
+
return items.map((it) => ({
|
|
18
|
+
id: String(it.postId ?? it.id ?? it.rank),
|
|
19
|
+
title: it.title,
|
|
20
|
+
url: it.url_overridden_by_dest || it.url,
|
|
21
|
+
author: it.author ?? '',
|
|
22
|
+
discussUrl: it.url,
|
|
23
|
+
meta: [
|
|
24
|
+
{ icon: '▲', value: it.score ?? 0 },
|
|
25
|
+
{ icon: '💬', value: it.comments ?? 0 },
|
|
26
|
+
it.subreddit ? { icon: '#', value: it.subreddit } : null,
|
|
27
|
+
].filter(Boolean),
|
|
28
|
+
extra: { rank: it.rank, subreddit: it.subreddit },
|
|
29
|
+
}));
|
|
30
|
+
},
|
|
31
|
+
};
|
package/sources/zhihu.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { runOpencli, extractJson, bringChromeToFront } from './_runner.js';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
id: 'zhihu',
|
|
5
|
+
label: '知乎',
|
|
6
|
+
icon: '知',
|
|
7
|
+
feeds: [
|
|
8
|
+
{ value: 'hot', label: '热榜' },
|
|
9
|
+
{ value: 'recommend', label: '推荐' },
|
|
10
|
+
],
|
|
11
|
+
defaultFeed: 'hot',
|
|
12
|
+
// marker for the frontend: this source supports the login flow below
|
|
13
|
+
supportsLogin: true,
|
|
14
|
+
// marker for the frontend: clicking a message link should be routed through
|
|
15
|
+
// /api/sources/:id/open instead of <a href>, so the URL is opened in
|
|
16
|
+
// opencli's logged-in Chrome rather than the user's regular browser.
|
|
17
|
+
externalOpen: true,
|
|
18
|
+
|
|
19
|
+
async fetch(feed = 'hot') {
|
|
20
|
+
const out = await runOpencli(
|
|
21
|
+
['zhihu', feed, '-f', 'json', '--site-session', 'persistent', '--keep-tab', 'true'],
|
|
22
|
+
{ timeoutMs: 120_000 },
|
|
23
|
+
);
|
|
24
|
+
const items = extractJson(out);
|
|
25
|
+
return items.map((it, i) => ({
|
|
26
|
+
id: String(it.id ?? it.questionId ?? `${feed}-${i}`),
|
|
27
|
+
title: it.title,
|
|
28
|
+
url: it.url || (it.questionId ? `https://www.zhihu.com/question/${it.questionId}` : undefined),
|
|
29
|
+
author: it.author ?? '',
|
|
30
|
+
meta: [
|
|
31
|
+
it.heat != null ? { icon: '🔥', value: it.heat } : null,
|
|
32
|
+
it.answers != null ? { icon: '💬', value: it.answers } : null,
|
|
33
|
+
].filter(Boolean),
|
|
34
|
+
extra: { rank: it.rank },
|
|
35
|
+
}));
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
// Opening a zhihu question URL cold triggers their 40362 risk-control
|
|
39
|
+
// ("您当前请求存在异常"). The same URL clicked from the homepage is fine.
|
|
40
|
+
// So we warm up the tab on https://www.zhihu.com first (which establishes
|
|
41
|
+
// a normal browsing session), wait briefly for cookies/JS to settle, then
|
|
42
|
+
// navigate the same tab to the target. Same session name keeps the tab
|
|
43
|
+
// shared so this is one continuous browsing context, not two cold loads.
|
|
44
|
+
async openUrl(url) {
|
|
45
|
+
if (!/^https?:\/\//.test(url)) throw new Error('invalid url');
|
|
46
|
+
await runOpencli(
|
|
47
|
+
['browser', 'inbox-zhihu', '--window', 'foreground', 'open', 'https://www.zhihu.com'],
|
|
48
|
+
{ timeoutMs: 60_000 },
|
|
49
|
+
);
|
|
50
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
51
|
+
const out = await runOpencli(
|
|
52
|
+
['browser', 'inbox-zhihu', '--window', 'foreground', 'open', url],
|
|
53
|
+
{ timeoutMs: 60_000 },
|
|
54
|
+
);
|
|
55
|
+
// opencli's --window foreground hits SetForegroundWindow restrictions on
|
|
56
|
+
// Win10/11 when the inbox isn't the focused app, so Chrome ends up active
|
|
57
|
+
// but buried. AttachThreadInput-based promotion lifts it visibly.
|
|
58
|
+
await bringChromeToFront();
|
|
59
|
+
return out;
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
async login() {
|
|
63
|
+
await runOpencli(
|
|
64
|
+
['browser', 'inbox-zhihu', '--window', 'foreground', 'open', 'https://www.zhihu.com/signin'],
|
|
65
|
+
{ timeoutMs: 60_000 },
|
|
66
|
+
);
|
|
67
|
+
await bringChromeToFront();
|
|
68
|
+
},
|
|
69
|
+
};
|