send-to-pi 1.2.0 → 1.3.0
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 +57 -113
- package/README_CN.md +57 -116
- package/chrome-extension/background.js +237 -65
- package/chrome-extension/manifest.json +10 -1
- package/chrome-extension/options.html +8 -2
- package/chrome-extension/options.js +22 -11
- package/chrome-extension/popup.html +160 -0
- package/chrome-extension/popup.js +145 -0
- package/package.json +3 -2
- package/pi-extension/README.md +9 -17
- package/pi-extension/index.ts +309 -79
- package/server/bin/send-to-pi.js +97 -42
- package/server/src/clipboard.js +53 -0
- package/server/src/config.js +99 -11
- package/server/src/index.js +153 -67
- package/server/src/launchers/custom.js +55 -0
- package/server/src/launchers/darwin.js +289 -45
- package/server/src/launchers/index.js +51 -8
- package/server/src/launchers/linux.js +60 -36
- package/server/src/launchers/win32.js +112 -66
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Send to Pi - Chrome Background Service Worker
|
|
3
|
-
* Version: 1.
|
|
3
|
+
* Version: 1.3.0
|
|
4
|
+
*
|
|
5
|
+
* Dual-path delivery:
|
|
6
|
+
* 1. Primary: Local Bridge Daemon (127.0.0.1:18090) — supports cold-start terminal launch & focus.
|
|
7
|
+
* 2. Zero-Daemon Direct Fallback: Probes active Pi sessions on 127.0.0.1:18091~18095 directly
|
|
8
|
+
* and routes to the most recently active session (or user-pinned session), terminal-agnostic.
|
|
9
|
+
* 3. Clipboard Fallback: Copies formatted Markdown to clipboard when neither Bridge nor Pi is active.
|
|
4
10
|
*/
|
|
5
11
|
|
|
6
|
-
const
|
|
7
|
-
const
|
|
12
|
+
const DEFAULT_BRIDGE_PORT = 18090;
|
|
13
|
+
const DEFAULT_INTERNAL_PORT_START = 18091;
|
|
14
|
+
const INTERNAL_PORT_COUNT = 5;
|
|
15
|
+
const isChinese = (navigator.language || "").toLowerCase().startsWith("zh");
|
|
8
16
|
|
|
9
17
|
const i18n = {
|
|
10
18
|
menuSelection: isChinese ? "🚀 发送选中内容给 Pi" : "🚀 Send Selection to Pi",
|
|
@@ -12,30 +20,51 @@ const i18n = {
|
|
|
12
20
|
menuLink: isChinese ? "🚀 发送此链接给 Pi" : "🚀 Send Link to Pi",
|
|
13
21
|
notifyLaunchedTitle: isChinese ? "已唤起终端并预填内容" : "Terminal Launched & Prefilled",
|
|
14
22
|
notifyLaunchedMsg: (term) =>
|
|
15
|
-
isChinese
|
|
23
|
+
isChinese
|
|
24
|
+
? `已启动 ${term || "终端"},内容已在 Pi 输入框就绪!`
|
|
25
|
+
: `Started ${term || "terminal"}, prompt is ready in Pi!`,
|
|
16
26
|
notifyPrefilledTitle: isChinese ? "已填入 Pi 终端输入框" : "Prefilled into Pi Terminal",
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
notifyDirectTitle: isChinese ? "已直连填入 Pi 输入框" : "Prefilled Directly into Pi",
|
|
28
|
+
notifyPrefilledMsg: (target) =>
|
|
29
|
+
isChinese
|
|
30
|
+
? `内容已送达${target ? `(${target})` : ""},可自由补充要求或按 Enter 发送!`
|
|
31
|
+
: `Delivered${target ? ` (${target})` : ""}. Append instructions or press Enter!`,
|
|
32
|
+
notifyClipboardTitle: isChinese ? "已复制到剪贴板(兜底)" : "Copied to Clipboard (Fallback)",
|
|
33
|
+
notifyClipboardMsg: isChinese
|
|
34
|
+
? "未检测到运行中的 Pi 或 Bridge,已将 Markdown 复制到剪贴板,请切回终端粘贴(或运行 npx send-to-pi start 启用自动唤醒)。"
|
|
35
|
+
: "No active Pi or Bridge detected. Markdown copied to clipboard—paste into your terminal (or run 'npx send-to-pi start' for cold-start launch).",
|
|
36
|
+
notifyBridgeClipboardMsg: isChinese
|
|
37
|
+
? "未能自动拉起终端,已将格式化内容写入系统剪贴板,请直接粘贴到你的终端中。"
|
|
38
|
+
: "Could not launch a terminal automatically. Content copied to your clipboard—paste into Pi.",
|
|
20
39
|
notifyFailedTitle: isChinese ? "发送失败" : "Failed to Send",
|
|
21
40
|
notifyFailedMsg: (code) =>
|
|
22
41
|
isChinese ? `Bridge 响应状态码: ${code}` : `Bridge daemon responded with status: ${code}`,
|
|
23
|
-
notifyNoConnTitle: isChinese ? "
|
|
42
|
+
notifyNoConnTitle: isChinese ? "未检测到运行中的 Pi" : "No Active Pi Session Found",
|
|
24
43
|
notifyNoConnMsg: isChinese
|
|
25
|
-
? "
|
|
26
|
-
: "
|
|
44
|
+
? "请先在任意终端打开 pi,或启动守护进程 (npx send-to-pi start) 以支持自动唤醒。"
|
|
45
|
+
: "Open Pi in any terminal for zero-daemon direct send, or run 'npx send-to-pi start' for cold-start launch.",
|
|
27
46
|
notifyNoSelectionTitle: isChinese ? "未检测到选中文本" : "No Selection Detected",
|
|
28
47
|
notifyNoSelectionMsg: isChinese
|
|
29
48
|
? "请先在页面上划选需要发送的文本或代码。"
|
|
30
49
|
: "Please select text or code on the webpage first.",
|
|
31
50
|
};
|
|
32
51
|
|
|
33
|
-
|
|
52
|
+
function getSettings() {
|
|
34
53
|
return new Promise((resolve) => {
|
|
35
|
-
chrome.storage.sync.get(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
54
|
+
chrome.storage.sync.get(
|
|
55
|
+
{
|
|
56
|
+
bridgePort: DEFAULT_BRIDGE_PORT,
|
|
57
|
+
internalPortStart: DEFAULT_INTERNAL_PORT_START,
|
|
58
|
+
preferredPort: 0,
|
|
59
|
+
},
|
|
60
|
+
(items) => {
|
|
61
|
+
resolve({
|
|
62
|
+
bridgePort: Number(items.bridgePort) || DEFAULT_BRIDGE_PORT,
|
|
63
|
+
internalPortStart: Number(items.internalPortStart) || DEFAULT_INTERNAL_PORT_START,
|
|
64
|
+
preferredPort: Number(items.preferredPort) || 0,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
);
|
|
39
68
|
});
|
|
40
69
|
}
|
|
41
70
|
|
|
@@ -43,12 +72,138 @@ function showNotify(title, message) {
|
|
|
43
72
|
chrome.notifications.create({
|
|
44
73
|
type: "basic",
|
|
45
74
|
iconUrl: "icons/icon128.png",
|
|
46
|
-
title
|
|
47
|
-
message
|
|
75
|
+
title,
|
|
76
|
+
message,
|
|
48
77
|
priority: 1,
|
|
49
78
|
});
|
|
50
79
|
}
|
|
51
80
|
|
|
81
|
+
function formatTargetLabel(info) {
|
|
82
|
+
if (!info) return "";
|
|
83
|
+
const parts = [];
|
|
84
|
+
if (info.workdir) {
|
|
85
|
+
const clean = String(info.workdir).replace(/[\\/]+$/, "");
|
|
86
|
+
const base = clean.split(/[\\/]/).pop();
|
|
87
|
+
if (base) parts.push(base);
|
|
88
|
+
}
|
|
89
|
+
if (info.appName || info.terminal) {
|
|
90
|
+
parts.push(info.appName || info.terminal);
|
|
91
|
+
}
|
|
92
|
+
return parts.join(" · ");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function formatContent(payload) {
|
|
96
|
+
const { type, text, title, url, lang } = payload;
|
|
97
|
+
const isZh = lang === "zh";
|
|
98
|
+
|
|
99
|
+
if (type === "selection") {
|
|
100
|
+
const label = isZh ? "【来源】" : "[Source]";
|
|
101
|
+
return `${label}: ${title || (isZh ? "网页摘录" : "Web Snippet")} (${url || ""})\n\n${text}\n\n`;
|
|
102
|
+
}
|
|
103
|
+
if (type === "article") {
|
|
104
|
+
const titleLabel = isZh ? "【网页文章】" : "[Web Article]";
|
|
105
|
+
const linkLabel = isZh ? "【网页链接】" : "[Link]";
|
|
106
|
+
return `${titleLabel}: ${title || (isZh ? "正文" : "Article")}\n${linkLabel}: ${url || ""}\n\n${text}\n\n`;
|
|
107
|
+
}
|
|
108
|
+
if (type === "link") {
|
|
109
|
+
const label = isZh ? "【网页链接】" : "[Link]";
|
|
110
|
+
return `${label}: ${url}\n\n`;
|
|
111
|
+
}
|
|
112
|
+
const titleLabel = isZh ? "【网页标题】" : "[Title]";
|
|
113
|
+
const linkLabel = isZh ? "【网页链接】" : "[Link]";
|
|
114
|
+
return `${titleLabel}: ${title || (isZh ? "网页" : "Page")}\n${linkLabel}: ${url || ""}\n\n`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function fetchWithTimeout(url, options, timeoutMs) {
|
|
118
|
+
const controller = new AbortController();
|
|
119
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
120
|
+
try {
|
|
121
|
+
return await fetch(url, { ...options, signal: controller.signal });
|
|
122
|
+
} finally {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function probePiPort(port) {
|
|
128
|
+
try {
|
|
129
|
+
const res = await fetchWithTimeout(
|
|
130
|
+
`http://127.0.0.1:${port}/health`,
|
|
131
|
+
{ method: "GET" },
|
|
132
|
+
450
|
|
133
|
+
);
|
|
134
|
+
if (!res.ok) return null;
|
|
135
|
+
const info = await res.json();
|
|
136
|
+
if (info && info.status === "alive") {
|
|
137
|
+
return { ...info, port };
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
} catch (_err) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function discoverDirectPiSessions(startPort) {
|
|
146
|
+
const ports = Array.from({ length: INTERNAL_PORT_COUNT }, (_, idx) => startPort + idx);
|
|
147
|
+
const results = await Promise.all(ports.map((p) => probePiPort(p)));
|
|
148
|
+
return results
|
|
149
|
+
.filter(Boolean)
|
|
150
|
+
.sort(
|
|
151
|
+
(a, b) =>
|
|
152
|
+
Number(b.activeAt || b.updatedAt || 0) - Number(a.activeAt || a.updatedAt || 0)
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function sendDirectlyToPi(port, content) {
|
|
157
|
+
const res = await fetchWithTimeout(
|
|
158
|
+
`http://127.0.0.1:${port}/receive`,
|
|
159
|
+
{
|
|
160
|
+
method: "POST",
|
|
161
|
+
headers: { "Content-Type": "application/json" },
|
|
162
|
+
body: JSON.stringify({ content }),
|
|
163
|
+
},
|
|
164
|
+
1500
|
|
165
|
+
);
|
|
166
|
+
if (!res.ok) {
|
|
167
|
+
throw new Error(`Pi port ${port} responded with HTTP ${res.status}`);
|
|
168
|
+
}
|
|
169
|
+
return await res.json();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function tryCopyToClipboardInTab(tabId, text) {
|
|
173
|
+
if (!tabId) return false;
|
|
174
|
+
try {
|
|
175
|
+
const results = await chrome.scripting.executeScript({
|
|
176
|
+
target: { tabId },
|
|
177
|
+
func: async (str) => {
|
|
178
|
+
try {
|
|
179
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
180
|
+
await navigator.clipboard.writeText(str);
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
} catch (_e) {}
|
|
184
|
+
try {
|
|
185
|
+
const ta = document.createElement("textarea");
|
|
186
|
+
ta.value = str;
|
|
187
|
+
ta.setAttribute("readonly", "");
|
|
188
|
+
ta.style.position = "fixed";
|
|
189
|
+
ta.style.opacity = "0";
|
|
190
|
+
document.body.appendChild(ta);
|
|
191
|
+
ta.select();
|
|
192
|
+
const ok = document.execCommand("copy");
|
|
193
|
+
document.body.removeChild(ta);
|
|
194
|
+
return Boolean(ok);
|
|
195
|
+
} catch (_e) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
args: [text],
|
|
200
|
+
});
|
|
201
|
+
return Boolean(results && results[0] && results[0].result);
|
|
202
|
+
} catch (_err) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
52
207
|
// 注册右键菜单
|
|
53
208
|
chrome.runtime.onInstalled.addListener(() => {
|
|
54
209
|
chrome.contextMenus.removeAll(() => {
|
|
@@ -93,12 +248,10 @@ function inPageExtractor(mode) {
|
|
|
93
248
|
|
|
94
249
|
const tag = node.tagName.toLowerCase();
|
|
95
250
|
|
|
96
|
-
// 忽略不可视或干扰性标签
|
|
97
251
|
if (["script", "style", "noscript", "svg", "canvas", "template"].includes(tag)) {
|
|
98
252
|
return "";
|
|
99
253
|
}
|
|
100
254
|
|
|
101
|
-
// 1. 代码块 <pre>
|
|
102
255
|
if (tag === "pre") {
|
|
103
256
|
const codeElem = node.querySelector("code") || node;
|
|
104
257
|
const langClass = (codeElem.getAttribute("class") || "") + " " + (node.getAttribute("class") || "");
|
|
@@ -108,7 +261,6 @@ function inPageExtractor(mode) {
|
|
|
108
261
|
return `\n\n\`\`\`${lang}\n${codeText}\n\`\`\`\n\n`;
|
|
109
262
|
}
|
|
110
263
|
|
|
111
|
-
// 2. 行内代码 <code> (不在 pre 内)
|
|
112
264
|
if (tag === "code") {
|
|
113
265
|
if (node.closest("pre")) {
|
|
114
266
|
return node.textContent;
|
|
@@ -117,7 +269,6 @@ function inPageExtractor(mode) {
|
|
|
117
269
|
return text.includes("`") ? `\`\` ${text} \`\`` : `\`${text}\``;
|
|
118
270
|
}
|
|
119
271
|
|
|
120
|
-
// 3. 标题
|
|
121
272
|
if (/^h[1-6]$/.test(tag)) {
|
|
122
273
|
const level = parseInt(tag[1], 10);
|
|
123
274
|
const prefix = "#".repeat(level);
|
|
@@ -125,7 +276,6 @@ function inPageExtractor(mode) {
|
|
|
125
276
|
return `\n\n${prefix} ${text}\n\n`;
|
|
126
277
|
}
|
|
127
278
|
|
|
128
|
-
// 4. 引用
|
|
129
279
|
if (tag === "blockquote") {
|
|
130
280
|
const content = getChildrenText(node, depth).trim();
|
|
131
281
|
const quoted = content
|
|
@@ -135,7 +285,6 @@ function inPageExtractor(mode) {
|
|
|
135
285
|
return `\n\n${quoted}\n\n`;
|
|
136
286
|
}
|
|
137
287
|
|
|
138
|
-
// 5. 链接
|
|
139
288
|
if (tag === "a") {
|
|
140
289
|
const text = getChildrenText(node, depth).trim();
|
|
141
290
|
const href = node.getAttribute("href");
|
|
@@ -146,7 +295,6 @@ function inPageExtractor(mode) {
|
|
|
146
295
|
return text;
|
|
147
296
|
}
|
|
148
297
|
|
|
149
|
-
// 6. 强调
|
|
150
298
|
if (tag === "strong" || tag === "b") {
|
|
151
299
|
const text = getChildrenText(node, depth).trim();
|
|
152
300
|
return text ? `**${text}**` : "";
|
|
@@ -160,29 +308,19 @@ function inPageExtractor(mode) {
|
|
|
160
308
|
return text ? `~~${text}~~` : "";
|
|
161
309
|
}
|
|
162
310
|
|
|
163
|
-
|
|
164
|
-
if (tag === "
|
|
165
|
-
return "\n\n---\n\n";
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 8. 换行
|
|
169
|
-
if (tag === "br") {
|
|
170
|
-
return "\n";
|
|
171
|
-
}
|
|
311
|
+
if (tag === "hr") return "\n\n---\n\n";
|
|
312
|
+
if (tag === "br") return "\n";
|
|
172
313
|
|
|
173
|
-
// 9. 图片
|
|
174
314
|
if (tag === "img") {
|
|
175
315
|
const alt = node.getAttribute("alt") || "";
|
|
176
316
|
const src = node.getAttribute("src") || "";
|
|
177
317
|
return src ? `` : "";
|
|
178
318
|
}
|
|
179
319
|
|
|
180
|
-
// 10. 表格
|
|
181
320
|
if (tag === "table") {
|
|
182
321
|
return renderTable(node);
|
|
183
322
|
}
|
|
184
323
|
|
|
185
|
-
// 11. 列表
|
|
186
324
|
if (tag === "ul" || tag === "ol") {
|
|
187
325
|
const isOrdered = tag === "ol";
|
|
188
326
|
let idx = 1;
|
|
@@ -198,7 +336,6 @@ function inPageExtractor(mode) {
|
|
|
198
336
|
return `\n\n${items.join("\n")}\n\n`;
|
|
199
337
|
}
|
|
200
338
|
|
|
201
|
-
// 12. 常用块级元素
|
|
202
339
|
if (["p", "div", "section", "article"].includes(tag)) {
|
|
203
340
|
const text = getChildrenText(node, depth).trim();
|
|
204
341
|
return text ? `\n\n${text}\n\n` : "";
|
|
@@ -279,7 +416,6 @@ function inPageExtractor(mode) {
|
|
|
279
416
|
}
|
|
280
417
|
|
|
281
418
|
if (mode === "article") {
|
|
282
|
-
// X/Twitter 页面不能读取整个 body:只尝试主 tweet 或文章节点。
|
|
283
419
|
const isXTwitter = /(^|\.)((twitter|x)\.com)$/i.test(window.location.hostname);
|
|
284
420
|
if (isXTwitter) {
|
|
285
421
|
const xSelectors = [
|
|
@@ -289,8 +425,8 @@ function inPageExtractor(mode) {
|
|
|
289
425
|
'[data-testid="twitter-article"]',
|
|
290
426
|
'[data-testid="article"]',
|
|
291
427
|
'article[data-testid="tweet"]',
|
|
292
|
-
|
|
293
|
-
|
|
428
|
+
"main article",
|
|
429
|
+
"article",
|
|
294
430
|
];
|
|
295
431
|
let mainTweet = null;
|
|
296
432
|
for (const selector of xSelectors) {
|
|
@@ -298,16 +434,13 @@ function inPageExtractor(mode) {
|
|
|
298
434
|
if (mainTweet) break;
|
|
299
435
|
}
|
|
300
436
|
|
|
301
|
-
// 某些 X 版本只标记 tweetText,向上定位其所属主 article。
|
|
302
437
|
if (!mainTweet) {
|
|
303
438
|
const tweetText = document.querySelector('[data-testid="tweetText"]');
|
|
304
|
-
mainTweet = tweetText ? tweetText.closest(
|
|
439
|
+
mainTweet = tweetText ? tweetText.closest("article") : null;
|
|
305
440
|
}
|
|
306
441
|
|
|
307
442
|
if (!mainTweet) return null;
|
|
308
443
|
|
|
309
|
-
// X 的主 article 同时包含作者栏、头像、媒体和互动按钮。
|
|
310
|
-
// 只保留正文结构,避免把整个帖子壳层发送给 Pi。
|
|
311
444
|
const cleanTweet = mainTweet.cloneNode(true);
|
|
312
445
|
[
|
|
313
446
|
'[data-testid="User-Name"]',
|
|
@@ -316,8 +449,8 @@ function inPageExtractor(mode) {
|
|
|
316
449
|
'[data-testid="tweetPhoto"]',
|
|
317
450
|
'[data-testid="videoPlayer"]',
|
|
318
451
|
'[role="group"]',
|
|
319
|
-
|
|
320
|
-
|
|
452
|
+
"time",
|
|
453
|
+
"img",
|
|
321
454
|
].forEach((selector) => {
|
|
322
455
|
cleanTweet.querySelectorAll(selector).forEach((el) => el.remove());
|
|
323
456
|
});
|
|
@@ -326,9 +459,7 @@ function inPageExtractor(mode) {
|
|
|
326
459
|
return xMarkdown || null;
|
|
327
460
|
}
|
|
328
461
|
|
|
329
|
-
// Readability 简版:整页正文提取
|
|
330
462
|
const clone = document.body.cloneNode(true);
|
|
331
|
-
|
|
332
463
|
const removeSelectors = [
|
|
333
464
|
"script", "style", "noscript", "nav", "header", "footer", "aside",
|
|
334
465
|
"iframe", "svg", "form", "[role='navigation']", "[role='banner']",
|
|
@@ -365,8 +496,7 @@ function inPageExtractor(mode) {
|
|
|
365
496
|
mainContainer = bestElem;
|
|
366
497
|
}
|
|
367
498
|
|
|
368
|
-
|
|
369
|
-
return md;
|
|
499
|
+
return domToMarkdown(mainContainer);
|
|
370
500
|
}
|
|
371
501
|
|
|
372
502
|
return null;
|
|
@@ -389,28 +519,71 @@ async function extractFromTab(tabId, mode) {
|
|
|
389
519
|
}
|
|
390
520
|
}
|
|
391
521
|
|
|
392
|
-
async function sendPayload(payload) {
|
|
393
|
-
const
|
|
522
|
+
async function sendPayload(payload, tabId = null) {
|
|
523
|
+
const { bridgePort, internalPortStart, preferredPort } = await getSettings();
|
|
524
|
+
const formattedContent = formatContent(payload);
|
|
525
|
+
|
|
526
|
+
// 0. If user pinned a specific Pi session port in the popup, try it first.
|
|
527
|
+
if (preferredPort > 0) {
|
|
528
|
+
try {
|
|
529
|
+
const res = await sendDirectlyToPi(preferredPort, formattedContent);
|
|
530
|
+
showNotify(i18n.notifyDirectTitle, i18n.notifyPrefilledMsg(formatTargetLabel(res)));
|
|
531
|
+
return;
|
|
532
|
+
} catch (_err) {
|
|
533
|
+
// Pinned session closed — fall through to normal routing.
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// 1. Primary path: local Bridge Daemon (handles hot delivery, cold-start launch, and focus).
|
|
394
538
|
try {
|
|
395
|
-
const res = await
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
539
|
+
const res = await fetchWithTimeout(
|
|
540
|
+
`http://127.0.0.1:${bridgePort}/send`,
|
|
541
|
+
{
|
|
542
|
+
method: "POST",
|
|
543
|
+
headers: { "Content-Type": "application/json" },
|
|
544
|
+
body: JSON.stringify(payload),
|
|
399
545
|
},
|
|
400
|
-
|
|
401
|
-
|
|
546
|
+
2500
|
|
547
|
+
);
|
|
402
548
|
|
|
403
549
|
if (res.ok) {
|
|
404
550
|
const data = await res.json();
|
|
405
551
|
if (data.mode === "launched") {
|
|
406
552
|
showNotify(i18n.notifyLaunchedTitle, i18n.notifyLaunchedMsg(data.terminal));
|
|
553
|
+
} else if (data.mode === "clipboard") {
|
|
554
|
+
showNotify(i18n.notifyClipboardTitle, i18n.notifyBridgeClipboardMsg);
|
|
407
555
|
} else {
|
|
408
|
-
showNotify(i18n.notifyPrefilledTitle, i18n.notifyPrefilledMsg);
|
|
556
|
+
showNotify(i18n.notifyPrefilledTitle, i18n.notifyPrefilledMsg(formatTargetLabel(data)));
|
|
409
557
|
}
|
|
558
|
+
return;
|
|
410
559
|
} else {
|
|
411
560
|
showNotify(i18n.notifyFailedTitle, i18n.notifyFailedMsg(res.status));
|
|
561
|
+
return;
|
|
412
562
|
}
|
|
413
|
-
} catch (
|
|
563
|
+
} catch (_bridgeErr) {
|
|
564
|
+
// Bridge daemon is not running; fall through to zero-daemon direct Pi discovery.
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// 2. Zero-Daemon Direct Fallback: probe active Pi instances directly (terminal-agnostic).
|
|
568
|
+
const directSessions = await discoverDirectPiSessions(internalPortStart);
|
|
569
|
+
for (const session of directSessions) {
|
|
570
|
+
try {
|
|
571
|
+
const res = await sendDirectlyToPi(session.port, formattedContent);
|
|
572
|
+
showNotify(
|
|
573
|
+
i18n.notifyDirectTitle,
|
|
574
|
+
i18n.notifyPrefilledMsg(formatTargetLabel({ ...session, ...res }))
|
|
575
|
+
);
|
|
576
|
+
return;
|
|
577
|
+
} catch (_err) {
|
|
578
|
+
// Try next healthy session if one disappeared mid-request.
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// 3. Final fallback: copy formatted Markdown to clipboard so content is never lost.
|
|
583
|
+
const copied = await tryCopyToClipboardInTab(tabId, formattedContent);
|
|
584
|
+
if (copied) {
|
|
585
|
+
showNotify(i18n.notifyClipboardTitle, i18n.notifyClipboardMsg);
|
|
586
|
+
} else {
|
|
414
587
|
showNotify(i18n.notifyNoConnTitle, i18n.notifyNoConnMsg);
|
|
415
588
|
}
|
|
416
589
|
}
|
|
@@ -424,7 +597,6 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
424
597
|
if (tabId) {
|
|
425
598
|
markdown = await extractFromTab(tabId, "selection");
|
|
426
599
|
}
|
|
427
|
-
// 平滑降级:若 HTML 提取失败则回退到 selectionText
|
|
428
600
|
const textToSend = markdown || info.selectionText || "";
|
|
429
601
|
|
|
430
602
|
const payload = {
|
|
@@ -434,7 +606,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
434
606
|
url: tab ? tab.url : "",
|
|
435
607
|
lang: isChinese ? "zh" : "en",
|
|
436
608
|
};
|
|
437
|
-
await sendPayload(payload);
|
|
609
|
+
await sendPayload(payload, tabId);
|
|
438
610
|
return;
|
|
439
611
|
}
|
|
440
612
|
|
|
@@ -451,7 +623,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
451
623
|
url: tab ? tab.url : "",
|
|
452
624
|
lang: isChinese ? "zh" : "en",
|
|
453
625
|
};
|
|
454
|
-
await sendPayload(payload);
|
|
626
|
+
await sendPayload(payload, tabId);
|
|
455
627
|
return;
|
|
456
628
|
}
|
|
457
629
|
|
|
@@ -463,7 +635,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
463
635
|
url: info.linkUrl || (tab ? tab.url : ""),
|
|
464
636
|
lang: isChinese ? "zh" : "en",
|
|
465
637
|
};
|
|
466
|
-
await sendPayload(payload);
|
|
638
|
+
await sendPayload(payload, tabId);
|
|
467
639
|
}
|
|
468
640
|
});
|
|
469
641
|
|
|
@@ -488,6 +660,6 @@ chrome.commands.onCommand.addListener(async (command) => {
|
|
|
488
660
|
lang: isChinese ? "zh" : "en",
|
|
489
661
|
};
|
|
490
662
|
|
|
491
|
-
await sendPayload(payload);
|
|
663
|
+
await sendPayload(payload, tab.id);
|
|
492
664
|
}
|
|
493
665
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"manifest_version": 3,
|
|
3
3
|
"name": "Send to Pi (发送给 Pi)",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "Send webpage snippets, URLs, or articles directly to your local Pi coding agent terminal.",
|
|
6
6
|
"permissions": [
|
|
7
7
|
"contextMenus",
|
|
@@ -22,6 +22,15 @@
|
|
|
22
22
|
"host_permissions": [
|
|
23
23
|
"http://127.0.0.1:*/*"
|
|
24
24
|
],
|
|
25
|
+
"action": {
|
|
26
|
+
"default_title": "Send to Pi",
|
|
27
|
+
"default_popup": "popup.html",
|
|
28
|
+
"default_icon": {
|
|
29
|
+
"16": "icons/icon16.png",
|
|
30
|
+
"48": "icons/icon48.png",
|
|
31
|
+
"128": "icons/icon128.png"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
25
34
|
"background": {
|
|
26
35
|
"service_worker": "background.js"
|
|
27
36
|
},
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
body {
|
|
8
8
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
9
9
|
padding: 16px 20px;
|
|
10
|
-
width:
|
|
10
|
+
width: 340px;
|
|
11
11
|
color: #333;
|
|
12
12
|
margin: 0;
|
|
13
13
|
}
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
font-size: 12px;
|
|
41
41
|
color: #666;
|
|
42
42
|
margin-top: 4px;
|
|
43
|
+
line-height: 1.4;
|
|
43
44
|
}
|
|
44
45
|
button {
|
|
45
46
|
background: #2563eb;
|
|
@@ -69,7 +70,12 @@
|
|
|
69
70
|
<div class="field">
|
|
70
71
|
<label for="bridgePort">Bridge Daemon Port</label>
|
|
71
72
|
<input type="number" id="bridgePort" min="1024" max="65535" value="18090" />
|
|
72
|
-
<div class="hint">Default
|
|
73
|
+
<div class="hint">Default: 18090. Optional—if Bridge is not running, the extension automatically connects directly to active Pi sessions.</div>
|
|
74
|
+
</div>
|
|
75
|
+
<div class="field">
|
|
76
|
+
<label for="internalPortStart">Pi Direct Port Start (probes 5 ports)</label>
|
|
77
|
+
<input type="number" id="internalPortStart" min="1024" max="65530" value="18091" />
|
|
78
|
+
<div class="hint">Default: 18091 (scans 18091~18095). Matches PI_INTERNAL_PORT in Pi.</div>
|
|
73
79
|
</div>
|
|
74
80
|
<button id="save">Save Settings</button>
|
|
75
81
|
<div id="status"></div>
|
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
const
|
|
1
|
+
const DEFAULT_BRIDGE_PORT = 18090;
|
|
2
|
+
const DEFAULT_INTERNAL_PORT_START = 18091;
|
|
2
3
|
|
|
3
4
|
function restoreOptions() {
|
|
4
|
-
chrome.storage.sync.get(
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
chrome.storage.sync.get(
|
|
6
|
+
{
|
|
7
|
+
bridgePort: DEFAULT_BRIDGE_PORT,
|
|
8
|
+
internalPortStart: DEFAULT_INTERNAL_PORT_START,
|
|
9
|
+
},
|
|
10
|
+
(items) => {
|
|
11
|
+
document.getElementById("bridgePort").value = items.bridgePort;
|
|
12
|
+
document.getElementById("internalPortStart").value = items.internalPortStart;
|
|
13
|
+
}
|
|
14
|
+
);
|
|
7
15
|
}
|
|
8
16
|
|
|
9
17
|
function saveOptions() {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
const bridgePort = parseInt(document.getElementById("bridgePort").value, 10) || DEFAULT_BRIDGE_PORT;
|
|
19
|
+
const internalPortStart =
|
|
20
|
+
parseInt(document.getElementById("internalPortStart").value, 10) || DEFAULT_INTERNAL_PORT_START;
|
|
21
|
+
|
|
22
|
+
chrome.storage.sync.set({ bridgePort, internalPortStart }, () => {
|
|
23
|
+
const status = document.getElementById("status");
|
|
24
|
+
status.textContent = "✅ Settings saved!";
|
|
14
25
|
setTimeout(() => {
|
|
15
|
-
status.textContent =
|
|
26
|
+
status.textContent = "";
|
|
16
27
|
}, 2000);
|
|
17
28
|
});
|
|
18
29
|
}
|
|
19
30
|
|
|
20
|
-
document.addEventListener(
|
|
21
|
-
document.getElementById(
|
|
31
|
+
document.addEventListener("DOMContentLoaded", restoreOptions);
|
|
32
|
+
document.getElementById("save").addEventListener("click", saveOptions);
|