@opentiny/webmcp-cli 0.0.6 → 0.0.8
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 +17 -0
- package/dist/bin.cjs +139 -219
- package/dist/bin.cjs.map +1 -1
- package/dist/bin.js +139 -219
- package/dist/bin.js.map +1 -1
- package/dist/index.cjs +101 -197
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +101 -197
- package/dist/index.js.map +1 -1
- package/dist/inject-bundle.js +1477 -936
- package/dist/webmcp-tools/editor.csdn.net.js +199 -9
- package/dist/webmcp-tools/juejin.cn.js +24454 -8
- package/dist/webmcp-tools/my.oschina.net.js +175 -33
- package/dist/webmcp-tools/segmentfault.com.js +157 -1
- package/dist/webmcp-tools/zhuanlan.zhihu.com.js +2261 -6
- package/package.json +8 -4
package/dist/bin.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// src/bin.ts
|
|
4
4
|
import fs2 from "fs";
|
|
5
5
|
import path2 from "path";
|
|
6
|
-
import
|
|
6
|
+
import os2 from "os";
|
|
7
7
|
import { Command } from "commander";
|
|
8
8
|
import pc2 from "picocolors";
|
|
9
9
|
import { readFileSync as readFileSync2 } from "fs";
|
|
@@ -199,11 +199,37 @@ import { spawn } from "child_process";
|
|
|
199
199
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
200
200
|
var __filename = fileURLToPath2(import.meta.url);
|
|
201
201
|
var __dirname = path.dirname(__filename);
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
function getCdpPort() {
|
|
203
|
+
const raw = process.env.WEBMCP_CDP_PORT;
|
|
204
|
+
if (raw) {
|
|
205
|
+
const port = Number.parseInt(raw, 10);
|
|
206
|
+
if (Number.isFinite(port) && port > 0) return port;
|
|
207
|
+
}
|
|
208
|
+
return 9222;
|
|
209
|
+
}
|
|
204
210
|
function getWorkspaceDir() {
|
|
205
211
|
return process.env.WEBMCP_WORKSPACE || path.join(os.homedir(), ".webmcp_chrome_profile");
|
|
206
212
|
}
|
|
213
|
+
function shouldLaunchHeadless() {
|
|
214
|
+
if (process.env.WEBMCP_HEADLESS === "0") return false;
|
|
215
|
+
return process.env.WEBMCP_HEADLESS === "1" || process.env.CI === "true" || process.env.CI === "1";
|
|
216
|
+
}
|
|
217
|
+
function readActivePortFromProfile(userDataDir) {
|
|
218
|
+
try {
|
|
219
|
+
const content = fs.readFileSync(path.join(userDataDir, "DevToolsActivePort"), "utf-8");
|
|
220
|
+
const port = parseInt(content.split("\n")[0].trim(), 10);
|
|
221
|
+
return Number.isFinite(port) && port > 0 ? port : null;
|
|
222
|
+
} catch {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function candidateCdpBaseUrls(port) {
|
|
227
|
+
return [
|
|
228
|
+
`http://127.0.0.1:${port}`,
|
|
229
|
+
`http://[::1]:${port}`,
|
|
230
|
+
`http://localhost:${port}`
|
|
231
|
+
];
|
|
232
|
+
}
|
|
207
233
|
function getLastTabIdFilePath() {
|
|
208
234
|
return path.join(getWorkspaceDir(), ".last-tab-id");
|
|
209
235
|
}
|
|
@@ -297,6 +323,13 @@ async function connectPuppeteer(browserBaseUrl, attempt = 1, maxAttempts = 3) {
|
|
|
297
323
|
}
|
|
298
324
|
}
|
|
299
325
|
function getDefaultBrowserPath() {
|
|
326
|
+
const envPath = process.env.CHROME_PATH || process.env.WEBMCP_CHROME_PATH || process.env.PUPPETEER_EXECUTABLE_PATH;
|
|
327
|
+
if (envPath && fs.existsSync(envPath)) {
|
|
328
|
+
return { path: envPath, name: "Chrome" };
|
|
329
|
+
}
|
|
330
|
+
if (envPath) {
|
|
331
|
+
console.log(pc.yellow(`\u6307\u5B9A\u7684\u6D4F\u89C8\u5668\u8DEF\u5F84\u4E0D\u5B58\u5728\uFF0C\u5C06\u56DE\u9000\u5230\u81EA\u52A8\u68C0\u6D4B: ${envPath}`));
|
|
332
|
+
}
|
|
300
333
|
const platform = os.platform();
|
|
301
334
|
if (platform === "darwin") {
|
|
302
335
|
const chromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
|
|
@@ -340,16 +373,30 @@ async function startBrowserInBackground() {
|
|
|
340
373
|
if (!browserInfo || !fs.existsSync(browserInfo.path)) {
|
|
341
374
|
throw new Error("\u65E0\u6CD5\u5728\u7CFB\u7EDF\u4E2D\u627E\u5230 Chrome \u6216 Edge \u6D4F\u89C8\u5668\u7684\u9ED8\u8BA4\u5B89\u88C5\u8DEF\u5F84\u3002");
|
|
342
375
|
}
|
|
343
|
-
|
|
344
|
-
|
|
376
|
+
const cdpPort = getCdpPort();
|
|
377
|
+
console.log(pc.yellow(`\u6B63\u5728\u542F\u52A8\u540E\u53F0 ${browserInfo.name} \u5B9E\u4F8B (\u7AEF\u53E3: ${cdpPort})...`));
|
|
378
|
+
const userDataDir = getWorkspaceDir();
|
|
379
|
+
if (!fs.existsSync(userDataDir)) {
|
|
380
|
+
fs.mkdirSync(userDataDir, { recursive: true });
|
|
381
|
+
}
|
|
382
|
+
try {
|
|
383
|
+
fs.unlinkSync(path.join(userDataDir, "DevToolsActivePort"));
|
|
384
|
+
} catch {
|
|
385
|
+
}
|
|
345
386
|
const launchArgs = [
|
|
346
|
-
`--remote-debugging-port=${
|
|
387
|
+
`--remote-debugging-port=${cdpPort}`,
|
|
347
388
|
`--user-data-dir=${userDataDir}`,
|
|
348
389
|
"--no-first-run",
|
|
349
390
|
"--no-default-browser-check"
|
|
350
391
|
];
|
|
351
|
-
if (
|
|
352
|
-
launchArgs.push(
|
|
392
|
+
if (shouldLaunchHeadless()) {
|
|
393
|
+
launchArgs.push(
|
|
394
|
+
"--headless=new",
|
|
395
|
+
"--no-sandbox",
|
|
396
|
+
"--disable-setuid-sandbox",
|
|
397
|
+
"--disable-gpu",
|
|
398
|
+
"--disable-dev-shm-usage"
|
|
399
|
+
);
|
|
353
400
|
}
|
|
354
401
|
const child = spawn(
|
|
355
402
|
browserInfo.path,
|
|
@@ -360,15 +407,22 @@ async function startBrowserInBackground() {
|
|
|
360
407
|
}
|
|
361
408
|
);
|
|
362
409
|
child.unref();
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
410
|
+
for (let i = 0; i < 40; i++) {
|
|
411
|
+
const ports = /* @__PURE__ */ new Set([cdpPort]);
|
|
412
|
+
const activePort = readActivePortFromProfile(userDataDir);
|
|
413
|
+
if (activePort) ports.add(activePort);
|
|
414
|
+
for (const port of ports) {
|
|
415
|
+
for (const baseUrl of candidateCdpBaseUrls(port)) {
|
|
416
|
+
try {
|
|
417
|
+
await httpGet(`${baseUrl}/json/version`, 1e3);
|
|
418
|
+
if (port !== cdpPort) {
|
|
419
|
+
console.log(pc.yellow(`\u68C0\u6D4B\u5230\u7AEF\u53E3 ${cdpPort} \u5DF2\u88AB\u5176\u4ED6\u7A0B\u5E8F\u5360\u7528\uFF0C${browserInfo.name} \u5DF2\u81EA\u52A8\u5207\u6362\u5230\u7AEF\u53E3 ${port}\u3002`));
|
|
420
|
+
}
|
|
421
|
+
console.log(pc.green(`${browserInfo.name} \u542F\u52A8\u5E76\u5C31\u7EEA\uFF08${baseUrl}\uFF09\u3002`));
|
|
422
|
+
await new Promise((resolve3) => setTimeout(resolve3, 500));
|
|
423
|
+
return baseUrl;
|
|
424
|
+
} catch {
|
|
425
|
+
}
|
|
372
426
|
}
|
|
373
427
|
}
|
|
374
428
|
await new Promise((resolve3) => setTimeout(resolve3, 500));
|
|
@@ -376,13 +430,16 @@ async function startBrowserInBackground() {
|
|
|
376
430
|
throw new Error(`${browserInfo.name} \u542F\u52A8\u8D85\u65F6\uFF0C\u65E0\u6CD5\u8FDE\u63A5\u5230 CDP \u7AEF\u53E3\u3002`);
|
|
377
431
|
}
|
|
378
432
|
async function connectBrowser() {
|
|
433
|
+
const userDataDir = getWorkspaceDir();
|
|
434
|
+
const cdpPort = getCdpPort();
|
|
435
|
+
const recordedPort = readActivePortFromProfile(userDataDir);
|
|
379
436
|
const versionAddresses = [
|
|
380
|
-
|
|
381
|
-
|
|
437
|
+
...recordedPort && recordedPort !== cdpPort ? candidateCdpBaseUrls(recordedPort).map((u) => `${u}/json/version`) : [],
|
|
438
|
+
...candidateCdpBaseUrls(cdpPort).map((u) => `${u}/json/version`)
|
|
382
439
|
];
|
|
383
440
|
const existingUrl = await findAvailableCdpUrl(versionAddresses, 2);
|
|
384
441
|
if (existingUrl) {
|
|
385
|
-
console.log(pc.yellow(`connectBrowser: \u68C0\u6D4B\u5230\u7AEF\u53E3 ${
|
|
442
|
+
console.log(pc.yellow(`connectBrowser: \u68C0\u6D4B\u5230\u7AEF\u53E3 ${cdpPort} \u5DF2\u5C31\u7EEA\uFF0C\u6B63\u5728\u8FDE\u63A5 ${existingUrl}...`));
|
|
386
443
|
try {
|
|
387
444
|
const browser = await connectPuppeteer(existingUrl);
|
|
388
445
|
console.log(pc.green(`connectBrowser: \u6210\u529F\u8FDE\u63A5 ${existingUrl}`));
|
|
@@ -394,25 +451,22 @@ async function connectBrowser() {
|
|
|
394
451
|
);
|
|
395
452
|
}
|
|
396
453
|
}
|
|
397
|
-
console.log(pc.yellow(`connectBrowser: \u7AEF\u53E3 ${
|
|
454
|
+
console.log(pc.yellow(`connectBrowser: \u7AEF\u53E3 ${cdpPort} \u65E0\u54CD\u5E94\uFF0C\u6B63\u5728\u542F\u52A8\u65B0\u7684\u6D4F\u89C8\u5668\u5B9E\u4F8B...`));
|
|
455
|
+
let launchedUrl;
|
|
398
456
|
try {
|
|
399
|
-
await startBrowserInBackground();
|
|
457
|
+
launchedUrl = await startBrowserInBackground();
|
|
400
458
|
} catch (launchError) {
|
|
401
459
|
const msg = launchError instanceof Error ? launchError.message : String(launchError);
|
|
402
460
|
console.error(pc.red(`\u65E0\u6CD5\u542F\u52A8\u6D4F\u89C8\u5668: ${msg}`));
|
|
403
461
|
throw new Error("Browser launch failed.");
|
|
404
462
|
}
|
|
405
|
-
const launchedUrl = await findAvailableCdpUrl(versionAddresses, 5);
|
|
406
|
-
if (!launchedUrl) {
|
|
407
|
-
throw new Error(`\u65E0\u6CD5\u8FDE\u63A5\u5230\u6D4F\u89C8\u5668\uFF08\u7AEF\u53E3 ${CDP_PORT}\uFF09\uFF0C\u8BF7\u68C0\u67E5 Chrome/Edge \u662F\u5426\u5DF2\u5B89\u88C5\u3002`);
|
|
408
|
-
}
|
|
409
463
|
try {
|
|
410
464
|
console.log(pc.yellow(`connectBrowser: \u6D4F\u89C8\u5668\u5DF2\u542F\u52A8\uFF0C\u6B63\u5728\u8FDE\u63A5 ${launchedUrl}...`));
|
|
411
465
|
const browser = await connectPuppeteer(launchedUrl);
|
|
412
466
|
console.log(pc.green(`connectBrowser: \u6210\u529F\u8FDE\u63A5 ${launchedUrl}`));
|
|
413
467
|
return browser;
|
|
414
468
|
} catch {
|
|
415
|
-
throw new Error(`\u65E0\u6CD5\u8FDE\u63A5\u5230\u6D4F\u89C8\u5668\uFF08
|
|
469
|
+
throw new Error(`\u65E0\u6CD5\u8FDE\u63A5\u5230\u6D4F\u89C8\u5668\uFF08${launchedUrl}\uFF09\uFF0C\u8BF7\u68C0\u67E5 Chrome/Edge \u662F\u5426\u5DF2\u5B89\u88C5\u3002`);
|
|
416
470
|
}
|
|
417
471
|
}
|
|
418
472
|
async function getPageTargetId(page) {
|
|
@@ -492,10 +546,11 @@ async function getTargetPage(browser, tabid) {
|
|
|
492
546
|
}
|
|
493
547
|
if (!targetPage) {
|
|
494
548
|
try {
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
];
|
|
549
|
+
const workspaceDir = getWorkspaceDir();
|
|
550
|
+
const cdpPort = getCdpPort();
|
|
551
|
+
const recordedPort = readActivePortFromProfile(workspaceDir);
|
|
552
|
+
const ports = recordedPort && recordedPort !== cdpPort ? [recordedPort, cdpPort] : [cdpPort];
|
|
553
|
+
const urls = ports.flatMap((port) => candidateCdpBaseUrls(port).map((u) => `${u}/json/list`));
|
|
499
554
|
let activeTargetId = null;
|
|
500
555
|
for (const url of urls) {
|
|
501
556
|
try {
|
|
@@ -532,10 +587,10 @@ async function getTargetPage(browser, tabid) {
|
|
|
532
587
|
return targetPage;
|
|
533
588
|
}
|
|
534
589
|
async function injectIntoPage(page) {
|
|
535
|
-
await injectWebMCPPolyfillAndTools(page
|
|
590
|
+
await injectWebMCPPolyfillAndTools(page);
|
|
536
591
|
}
|
|
537
|
-
async function injectWebMCPPolyfillAndTools(page
|
|
538
|
-
const polyfillReady =
|
|
592
|
+
async function injectWebMCPPolyfillAndTools(page) {
|
|
593
|
+
const polyfillReady = await page.evaluate(() => {
|
|
539
594
|
return !!window.__webmcpcli_init;
|
|
540
595
|
}).catch(() => false);
|
|
541
596
|
if (!polyfillReady) {
|
|
@@ -625,11 +680,19 @@ async function stateCommand({ tabid }) {
|
|
|
625
680
|
const state = await page.evaluate(async () => {
|
|
626
681
|
const url = document.URL;
|
|
627
682
|
const title = document.title;
|
|
628
|
-
const mcp = document.modelContext;
|
|
683
|
+
const mcp = document.modelContext || navigator.modelContext;
|
|
629
684
|
let webmcpTools = [];
|
|
630
|
-
if (mcp && typeof mcp.
|
|
631
|
-
|
|
632
|
-
|
|
685
|
+
if (mcp && typeof mcp.getTools === "function") {
|
|
686
|
+
try {
|
|
687
|
+
const toolsResult = await mcp.getTools();
|
|
688
|
+
const list = toolsResult?.tools || toolsResult || [];
|
|
689
|
+
webmcpTools = list.filter((t) => t && typeof t.name === "string").map((t) => ({
|
|
690
|
+
name: t.name,
|
|
691
|
+
description: typeof t.description === "string" ? t.description.slice(0, 200) : void 0
|
|
692
|
+
}));
|
|
693
|
+
} catch {
|
|
694
|
+
webmcpTools = [];
|
|
695
|
+
}
|
|
633
696
|
}
|
|
634
697
|
return { url, title, webmcpTools };
|
|
635
698
|
});
|
|
@@ -661,155 +724,6 @@ async function stateCommand({ tabid }) {
|
|
|
661
724
|
}
|
|
662
725
|
}
|
|
663
726
|
|
|
664
|
-
// src/zhihu/create-article.ts
|
|
665
|
-
import os2 from "os";
|
|
666
|
-
|
|
667
|
-
// src/zhihu/markdown.ts
|
|
668
|
-
import { Marked } from "marked";
|
|
669
|
-
function stripLeadingH1(markdown) {
|
|
670
|
-
return markdown.replace(/^#\s+[^\n]*\n+/, "");
|
|
671
|
-
}
|
|
672
|
-
function markdownToHtml(markdown) {
|
|
673
|
-
const marked = new Marked({
|
|
674
|
-
gfm: true,
|
|
675
|
-
breaks: false,
|
|
676
|
-
renderer: {
|
|
677
|
-
code({ text, lang }) {
|
|
678
|
-
const langClass = lang ? ` class="language-${lang}"` : "";
|
|
679
|
-
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
680
|
-
return `<pre><code${langClass}>${escaped}</code></pre>`;
|
|
681
|
-
},
|
|
682
|
-
codespan({ text }) {
|
|
683
|
-
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
684
|
-
return `<code>${escaped}</code>`;
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
});
|
|
688
|
-
return marked.parse(markdown);
|
|
689
|
-
}
|
|
690
|
-
function decodeBase64Content(content) {
|
|
691
|
-
return Buffer.from(content, "base64").toString("utf-8");
|
|
692
|
-
}
|
|
693
|
-
var ZHIHU_WRITE_PAGE_RE = /^https:\/\/zhuanlan\.zhihu\.com\/(write|p\/\d+\/edit)/;
|
|
694
|
-
function isZhihuWriteUrl(url) {
|
|
695
|
-
return ZHIHU_WRITE_PAGE_RE.test(url);
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
// src/zhihu/create-article.ts
|
|
699
|
-
function delay(ms) {
|
|
700
|
-
return new Promise((resolve3) => setTimeout(resolve3, ms));
|
|
701
|
-
}
|
|
702
|
-
function getPasteModifier() {
|
|
703
|
-
return os2.platform() === "darwin" ? "Meta" : "Control";
|
|
704
|
-
}
|
|
705
|
-
async function fillTitle(page, title) {
|
|
706
|
-
await page.evaluate((titleText) => {
|
|
707
|
-
const input = document.querySelector('textarea[placeholder*="\u6807\u9898"]') || document.querySelector(".WriteIndex-titleInput textarea") || document.querySelector(".WriteIndex-titleInput input");
|
|
708
|
-
if (!input) throw new Error("\u672A\u627E\u5230\u6807\u9898\u8F93\u5165\u6846\uFF0C\u8BF7\u786E\u8BA4\u7F16\u8F91\u5668\u9875\u9762\u5DF2\u5B8C\u5168\u52A0\u8F7D");
|
|
709
|
-
const setter = Object.getOwnPropertyDescriptor(
|
|
710
|
-
input instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype,
|
|
711
|
-
"value"
|
|
712
|
-
)?.set;
|
|
713
|
-
if (setter) {
|
|
714
|
-
setter.call(input, titleText);
|
|
715
|
-
} else {
|
|
716
|
-
input.value = titleText;
|
|
717
|
-
}
|
|
718
|
-
input.dispatchEvent(new InputEvent("input", { bubbles: true, cancelable: true, data: titleText }));
|
|
719
|
-
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
720
|
-
if (input.value.trim() !== titleText.trim()) {
|
|
721
|
-
throw new Error("\u6807\u9898\u586B\u5199\u5931\u8D25\uFF0C\u8BF7\u5237\u65B0\u9875\u9762\u540E\u91CD\u8BD5");
|
|
722
|
-
}
|
|
723
|
-
}, title);
|
|
724
|
-
}
|
|
725
|
-
async function pasteHtmlToEditor(page, html) {
|
|
726
|
-
const browser = page.browser();
|
|
727
|
-
const tempPage = await browser.newPage();
|
|
728
|
-
const modifier = getPasteModifier();
|
|
729
|
-
try {
|
|
730
|
-
const fullHtml = `<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>${html}</body></html>`;
|
|
731
|
-
await tempPage.setContent(fullHtml, { waitUntil: "domcontentloaded" });
|
|
732
|
-
await delay(300);
|
|
733
|
-
await tempPage.click("body");
|
|
734
|
-
await tempPage.keyboard.down(modifier);
|
|
735
|
-
await tempPage.keyboard.press("KeyA");
|
|
736
|
-
await tempPage.keyboard.up(modifier);
|
|
737
|
-
await delay(100);
|
|
738
|
-
await tempPage.keyboard.down(modifier);
|
|
739
|
-
await tempPage.keyboard.press("KeyC");
|
|
740
|
-
await tempPage.keyboard.up(modifier);
|
|
741
|
-
await delay(300);
|
|
742
|
-
} finally {
|
|
743
|
-
await tempPage.close();
|
|
744
|
-
}
|
|
745
|
-
await page.bringToFront();
|
|
746
|
-
await page.waitForSelector(".public-DraftEditor-content", { timeout: 1e4 });
|
|
747
|
-
await page.click(".public-DraftEditor-content");
|
|
748
|
-
await delay(200);
|
|
749
|
-
await page.keyboard.down(modifier);
|
|
750
|
-
await page.keyboard.press("KeyA");
|
|
751
|
-
await page.keyboard.up(modifier);
|
|
752
|
-
await delay(100);
|
|
753
|
-
await page.keyboard.press("Backspace");
|
|
754
|
-
await delay(200);
|
|
755
|
-
await page.keyboard.down(modifier);
|
|
756
|
-
await page.keyboard.press("KeyV");
|
|
757
|
-
await page.keyboard.up(modifier);
|
|
758
|
-
await delay(1500);
|
|
759
|
-
const probe = html.replace(/<[^>]+>/g, "").trim().slice(0, 15);
|
|
760
|
-
const verifyResult = await page.evaluate((probeText) => {
|
|
761
|
-
const editor = document.querySelector(".public-DraftEditor-content");
|
|
762
|
-
if (!editor) return { ok: false, reason: "no editor" };
|
|
763
|
-
const text = (editor.innerText || "").trim();
|
|
764
|
-
if (!text) return { ok: false, reason: "empty" };
|
|
765
|
-
const inner = editor.innerHTML;
|
|
766
|
-
const hasRichFormat = inner.includes("data-text") || /font-weight:\s*bold|font-style:\s*italic|code-block|blockquote/i.test(inner);
|
|
767
|
-
const normalizedText = text.replace(/\s+/g, "");
|
|
768
|
-
const normalizedProbe = probeText.replace(/\s+/g, "");
|
|
769
|
-
const hasProbe = normalizedText.includes(normalizedProbe);
|
|
770
|
-
return {
|
|
771
|
-
ok: text.length > 20 && hasProbe && (hasRichFormat || text.length > 50),
|
|
772
|
-
reason: !hasProbe ? "probe mismatch" : !hasRichFormat && text.length <= 50 ? "no rich format" : void 0
|
|
773
|
-
};
|
|
774
|
-
}, probe);
|
|
775
|
-
if (!verifyResult.ok) {
|
|
776
|
-
throw new Error(
|
|
777
|
-
`\u6B63\u6587\u586B\u5199\u5931\u8D25\uFF08Markdown \u8F6C HTML \u7C98\u8D34\u672A\u751F\u6548\uFF09${verifyResult.reason ? `: ${verifyResult.reason}` : ""}`
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
async function zhihuCreateArticle(page, args) {
|
|
782
|
-
const { title, content } = args;
|
|
783
|
-
if (!title?.trim()) throw new Error("\u53C2\u6570 title \u4E0D\u80FD\u4E3A\u7A7A");
|
|
784
|
-
if (!content?.trim()) throw new Error("\u53C2\u6570 content \u4E0D\u80FD\u4E3A\u7A7A");
|
|
785
|
-
const url = page.url();
|
|
786
|
-
if (!isZhihuWriteUrl(url)) {
|
|
787
|
-
throw new Error("\u5F53\u524D\u9875\u9762\u4E0D\u662F\u77E5\u4E4E\u4E13\u680F\u7F16\u8F91\u5668\uFF0C\u8BF7\u5148\u6253\u5F00 https://zhuanlan.zhihu.com/write");
|
|
788
|
-
}
|
|
789
|
-
let markdown;
|
|
790
|
-
try {
|
|
791
|
-
markdown = stripLeadingH1(decodeBase64Content(content));
|
|
792
|
-
} catch {
|
|
793
|
-
throw new Error("content \u4E0D\u662F\u6709\u6548\u7684 Base64 \u7F16\u7801\uFF0C\u8BF7\u68C0\u67E5\u53C2\u6570\u6216\u4F7F\u7528 @base64file: \u5F15\u7528\u6587\u4EF6");
|
|
794
|
-
}
|
|
795
|
-
if (!markdown.trim()) {
|
|
796
|
-
throw new Error("\u6B63\u6587\u5185\u5BB9\u4E3A\u7A7A\uFF0C\u8BF7\u68C0\u67E5 Markdown \u6587\u4EF6");
|
|
797
|
-
}
|
|
798
|
-
const html = markdownToHtml(markdown);
|
|
799
|
-
await fillTitle(page, title.trim());
|
|
800
|
-
await delay(500);
|
|
801
|
-
await pasteHtmlToEditor(page, html);
|
|
802
|
-
const result = {
|
|
803
|
-
success: true,
|
|
804
|
-
message: "\u6587\u7AE0\u6807\u9898\u548C\u6B63\u6587\u5DF2\u6210\u529F\u586B\u5199\u5230\u77E5\u4E4E\u4E13\u680F\u7F16\u8F91\u5668\uFF08Markdown \u5DF2\u8F6C\u4E3A\u5BCC\u6587\u672C\uFF09\uFF0C\u8349\u7A3F\u5C06\u81EA\u52A8\u4FDD\u5B58",
|
|
805
|
-
title: title.trim(),
|
|
806
|
-
contentLength: markdown.length
|
|
807
|
-
};
|
|
808
|
-
return {
|
|
809
|
-
content: [{ type: "text", text: JSON.stringify(result) }]
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
|
|
813
727
|
// src/commands/run.ts
|
|
814
728
|
async function runCommand({
|
|
815
729
|
toolName,
|
|
@@ -820,18 +734,8 @@ async function runCommand({
|
|
|
820
734
|
try {
|
|
821
735
|
const page = await getTargetPage(browser, tabid);
|
|
822
736
|
const urlBefore = page.url();
|
|
737
|
+
await injectIntoPage(page);
|
|
823
738
|
const cleanedArgs = argsJson.trim();
|
|
824
|
-
if (toolName === "create_article") {
|
|
825
|
-
let parsedArgs;
|
|
826
|
-
try {
|
|
827
|
-
parsedArgs = JSON.parse(cleanedArgs);
|
|
828
|
-
} catch {
|
|
829
|
-
throw new Error("\u53C2\u6570\u4E0D\u662F\u6709\u6548\u7684 JSON");
|
|
830
|
-
}
|
|
831
|
-
if (isZhihuWriteUrl(page.url())) {
|
|
832
|
-
return await zhihuCreateArticle(page, parsedArgs);
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
739
|
let result;
|
|
836
740
|
try {
|
|
837
741
|
result = await page.evaluate(async (name, inputString) => {
|
|
@@ -1019,7 +923,7 @@ async function tabsForwardCommand(tabid) {
|
|
|
1019
923
|
// package.json
|
|
1020
924
|
var package_default = {
|
|
1021
925
|
name: "@opentiny/webmcp-cli",
|
|
1022
|
-
version: "0.0.
|
|
926
|
+
version: "0.0.8",
|
|
1023
927
|
type: "module",
|
|
1024
928
|
description: "WebMCP CLI for AI Agents to interact with browser via Puppeteer",
|
|
1025
929
|
main: "dist/index.js",
|
|
@@ -1037,7 +941,10 @@ var package_default = {
|
|
|
1037
941
|
build: "tsup && pnpm run build:inject",
|
|
1038
942
|
prepublishOnly: "pnpm run build",
|
|
1039
943
|
"link:global": "npm install -g .",
|
|
1040
|
-
start: "node dist/bin.js"
|
|
944
|
+
start: "node dist/bin.js",
|
|
945
|
+
test: "vitest run",
|
|
946
|
+
"test:watch": "vitest",
|
|
947
|
+
"test:browser": "vitest run --config vitest.e2e.config.ts"
|
|
1041
948
|
},
|
|
1042
949
|
keywords: [
|
|
1043
950
|
"chrome",
|
|
@@ -1068,7 +975,8 @@ var package_default = {
|
|
|
1068
975
|
esbuild: "^0.25.0",
|
|
1069
976
|
tsup: "^8.0.2",
|
|
1070
977
|
tsx: "^4.0.0",
|
|
1071
|
-
typescript: "^5.3.3"
|
|
978
|
+
typescript: "^5.3.3",
|
|
979
|
+
vitest: "^4.1.10"
|
|
1072
980
|
}
|
|
1073
981
|
};
|
|
1074
982
|
|
|
@@ -1078,6 +986,30 @@ function parseTabId(id) {
|
|
|
1078
986
|
if (!id) return void 0;
|
|
1079
987
|
return id;
|
|
1080
988
|
}
|
|
989
|
+
function tryParseBrowserStateText(text) {
|
|
990
|
+
const tryParse = (jsonStr) => {
|
|
991
|
+
try {
|
|
992
|
+
const parsed = JSON.parse(jsonStr);
|
|
993
|
+
if (parsed && typeof parsed.content === "string") {
|
|
994
|
+
return parsed;
|
|
995
|
+
}
|
|
996
|
+
} catch {
|
|
997
|
+
}
|
|
998
|
+
return null;
|
|
999
|
+
};
|
|
1000
|
+
const direct = tryParse(text);
|
|
1001
|
+
if (direct) return direct;
|
|
1002
|
+
if (text.startsWith("\u6D4F\u89C8\u5668\u72B6\u6001: ")) {
|
|
1003
|
+
let jsonStr = text.substring("\u6D4F\u89C8\u5668\u72B6\u6001: ".length);
|
|
1004
|
+
const firstNewline = jsonStr.indexOf("\n");
|
|
1005
|
+
if (firstNewline !== -1) {
|
|
1006
|
+
const maybeJson = jsonStr.substring(0, firstNewline);
|
|
1007
|
+
if (tryParse(maybeJson)) jsonStr = maybeJson;
|
|
1008
|
+
}
|
|
1009
|
+
return tryParse(jsonStr);
|
|
1010
|
+
}
|
|
1011
|
+
return null;
|
|
1012
|
+
}
|
|
1081
1013
|
function cleanOldLogs(baseDir, logDir) {
|
|
1082
1014
|
try {
|
|
1083
1015
|
const oldLogFile = path2.join(baseDir, "webmcp-cli.log");
|
|
@@ -1133,23 +1065,11 @@ function formatLogResult(result) {
|
|
|
1133
1065
|
let searchResultStr = "";
|
|
1134
1066
|
if (clone && Array.isArray(clone.content) && clone.content.length > 0 && clone.content[0] && typeof clone.content[0].text === "string") {
|
|
1135
1067
|
const text = clone.content[0].text;
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
extraAlerts = jsonStr.substring(firstNewline);
|
|
1142
|
-
jsonStr = jsonStr.substring(0, firstNewline);
|
|
1143
|
-
}
|
|
1144
|
-
try {
|
|
1145
|
-
const parsed = JSON.parse(jsonStr);
|
|
1146
|
-
if (parsed && typeof parsed.content === "string") {
|
|
1147
|
-
a11yTreeStr = parsed.content;
|
|
1148
|
-
parsed.content = "[Formatted A11y Tree - See details below]";
|
|
1149
|
-
clone.content[0].text = `\u6D4F\u89C8\u5668\u72B6\u6001: ${JSON.stringify(parsed, null, 2)}${extraAlerts}`;
|
|
1150
|
-
}
|
|
1151
|
-
} catch {
|
|
1152
|
-
}
|
|
1068
|
+
const browserState = tryParseBrowserStateText(text);
|
|
1069
|
+
if (browserState) {
|
|
1070
|
+
a11yTreeStr = browserState.content;
|
|
1071
|
+
browserState.content = "[Formatted A11y Tree - See details below]";
|
|
1072
|
+
clone.content[0].text = JSON.stringify(browserState, null, 2);
|
|
1153
1073
|
} else if (text.includes("A11y Tree \u641C\u7D22\u7ED3\u679C") || text.includes("\u65E0\u969C\u788D\u6811\u641C\u7D22\u7ED3\u679C") || text.includes("\u65E0\u969C\u788D\u6811\u641C\u7D22") || text.includes("\u547D\u4E2D\u884C") || text.includes("context:")) {
|
|
1154
1074
|
searchResultStr = text;
|
|
1155
1075
|
clone.content[0].text = "[Search A11y Tree Result - See details below]";
|
|
@@ -1179,7 +1099,7 @@ ${searchResultStr}`;
|
|
|
1179
1099
|
}
|
|
1180
1100
|
function writeLog(commandName, args, result, error) {
|
|
1181
1101
|
try {
|
|
1182
|
-
const baseDir = process.env.WEBMCP_WORKSPACE || path2.join(
|
|
1102
|
+
const baseDir = process.env.WEBMCP_WORKSPACE || path2.join(os2.homedir(), ".webmcp_chrome_profile");
|
|
1183
1103
|
const logDir = path2.join(baseDir, "logs");
|
|
1184
1104
|
if (!fs2.existsSync(logDir)) {
|
|
1185
1105
|
fs2.mkdirSync(logDir, { recursive: true });
|