send-to-pi 1.1.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 -105
- package/README_CN.md +57 -108
- package/chrome-extension/background.js +275 -56
- 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 -16
- package/pi-extension/index.ts +334 -37
- package/server/bin/send-to-pi.js +97 -42
- package/server/src/clipboard.js +53 -0
- package/server/src/config.js +107 -10
- package/server/src/index.js +190 -46
- 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 +110 -43
|
@@ -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
|
-
}
|
|
311
|
+
if (tag === "hr") return "\n\n---\n\n";
|
|
312
|
+
if (tag === "br") return "\n";
|
|
167
313
|
|
|
168
|
-
// 8. 换行
|
|
169
|
-
if (tag === "br") {
|
|
170
|
-
return "\n";
|
|
171
|
-
}
|
|
172
|
-
|
|
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,9 +416,50 @@ function inPageExtractor(mode) {
|
|
|
279
416
|
}
|
|
280
417
|
|
|
281
418
|
if (mode === "article") {
|
|
282
|
-
|
|
283
|
-
|
|
419
|
+
const isXTwitter = /(^|\.)((twitter|x)\.com)$/i.test(window.location.hostname);
|
|
420
|
+
if (isXTwitter) {
|
|
421
|
+
const xSelectors = [
|
|
422
|
+
'main [data-testid="twitter-article"]',
|
|
423
|
+
'main [data-testid="article"]',
|
|
424
|
+
'main article[data-testid="tweet"]',
|
|
425
|
+
'[data-testid="twitter-article"]',
|
|
426
|
+
'[data-testid="article"]',
|
|
427
|
+
'article[data-testid="tweet"]',
|
|
428
|
+
"main article",
|
|
429
|
+
"article",
|
|
430
|
+
];
|
|
431
|
+
let mainTweet = null;
|
|
432
|
+
for (const selector of xSelectors) {
|
|
433
|
+
mainTweet = document.querySelector(selector);
|
|
434
|
+
if (mainTweet) break;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (!mainTweet) {
|
|
438
|
+
const tweetText = document.querySelector('[data-testid="tweetText"]');
|
|
439
|
+
mainTweet = tweetText ? tweetText.closest("article") : null;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (!mainTweet) return null;
|
|
443
|
+
|
|
444
|
+
const cleanTweet = mainTweet.cloneNode(true);
|
|
445
|
+
[
|
|
446
|
+
'[data-testid="User-Name"]',
|
|
447
|
+
'[data-testid="User-Names"]',
|
|
448
|
+
'[data-testid="UserDescription"]',
|
|
449
|
+
'[data-testid="tweetPhoto"]',
|
|
450
|
+
'[data-testid="videoPlayer"]',
|
|
451
|
+
'[role="group"]',
|
|
452
|
+
"time",
|
|
453
|
+
"img",
|
|
454
|
+
].forEach((selector) => {
|
|
455
|
+
cleanTweet.querySelectorAll(selector).forEach((el) => el.remove());
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
const xMarkdown = domToMarkdown(cleanTweet);
|
|
459
|
+
return xMarkdown || null;
|
|
460
|
+
}
|
|
284
461
|
|
|
462
|
+
const clone = document.body.cloneNode(true);
|
|
285
463
|
const removeSelectors = [
|
|
286
464
|
"script", "style", "noscript", "nav", "header", "footer", "aside",
|
|
287
465
|
"iframe", "svg", "form", "[role='navigation']", "[role='banner']",
|
|
@@ -318,8 +496,7 @@ function inPageExtractor(mode) {
|
|
|
318
496
|
mainContainer = bestElem;
|
|
319
497
|
}
|
|
320
498
|
|
|
321
|
-
|
|
322
|
-
return md;
|
|
499
|
+
return domToMarkdown(mainContainer);
|
|
323
500
|
}
|
|
324
501
|
|
|
325
502
|
return null;
|
|
@@ -342,28 +519,71 @@ async function extractFromTab(tabId, mode) {
|
|
|
342
519
|
}
|
|
343
520
|
}
|
|
344
521
|
|
|
345
|
-
async function sendPayload(payload) {
|
|
346
|
-
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).
|
|
347
538
|
try {
|
|
348
|
-
const res = await
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
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),
|
|
352
545
|
},
|
|
353
|
-
|
|
354
|
-
|
|
546
|
+
2500
|
|
547
|
+
);
|
|
355
548
|
|
|
356
549
|
if (res.ok) {
|
|
357
550
|
const data = await res.json();
|
|
358
551
|
if (data.mode === "launched") {
|
|
359
552
|
showNotify(i18n.notifyLaunchedTitle, i18n.notifyLaunchedMsg(data.terminal));
|
|
553
|
+
} else if (data.mode === "clipboard") {
|
|
554
|
+
showNotify(i18n.notifyClipboardTitle, i18n.notifyBridgeClipboardMsg);
|
|
360
555
|
} else {
|
|
361
|
-
showNotify(i18n.notifyPrefilledTitle, i18n.notifyPrefilledMsg);
|
|
556
|
+
showNotify(i18n.notifyPrefilledTitle, i18n.notifyPrefilledMsg(formatTargetLabel(data)));
|
|
362
557
|
}
|
|
558
|
+
return;
|
|
363
559
|
} else {
|
|
364
560
|
showNotify(i18n.notifyFailedTitle, i18n.notifyFailedMsg(res.status));
|
|
561
|
+
return;
|
|
365
562
|
}
|
|
366
|
-
} 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 {
|
|
367
587
|
showNotify(i18n.notifyNoConnTitle, i18n.notifyNoConnMsg);
|
|
368
588
|
}
|
|
369
589
|
}
|
|
@@ -377,7 +597,6 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
377
597
|
if (tabId) {
|
|
378
598
|
markdown = await extractFromTab(tabId, "selection");
|
|
379
599
|
}
|
|
380
|
-
// 平滑降级:若 HTML 提取失败则回退到 selectionText
|
|
381
600
|
const textToSend = markdown || info.selectionText || "";
|
|
382
601
|
|
|
383
602
|
const payload = {
|
|
@@ -387,7 +606,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
387
606
|
url: tab ? tab.url : "",
|
|
388
607
|
lang: isChinese ? "zh" : "en",
|
|
389
608
|
};
|
|
390
|
-
await sendPayload(payload);
|
|
609
|
+
await sendPayload(payload, tabId);
|
|
391
610
|
return;
|
|
392
611
|
}
|
|
393
612
|
|
|
@@ -404,7 +623,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
404
623
|
url: tab ? tab.url : "",
|
|
405
624
|
lang: isChinese ? "zh" : "en",
|
|
406
625
|
};
|
|
407
|
-
await sendPayload(payload);
|
|
626
|
+
await sendPayload(payload, tabId);
|
|
408
627
|
return;
|
|
409
628
|
}
|
|
410
629
|
|
|
@@ -416,7 +635,7 @@ chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
416
635
|
url: info.linkUrl || (tab ? tab.url : ""),
|
|
417
636
|
lang: isChinese ? "zh" : "en",
|
|
418
637
|
};
|
|
419
|
-
await sendPayload(payload);
|
|
638
|
+
await sendPayload(payload, tabId);
|
|
420
639
|
}
|
|
421
640
|
});
|
|
422
641
|
|
|
@@ -441,6 +660,6 @@ chrome.commands.onCommand.addListener(async (command) => {
|
|
|
441
660
|
lang: isChinese ? "zh" : "en",
|
|
442
661
|
};
|
|
443
662
|
|
|
444
|
-
await sendPayload(payload);
|
|
663
|
+
await sendPayload(payload, tab.id);
|
|
445
664
|
}
|
|
446
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);
|