@jackwener/browsermcp 0.0.0-stage → 0.0.33
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/CHANGELOG.md +334 -0
- package/LICENSE +202 -0
- package/README.md +177 -2
- package/adapter-sdk/index.d.ts +94 -0
- package/adapter-sdk/index.js +30 -0
- package/adapters/bilibili/_shared.js +254 -0
- package/adapters/bilibili/comment.js +55 -0
- package/adapters/bilibili/comments.js +47 -0
- package/adapters/bilibili/download.js +73 -0
- package/adapters/bilibili/dynamic.js +29 -0
- package/adapters/bilibili/favorite.js +39 -0
- package/adapters/bilibili/feed-detail.js +66 -0
- package/adapters/bilibili/feed.js +95 -0
- package/adapters/bilibili/follow.js +36 -0
- package/adapters/bilibili/following.js +29 -0
- package/adapters/bilibili/history.js +27 -0
- package/adapters/bilibili/hot.js +25 -0
- package/adapters/bilibili/me.js +24 -0
- package/adapters/bilibili/popular.js +26 -0
- package/adapters/bilibili/ranking.js +24 -0
- package/adapters/bilibili/search.js +37 -0
- package/adapters/bilibili/site.json +3 -0
- package/adapters/bilibili/subtitle.js +49 -0
- package/adapters/bilibili/summary.js +59 -0
- package/adapters/bilibili/unfollow.js +35 -0
- package/adapters/bilibili/user-videos.js +32 -0
- package/adapters/bilibili/video.js +70 -0
- package/adapters/discord/_shared.js +112 -0
- package/adapters/discord/channels.js +22 -0
- package/adapters/discord/delete.js +22 -0
- package/adapters/discord/goto.js +23 -0
- package/adapters/discord/members.js +28 -0
- package/adapters/discord/read.js +24 -0
- package/adapters/discord/search.js +28 -0
- package/adapters/discord/send.js +22 -0
- package/adapters/discord/servers.js +14 -0
- package/adapters/discord/site.json +1 -0
- package/adapters/discord/status.js +13 -0
- package/adapters/discord/thread-read.js +25 -0
- package/adapters/discord/threads.js +42 -0
- package/adapters/facebook/_friending.js +64 -0
- package/adapters/facebook/_graphql.js +51 -0
- package/adapters/facebook/_marketplace.js +31 -0
- package/adapters/facebook/_shared.js +148 -0
- package/adapters/facebook/add-friend.js +14 -0
- package/adapters/facebook/cancel-friend-request.js +14 -0
- package/adapters/facebook/events.js +77 -0
- package/adapters/facebook/feed.js +146 -0
- package/adapters/facebook/friends.js +72 -0
- package/adapters/facebook/groups.js +69 -0
- package/adapters/facebook/join-group.js +68 -0
- package/adapters/facebook/marketplace-inbox.js +71 -0
- package/adapters/facebook/marketplace-listings.js +68 -0
- package/adapters/facebook/memories.js +38 -0
- package/adapters/facebook/notifications.js +82 -0
- package/adapters/facebook/posts.js +19 -0
- package/adapters/facebook/profile.js +66 -0
- package/adapters/facebook/search.js +20 -0
- package/adapters/facebook/site.json +1 -0
- package/adapters/gmail/_query.js +19 -0
- package/adapters/gmail/_shared.js +241 -0
- package/adapters/gmail/attachments.js +17 -0
- package/adapters/gmail/create-draft.js +13 -0
- package/adapters/gmail/delete.js +30 -0
- package/adapters/gmail/drafts.js +2 -0
- package/adapters/gmail/forward.js +30 -0
- package/adapters/gmail/important.js +2 -0
- package/adapters/gmail/inbox.js +2 -0
- package/adapters/gmail/labels.js +10 -0
- package/adapters/gmail/modify.js +44 -0
- package/adapters/gmail/reply.js +28 -0
- package/adapters/gmail/search.js +21 -0
- package/adapters/gmail/send.js +84 -0
- package/adapters/gmail/sent.js +2 -0
- package/adapters/gmail/site.json +1 -0
- package/adapters/gmail/snoozed.js +2 -0
- package/adapters/gmail/spam.js +2 -0
- package/adapters/gmail/starred.js +2 -0
- package/adapters/gmail/thread.js +16 -0
- package/adapters/gmail/trash.js +2 -0
- package/adapters/gmail/unread.js +2 -0
- package/adapters/linkedin/_profile-card.js +42 -0
- package/adapters/linkedin/_profile-sections.js +76 -0
- package/adapters/linkedin/_profile.js +112 -0
- package/adapters/linkedin/_shared.js +50 -0
- package/adapters/linkedin/company.js +56 -0
- package/adapters/linkedin/connect.js +53 -0
- package/adapters/linkedin/connections.js +45 -0
- package/adapters/linkedin/inbox.js +102 -0
- package/adapters/linkedin/job-detail.js +49 -0
- package/adapters/linkedin/jobs-preferences.js +33 -0
- package/adapters/linkedin/jobs.js +98 -0
- package/adapters/linkedin/people-search.js +52 -0
- package/adapters/linkedin/post-analytics.js +38 -0
- package/adapters/linkedin/posts.js +21 -0
- package/adapters/linkedin/profile-analytics.js +43 -0
- package/adapters/linkedin/profile-experience.js +18 -0
- package/adapters/linkedin/profile-projects.js +18 -0
- package/adapters/linkedin/profile-read.js +32 -0
- package/adapters/linkedin/salesnav-inbox.js +52 -0
- package/adapters/linkedin/salesnav-message.js +126 -0
- package/adapters/linkedin/salesnav-search.js +56 -0
- package/adapters/linkedin/salesnav-thread.js +106 -0
- package/adapters/linkedin/sent-invitations.js +40 -0
- package/adapters/linkedin/services-read.js +81 -0
- package/adapters/linkedin/site.json +3 -0
- package/adapters/linkedin/timeline.js +45 -0
- package/adapters/reddit/_shared.js +115 -0
- package/adapters/reddit/comment.js +26 -0
- package/adapters/reddit/frontpage.js +22 -0
- package/adapters/reddit/home.js +25 -0
- package/adapters/reddit/hot.js +25 -0
- package/adapters/reddit/popular.js +22 -0
- package/adapters/reddit/read.js +156 -0
- package/adapters/reddit/reply.js +26 -0
- package/adapters/reddit/save.js +21 -0
- package/adapters/reddit/saved.js +23 -0
- package/adapters/reddit/search.js +31 -0
- package/adapters/reddit/site.json +3 -0
- package/adapters/reddit/subreddit-info.js +39 -0
- package/adapters/reddit/subreddit.js +27 -0
- package/adapters/reddit/subscribe.js +20 -0
- package/adapters/reddit/subscribed.js +43 -0
- package/adapters/reddit/upvote.js +23 -0
- package/adapters/reddit/upvoted.js +23 -0
- package/adapters/reddit/user-comments.js +25 -0
- package/adapters/reddit/user-posts.js +25 -0
- package/adapters/reddit/user.js +36 -0
- package/adapters/reddit/whoami.js +29 -0
- package/adapters/twitter/_shared.js +345 -0
- package/adapters/twitter/accept.js +16 -0
- package/adapters/twitter/article.js +104 -0
- package/adapters/twitter/block.js +16 -0
- package/adapters/twitter/bookmark-folder.js +39 -0
- package/adapters/twitter/bookmark-folders.js +46 -0
- package/adapters/twitter/bookmark.js +14 -0
- package/adapters/twitter/bookmarks.js +33 -0
- package/adapters/twitter/collection.js +135 -0
- package/adapters/twitter/delete.js +14 -0
- package/adapters/twitter/device-follow.js +60 -0
- package/adapters/twitter/download.js +87 -0
- package/adapters/twitter/follow-batch.js +28 -0
- package/adapters/twitter/follow.js +16 -0
- package/adapters/twitter/followers.js +60 -0
- package/adapters/twitter/following.js +65 -0
- package/adapters/twitter/hide-reply.js +19 -0
- package/adapters/twitter/like.js +14 -0
- package/adapters/twitter/likes.js +41 -0
- package/adapters/twitter/list-add-batch.js +30 -0
- package/adapters/twitter/list-add.js +53 -0
- package/adapters/twitter/list-create.js +45 -0
- package/adapters/twitter/list-delete.js +15 -0
- package/adapters/twitter/list-remove-batch.js +30 -0
- package/adapters/twitter/list-remove.js +28 -0
- package/adapters/twitter/list-tweets.js +38 -0
- package/adapters/twitter/lists.js +18 -0
- package/adapters/twitter/mute-word.js +15 -0
- package/adapters/twitter/notifications.js +90 -0
- package/adapters/twitter/post.js +18 -0
- package/adapters/twitter/profile.js +43 -0
- package/adapters/twitter/quote.js +23 -0
- package/adapters/twitter/reply-dm.js +49 -0
- package/adapters/twitter/reply.js +23 -0
- package/adapters/twitter/retweet.js +16 -0
- package/adapters/twitter/search.js +150 -0
- package/adapters/twitter/site.json +3 -0
- package/adapters/twitter/thread.js +56 -0
- package/adapters/twitter/timeline.js +62 -0
- package/adapters/twitter/trending.js +32 -0
- package/adapters/twitter/tweets.js +46 -0
- package/adapters/twitter/unblock.js +16 -0
- package/adapters/twitter/unbookmark.js +14 -0
- package/adapters/twitter/unfollow.js +16 -0
- package/adapters/twitter/unlike.js +14 -0
- package/adapters/twitter/unretweet.js +15 -0
- package/cordis.patch.yml +13 -0
- package/dist/docs/api-reference.md +294 -0
- package/dist/docs/api-use.md +76 -0
- package/dist/docs/capabilities/cdp.md +7 -0
- package/dist/docs/capabilities/chrome.md +60 -0
- package/dist/docs/capabilities/visibility.md +2 -0
- package/dist/docs/capabilities/webmcp.md +6 -0
- package/dist/docs/define-tools.md +25 -0
- package/dist/docs/errors.md +28 -0
- package/dist/docs/instructions.md +14 -0
- package/dist/docs/js-tool.md +36 -0
- package/dist/docs/recon.md +5 -0
- package/dist/docs/repl-session.md +23 -0
- package/dist/docs/safety.md +8 -0
- package/dist/docs/sites.md +22 -0
- package/dist/docs/tab-lifecycle.md +15 -0
- package/dist/docs/troubleshooting.md +7 -0
- package/dist/src/api/action-input.js +171 -0
- package/dist/src/api/action-input.js.map +1 -0
- package/dist/src/api/agent.js +11 -0
- package/dist/src/api/agent.js.map +1 -0
- package/dist/src/api/api.js +69 -0
- package/dist/src/api/api.js.map +1 -0
- package/dist/src/api/browser.js +154 -0
- package/dist/src/api/browser.js.map +1 -0
- package/dist/src/api/context.js +2 -0
- package/dist/src/api/context.js.map +1 -0
- package/dist/src/api/diff.js +65 -0
- package/dist/src/api/diff.js.map +1 -0
- package/dist/src/api/errors.js +24 -0
- package/dist/src/api/errors.js.map +1 -0
- package/dist/src/api/network.js +33 -0
- package/dist/src/api/network.js.map +1 -0
- package/dist/src/api/observation.js +96 -0
- package/dist/src/api/observation.js.map +1 -0
- package/dist/src/api/streams.js +33 -0
- package/dist/src/api/streams.js.map +1 -0
- package/dist/src/api/tab.js +489 -0
- package/dist/src/api/tab.js.map +1 -0
- package/dist/src/api/webmcp.js +43 -0
- package/dist/src/api/webmcp.js.map +1 -0
- package/dist/src/backends/browser-helpers.js +76 -0
- package/dist/src/backends/browser-helpers.js.map +1 -0
- package/dist/src/backends/extension-page.js +322 -0
- package/dist/src/backends/extension-page.js.map +1 -0
- package/dist/src/backends/page-types.js +2 -0
- package/dist/src/backends/page-types.js.map +1 -0
- package/dist/src/docs/manifest.js +92 -0
- package/dist/src/docs/manifest.js.map +1 -0
- package/dist/src/docs/surface.js +95 -0
- package/dist/src/docs/surface.js.map +1 -0
- package/dist/src/host/bridge.js +126 -0
- package/dist/src/host/bridge.js.map +1 -0
- package/dist/src/host/doctor.js +92 -0
- package/dist/src/host/doctor.js.map +1 -0
- package/dist/src/host/extension.js +4 -0
- package/dist/src/host/extension.js.map +1 -0
- package/dist/src/host/host.js +77 -0
- package/dist/src/host/host.js.map +1 -0
- package/dist/src/host/ipc.js +80 -0
- package/dist/src/host/ipc.js.map +1 -0
- package/dist/src/host/jsonc-config.js +30 -0
- package/dist/src/host/jsonc-config.js.map +1 -0
- package/dist/src/host/launch.js +103 -0
- package/dist/src/host/launch.js.map +1 -0
- package/dist/src/host/native-messaging.js +81 -0
- package/dist/src/host/native-messaging.js.map +1 -0
- package/dist/src/host/opencode.js +14 -0
- package/dist/src/host/opencode.js.map +1 -0
- package/dist/src/host/pi.js +11 -0
- package/dist/src/host/pi.js.map +1 -0
- package/dist/src/host/registration.js +121 -0
- package/dist/src/host/registration.js.map +1 -0
- package/dist/src/host/setup.js +196 -0
- package/dist/src/host/setup.js.map +1 -0
- package/dist/src/host/socket.js +99 -0
- package/dist/src/host/socket.js.map +1 -0
- package/dist/src/host/state.js +123 -0
- package/dist/src/host/state.js.map +1 -0
- package/dist/src/launcher/stdio.js +153 -0
- package/dist/src/launcher/stdio.js.map +1 -0
- package/dist/src/lib/paths.js +4 -0
- package/dist/src/lib/paths.js.map +1 -0
- package/dist/src/lib/sources.js +27 -0
- package/dist/src/lib/sources.js.map +1 -0
- package/dist/src/main.js +131 -0
- package/dist/src/main.js.map +1 -0
- package/dist/src/mcp/catalog.js +28 -0
- package/dist/src/mcp/catalog.js.map +1 -0
- package/dist/src/mcp/js-evaluator.mjs +65 -0
- package/dist/src/mcp/js-session.js +292 -0
- package/dist/src/mcp/js-session.js.map +1 -0
- package/dist/src/mcp/js-worker.mjs +135 -0
- package/dist/src/mcp/server.js +221 -0
- package/dist/src/mcp/server.js.map +1 -0
- package/dist/src/protocol.js +18 -0
- package/dist/src/protocol.js.map +1 -0
- package/dist/src/recon/analyzer.js +366 -0
- package/dist/src/recon/analyzer.js.map +1 -0
- package/dist/src/recon/discover.js +130 -0
- package/dist/src/recon/discover.js.map +1 -0
- package/dist/src/runtime/runtime.js +231 -0
- package/dist/src/runtime/runtime.js.map +1 -0
- package/dist/src/shared/aria-tree.js +16 -0
- package/dist/src/shared/aria-tree.js.map +1 -0
- package/dist/src/shared/element-ref.js +7 -0
- package/dist/src/shared/element-ref.js.map +1 -0
- package/dist/src/shared/engine.js +426 -0
- package/dist/src/shared/engine.js.map +1 -0
- package/dist/src/shared/injected-source.js +5 -0
- package/dist/src/shared/injected-source.js.map +1 -0
- package/dist/src/shared/page-contract.js +8 -0
- package/dist/src/shared/page-contract.js.map +1 -0
- package/dist/src/sites/define.js +107 -0
- package/dist/src/sites/define.js.map +1 -0
- package/dist/src/sites/drafts.js +134 -0
- package/dist/src/sites/drafts.js.map +1 -0
- package/dist/src/sites/executor.js +75 -0
- package/dist/src/sites/executor.js.map +1 -0
- package/dist/src/sites/loader.js +234 -0
- package/dist/src/sites/loader.js.map +1 -0
- package/dist/src/sites/schema.js +169 -0
- package/dist/src/sites/schema.js.map +1 -0
- package/docs/api-reference.md +294 -0
- package/docs/api-use.md +76 -0
- package/docs/capabilities/cdp.md +7 -0
- package/docs/capabilities/chrome.md +60 -0
- package/docs/capabilities/visibility.md +2 -0
- package/docs/capabilities/webmcp.md +6 -0
- package/docs/define-tools.md +25 -0
- package/docs/errors.md +28 -0
- package/docs/guide.zh-CN.md +49 -0
- package/docs/instructions.md +14 -0
- package/docs/js-tool.md +36 -0
- package/docs/observation-design.md +51 -0
- package/docs/recon.md +5 -0
- package/docs/releasing.md +26 -0
- package/docs/repl-session.md +23 -0
- package/docs/safety.md +8 -0
- package/docs/setup.md +255 -0
- package/docs/sites.md +22 -0
- package/docs/tab-lifecycle.md +15 -0
- package/docs/troubleshooting.md +7 -0
- package/extension/dist/background.js +5261 -0
- package/extension/dist/chrome-reference.LICENSE.txt +21 -0
- package/extension/dist/content/cursor.js +156 -0
- package/extension/dist/cursor.svg +4 -0
- package/extension/dist/icons/icon-128.png +0 -0
- package/extension/dist/icons/icon-16.png +0 -0
- package/extension/dist/icons/icon-32.png +0 -0
- package/extension/dist/icons/icon-48.png +0 -0
- package/extension/dist/manifest.json +48 -0
- package/extension/dist/page.js +834 -0
- package/package.json +76 -4
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
|
|
3
|
+
export async function facebookGraphql(tab, pageUrl, operation, variables, { allowPartial = false, docId } = {}) {
|
|
4
|
+
if (!/^https:\/\/www\.facebook\.com\/[\w/?=&%.-]*$/.test(pageUrl) || !/^[A-Za-z0-9_]+$/.test(operation)) {
|
|
5
|
+
throw errors.argument('Invalid Facebook GraphQL request');
|
|
6
|
+
}
|
|
7
|
+
await tab.goto(pageUrl, { waitUntil: 'load' });
|
|
8
|
+
await tab.network.start('/api/graphql/');
|
|
9
|
+
const before = (await tab.network.read({ pattern: '/api/graphql/', limit: 1000 })).cursor;
|
|
10
|
+
const url = new URL(pageUrl);
|
|
11
|
+
url.searchParams.set('browsermcp_probe', String(Date.now()));
|
|
12
|
+
await tab.goto(url.href, { waitUntil: 'load' });
|
|
13
|
+
let entry;
|
|
14
|
+
let cursor = before;
|
|
15
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
16
|
+
const page = await tab.network.read({ pattern: '/api/graphql/', afterSequence: cursor, limit: 100 });
|
|
17
|
+
cursor = page.cursor;
|
|
18
|
+
entry = page.entries.find((item) => item.method === 'POST' && !item.requestBodyTruncated && item.requestBodyPreview);
|
|
19
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
20
|
+
}
|
|
21
|
+
if (!entry) throw errors.upstream('Facebook did not issue a GraphQL request for session bootstrap');
|
|
22
|
+
const form = new URLSearchParams(entry.requestBodyPreview);
|
|
23
|
+
const identity = await tab.evaluate(`(() => {
|
|
24
|
+
let provided = {};
|
|
25
|
+
try {
|
|
26
|
+
const query = require('${operation}.graphql');
|
|
27
|
+
provided = Object.fromEntries(Object.entries(query.params.providedVariables || {})
|
|
28
|
+
.map(([key, value]) => [key, value.get()]));
|
|
29
|
+
} catch {}
|
|
30
|
+
let id;
|
|
31
|
+
try { id = require('${operation}_facebookRelayOperation'); } catch { id = null; }
|
|
32
|
+
return { id, provided };
|
|
33
|
+
})()`);
|
|
34
|
+
const id = identity?.id || docId;
|
|
35
|
+
if (!/^\d+$/.test(String(id || ''))) throw errors.upstream(`Facebook did not load ${operation}`);
|
|
36
|
+
form.set('fb_api_req_friendly_name', operation);
|
|
37
|
+
form.set('doc_id', id);
|
|
38
|
+
form.set('variables', JSON.stringify({ ...identity.provided, ...variables }));
|
|
39
|
+
const response = await tab.evaluate(`(async () => {
|
|
40
|
+
const response = await fetch('/api/graphql/', { method: 'POST', credentials: 'include',
|
|
41
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: ${JSON.stringify(form.toString())} });
|
|
42
|
+
if (!response.ok) return { httpError: response.status };
|
|
43
|
+
const text = await response.text();
|
|
44
|
+
try { return JSON.parse(text.split(String.fromCharCode(10))[0].replace(/^for\\s*\\(;;\\);/, '')); }
|
|
45
|
+
catch { return { malformed: true }; }
|
|
46
|
+
})()`);
|
|
47
|
+
if (response?.httpError) throw errors.upstream(`Facebook GraphQL returned HTTP ${response.httpError}`);
|
|
48
|
+
if (response?.malformed) throw errors.upstream('Facebook GraphQL returned malformed JSON');
|
|
49
|
+
if (response?.errors?.length && !allowPartial) throw errors.upstream(`Facebook GraphQL: ${String(response.errors[0]?.message || 'query failed')}`);
|
|
50
|
+
return response;
|
|
51
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { facebookGraphql } from './_graphql.js';
|
|
3
|
+
|
|
4
|
+
const HOME = 'https://www.facebook.com/';
|
|
5
|
+
|
|
6
|
+
export async function ensureMarketplace(tab, path) {
|
|
7
|
+
await tab.goto(`https://www.facebook.com/marketplace/${path}`, { waitUntil: 'load' });
|
|
8
|
+
const url = await tab.url();
|
|
9
|
+
if (/\/marketplace\/ineligible\//.test(url || '')) {
|
|
10
|
+
throw errors.auth('Facebook Marketplace is unavailable for this account',
|
|
11
|
+
'Sign in with a Facebook account that can access Marketplace.');
|
|
12
|
+
}
|
|
13
|
+
if (!url?.startsWith('https://www.facebook.com/marketplace/')) {
|
|
14
|
+
throw errors.auth('Facebook Marketplace did not open in the signed-in session');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function marketplaceQuery(tab, name, docId, variables) {
|
|
19
|
+
return facebookGraphql(tab, HOME, name, variables, { docId });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function textOf(value) {
|
|
23
|
+
if (typeof value === 'string' || typeof value === 'number') return String(value).trim();
|
|
24
|
+
if (value && typeof value === 'object') {
|
|
25
|
+
for (const field of ['text', 'formatted_amount', 'display_amount', 'amount', 'name', 'title']) {
|
|
26
|
+
const result = textOf(value[field]);
|
|
27
|
+
if (result) return result;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return '';
|
|
31
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
|
|
3
|
+
const ORIGIN = 'https://www.facebook.com';
|
|
4
|
+
const SEARCH_OPERATION = 'SearchCometResultsPaginatedResultsQuery';
|
|
5
|
+
|
|
6
|
+
export function safeFacebookUrl(value) {
|
|
7
|
+
if (typeof value !== 'string' || !value.trim()) return null;
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(value, ORIGIN);
|
|
10
|
+
if (url.protocol !== 'https:' || !['facebook.com', 'www.facebook.com'].includes(url.hostname)) return null;
|
|
11
|
+
return url.href;
|
|
12
|
+
} catch { return null; }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function parseProfileSearch(json) {
|
|
16
|
+
if (Array.isArray(json?.errors) && json.errors.length) throw errors.upstream(`Facebook search API: ${String(json.errors[0]?.message || 'GraphQL error')}`);
|
|
17
|
+
const result = json?.data?.serpResponse?.results;
|
|
18
|
+
if (!result || !Array.isArray(result.edges)) throw errors.upstream('Facebook search API response changed shape');
|
|
19
|
+
const rows = result.edges.map((edge) => {
|
|
20
|
+
const view = edge?.rendering_strategy?.view_model;
|
|
21
|
+
const profile = view?.loggedProfile;
|
|
22
|
+
if (!profile?.id || !profile?.name) return null;
|
|
23
|
+
return {
|
|
24
|
+
id: String(profile.id),
|
|
25
|
+
name: String(profile.name).trim(),
|
|
26
|
+
url: safeFacebookUrl(profile.url || view?.profile?.url),
|
|
27
|
+
description: String(view?.primary_snippet_text_with_entities?.text || '').trim() || null,
|
|
28
|
+
picture: typeof profile?.typeaheadProfilePicture?.uri === 'string' ? profile.typeaheadProfilePicture.uri : null,
|
|
29
|
+
};
|
|
30
|
+
}).filter(Boolean);
|
|
31
|
+
return { rows, pageInfo: result.page_info || null };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Bootstrap a current GraphQL document and CSRF form from Facebook's own search request. */
|
|
35
|
+
async function searchForm(tab, query, type, limit, cursor) {
|
|
36
|
+
const normalized = String(query || '').trim();
|
|
37
|
+
if (!normalized) throw errors.argument('query is required');
|
|
38
|
+
if (!['people', 'groups', 'pages'].includes(type)) throw errors.argument('type must be people, groups, or pages');
|
|
39
|
+
const path = '/api/graphql/';
|
|
40
|
+
await tab.goto(`${ORIGIN}/`, { waitUntil: 'load' });
|
|
41
|
+
await tab.network.start(path);
|
|
42
|
+
const before = (await tab.network.read({ pattern: path, limit: 1000 })).cursor;
|
|
43
|
+
await tab.goto(`${ORIGIN}/search/${type}/?q=${encodeURIComponent(normalized)}`, { waitUntil: 'load' });
|
|
44
|
+
let networkCursor = before;
|
|
45
|
+
let entry;
|
|
46
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
47
|
+
const page = await tab.network.read({ pattern: path, afterSequence: networkCursor, limit: 100 });
|
|
48
|
+
networkCursor = page.cursor;
|
|
49
|
+
entry = page.entries.find((item) => {
|
|
50
|
+
if (item.method !== 'POST' || item.requestBodyTruncated || !item.requestBodyPreview) return false;
|
|
51
|
+
return new URLSearchParams(item.requestBodyPreview).get('fb_api_req_friendly_name') === SEARCH_OPERATION;
|
|
52
|
+
});
|
|
53
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
54
|
+
}
|
|
55
|
+
if (!entry) throw errors.upstream('Facebook did not issue a search GraphQL request');
|
|
56
|
+
const form = new URLSearchParams(entry.requestBodyPreview);
|
|
57
|
+
let variables;
|
|
58
|
+
try { variables = JSON.parse(form.get('variables') || '{}'); } catch { throw errors.upstream('Facebook search variables are malformed'); }
|
|
59
|
+
if (!variables?.args || variables.args.text !== normalized || variables.args.experience?.type !== `${type.toUpperCase()}_TAB`) throw errors.upstream('Facebook search request did not match the query');
|
|
60
|
+
variables.cursor = cursor;
|
|
61
|
+
variables.count = limit;
|
|
62
|
+
form.set('variables', JSON.stringify(variables));
|
|
63
|
+
return form;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function searchProfilesApi(tab, query, type, limit, cursor = null) {
|
|
67
|
+
const form = await searchForm(tab, query, type, limit, cursor);
|
|
68
|
+
const path = '/api/graphql/';
|
|
69
|
+
const body = form.toString();
|
|
70
|
+
const response = await tab.evaluate(`(async () => {
|
|
71
|
+
const response = await fetch('${path}', {
|
|
72
|
+
method: 'POST', credentials: 'include',
|
|
73
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
74
|
+
body: ${JSON.stringify(body)},
|
|
75
|
+
});
|
|
76
|
+
const text = await response.text();
|
|
77
|
+
if (!response.ok) return { httpError: response.status };
|
|
78
|
+
try { return JSON.parse(text.replace(/^for\\s*\\(;;\\);/, '')); }
|
|
79
|
+
catch { return { malformed: true }; }
|
|
80
|
+
})()`);
|
|
81
|
+
if (response?.httpError) throw errors.upstream(`Facebook GraphQL returned HTTP ${response.httpError}`);
|
|
82
|
+
if (response?.malformed) throw errors.upstream('Facebook GraphQL returned malformed JSON');
|
|
83
|
+
return response;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function parsePostRows(result) {
|
|
87
|
+
if (Array.isArray(result?.errors) && result.errors.length) throw errors.upstream(`Facebook posts search API: ${String(result.errors[0]?.message || 'GraphQL error')}`);
|
|
88
|
+
if (!Array.isArray(result?.edges)) throw errors.upstream('Facebook posts search API response changed shape');
|
|
89
|
+
const rows = result.edges.map((edge) => {
|
|
90
|
+
const story = edge?.rendering_strategy?.view_model?.click_model?.story;
|
|
91
|
+
if (!story) return null;
|
|
92
|
+
const content = story.comet_sections?.content?.story;
|
|
93
|
+
const nested = content?.comet_sections?.message?.story;
|
|
94
|
+
const id = String(story.post_id || story.id || '').trim();
|
|
95
|
+
const url = safeFacebookUrl(story.permalink_url || nested?.permalink_url || content?.permalink_url);
|
|
96
|
+
if (!id || !url) return null;
|
|
97
|
+
const timestamp = Number(story.creation_time || 0);
|
|
98
|
+
return {
|
|
99
|
+
id,
|
|
100
|
+
author: String(story.actors?.[0]?.name || content?.actors?.[0]?.name || '').trim() || null,
|
|
101
|
+
author_id: String(story.actors?.[0]?.id || content?.actors?.[0]?.id || '').trim() || null,
|
|
102
|
+
text: String(story.message?.text || content?.message?.text || nested?.message?.text || '').trim().slice(0, 5000) || null,
|
|
103
|
+
url,
|
|
104
|
+
created_at: timestamp > 0 ? new Date(timestamp < 1e10 ? timestamp * 1000 : timestamp).toISOString() : null,
|
|
105
|
+
};
|
|
106
|
+
}).filter(Boolean);
|
|
107
|
+
return { rows, pageInfo: result.pageInfo || null };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Posts stream large deferred media patches; return only first-chunk story fields. */
|
|
111
|
+
export async function searchPostsApi(tab, query, limit, cursor = null) {
|
|
112
|
+
const form = await searchForm(tab, query, 'groups', limit, cursor);
|
|
113
|
+
const variables = JSON.parse(form.get('variables'));
|
|
114
|
+
variables.args.experience.type = 'POSTS_TAB';
|
|
115
|
+
form.set('variables', JSON.stringify(variables));
|
|
116
|
+
const response = await tab.evaluate(`(async () => {
|
|
117
|
+
const response = await fetch('/api/graphql/', {
|
|
118
|
+
method: 'POST', credentials: 'include',
|
|
119
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
120
|
+
body: ${JSON.stringify(form.toString())},
|
|
121
|
+
});
|
|
122
|
+
if (!response.ok) return { httpError: response.status };
|
|
123
|
+
const text = await response.text();
|
|
124
|
+
let json;
|
|
125
|
+
try { json = JSON.parse(text.split(String.fromCharCode(10))[0].replace(/^for\\s*\\(;;\\);/, '')); }
|
|
126
|
+
catch { return { malformed: true }; }
|
|
127
|
+
const result = json?.data?.serpResponse?.results;
|
|
128
|
+
return { edges: (result?.edges || []).map((edge) => ({
|
|
129
|
+
rendering_strategy: { view_model: { click_model: { story: (() => {
|
|
130
|
+
const story = edge?.rendering_strategy?.view_model?.click_model?.story;
|
|
131
|
+
const content = story?.comet_sections?.content?.story;
|
|
132
|
+
const nested = content?.comet_sections?.message?.story;
|
|
133
|
+
return story ? {
|
|
134
|
+
id: story.id, post_id: story.post_id, permalink_url: story.permalink_url,
|
|
135
|
+
creation_time: story.creation_time, actors: story.actors,
|
|
136
|
+
message: story.message,
|
|
137
|
+
comet_sections: { content: { story: content ? {
|
|
138
|
+
actors: content.actors, message: content.message, permalink_url: content.permalink_url,
|
|
139
|
+
comet_sections: { message: { story: nested ? { message: nested.message, permalink_url: nested.permalink_url } : null } },
|
|
140
|
+
} : null } },
|
|
141
|
+
} : null;
|
|
142
|
+
})() } } },
|
|
143
|
+
})), pageInfo: result?.page_info, errors: json?.errors };
|
|
144
|
+
})()`);
|
|
145
|
+
if (response?.httpError) throw errors.upstream(`Facebook posts GraphQL returned HTTP ${response.httpError}`);
|
|
146
|
+
if (response?.malformed) throw errors.upstream('Facebook posts GraphQL returned malformed JSON');
|
|
147
|
+
return response;
|
|
148
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { friendMutation, resolveFriendId } from './_friending.js';
|
|
3
|
+
|
|
4
|
+
export default defineAdapter({
|
|
5
|
+
description: 'Send a Facebook friend request through the friending GraphQL API.',
|
|
6
|
+
access: 'write', domain: 'facebook.com',
|
|
7
|
+
result: { kind: 'value', description: 'Friend request status' },
|
|
8
|
+
args: [{ name: 'user', type: 'string', required: true, help: 'Facebook user ID, username, or profile URL' }],
|
|
9
|
+
async run({ tab, args }) {
|
|
10
|
+
if (!args.user) throw errors.argument('user is required');
|
|
11
|
+
const id = await resolveFriendId(tab, args.user);
|
|
12
|
+
return friendMutation(tab, 'send', id);
|
|
13
|
+
},
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { friendMutation, resolveFriendId } from './_friending.js';
|
|
3
|
+
|
|
4
|
+
export default defineAdapter({
|
|
5
|
+
description: 'Cancel a pending Facebook friend request through the friending GraphQL API.',
|
|
6
|
+
access: 'write', domain: 'facebook.com',
|
|
7
|
+
result: { kind: 'value', description: 'Friend request status' },
|
|
8
|
+
args: [{ name: 'user', type: 'string', required: true, help: 'Facebook user ID, username, or profile URL' }],
|
|
9
|
+
async run({ tab, args }) {
|
|
10
|
+
if (!args.user) throw errors.argument('user is required');
|
|
11
|
+
const id = await resolveFriendId(tab, args.user);
|
|
12
|
+
return friendMutation(tab, 'cancel', id);
|
|
13
|
+
},
|
|
14
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { safeFacebookUrl } from './_shared.js';
|
|
3
|
+
|
|
4
|
+
const OPERATION = 'EventCometHomeDiscoverContentRefetchQuery';
|
|
5
|
+
|
|
6
|
+
export function mapEvents(result) {
|
|
7
|
+
if (result?.errors?.length) throw errors.upstream(`Facebook events API: ${String(result.errors[0]?.message || 'GraphQL error')}`);
|
|
8
|
+
const connection = result?.data?.node?.content_tab?.requested_tab?.events;
|
|
9
|
+
if (!Array.isArray(connection?.edges)) throw errors.upstream('Facebook events API response changed shape');
|
|
10
|
+
const rows = connection.edges.map(({ node }) => {
|
|
11
|
+
const id = String(node?.id || '').trim();
|
|
12
|
+
const name = String(node?.name || '').trim();
|
|
13
|
+
if (!id || !name) return null;
|
|
14
|
+
const timestamp = Number(node.start_timestamp);
|
|
15
|
+
return {
|
|
16
|
+
id, name,
|
|
17
|
+
url: safeFacebookUrl(node.eventUrl) || `https://www.facebook.com/events/${id}/`,
|
|
18
|
+
starts_at: Number.isFinite(timestamp) && timestamp > 0 ? new Date(timestamp * 1000).toISOString() : null,
|
|
19
|
+
when: String(node.day_time_sentence || '').trim() || null,
|
|
20
|
+
place: String(node.event_place?.name || '').trim() || null,
|
|
21
|
+
online: Boolean(node.is_online),
|
|
22
|
+
};
|
|
23
|
+
}).filter(Boolean);
|
|
24
|
+
return { rows, pageInfo: connection.page_info || null };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function eventForm(tab, limit, cursor) {
|
|
28
|
+
await tab.goto('https://www.facebook.com/events', { waitUntil: 'load' });
|
|
29
|
+
await tab.network.start('/api/graphql/');
|
|
30
|
+
let networkCursor = (await tab.network.read({ pattern: '/api/graphql/', limit: 1000 })).cursor;
|
|
31
|
+
await tab.goto(`https://www.facebook.com/events?browsermcp_events=${Date.now()}`, { waitUntil: 'load' });
|
|
32
|
+
let entry;
|
|
33
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
34
|
+
const page = await tab.network.read({ pattern: '/api/graphql/', afterSequence: networkCursor, limit: 100 });
|
|
35
|
+
networkCursor = page.cursor;
|
|
36
|
+
entry = page.entries.find((item) => item.method === 'POST' && !item.requestBodyTruncated &&
|
|
37
|
+
new URLSearchParams(item.requestBodyPreview || '').get('fb_api_req_friendly_name') === OPERATION);
|
|
38
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
39
|
+
}
|
|
40
|
+
if (!entry) throw errors.upstream('Facebook did not issue an events GraphQL request');
|
|
41
|
+
const form = new URLSearchParams(entry.requestBodyPreview);
|
|
42
|
+
let variables;
|
|
43
|
+
try { variables = JSON.parse(form.get('variables') || '{}'); }
|
|
44
|
+
catch { throw errors.upstream('Facebook events variables are malformed'); }
|
|
45
|
+
if (!variables || typeof variables !== 'object') throw errors.upstream('Facebook events variables are malformed');
|
|
46
|
+
variables.count = limit;
|
|
47
|
+
if (cursor) variables.cursor = cursor;
|
|
48
|
+
form.set('variables', JSON.stringify(variables));
|
|
49
|
+
return form.toString();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default defineAdapter({
|
|
53
|
+
description: 'Discover Facebook events through its logged-in GraphQL API.',
|
|
54
|
+
access: 'read', domain: 'facebook.com',
|
|
55
|
+
result: { kind: 'rows', description: 'Events', paginated: true },
|
|
56
|
+
args: [
|
|
57
|
+
{ name: 'limit', type: 'int', default: 9, min: 1, max: 20 },
|
|
58
|
+
{ name: 'cursor', type: 'string', help: 'Cursor from a previous page' },
|
|
59
|
+
],
|
|
60
|
+
async run({ tab, args }) {
|
|
61
|
+
const limit = Number(args.limit ?? 9);
|
|
62
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 20) throw errors.argument('limit must be between 1 and 20');
|
|
63
|
+
const body = await eventForm(tab, limit, args.cursor || null);
|
|
64
|
+
const response = await tab.evaluate(`(async () => {
|
|
65
|
+
const response = await fetch('/api/graphql/', { method: 'POST', credentials: 'include',
|
|
66
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: ${JSON.stringify(body)} });
|
|
67
|
+
if (!response.ok) return { httpError: response.status };
|
|
68
|
+
const text = await response.text();
|
|
69
|
+
try { return JSON.parse(text.split(String.fromCharCode(10))[0].replace(/^for\\s*\\(;;\\);/, '')); }
|
|
70
|
+
catch { return { malformed: true }; }
|
|
71
|
+
})()`);
|
|
72
|
+
if (response?.httpError) throw errors.upstream(`Facebook events GraphQL returned HTTP ${response.httpError}`);
|
|
73
|
+
if (response?.malformed) throw errors.upstream('Facebook events GraphQL returned malformed JSON');
|
|
74
|
+
const { rows, pageInfo } = mapEvents(response);
|
|
75
|
+
return { rows, ...(pageInfo?.has_next_page && pageInfo?.end_cursor ? { nextCursor: pageInfo.end_cursor } : {}) };
|
|
76
|
+
},
|
|
77
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { safeFacebookUrl } from './_shared.js';
|
|
3
|
+
|
|
4
|
+
const ORIGIN = 'https://www.facebook.com';
|
|
5
|
+
const INITIAL = 'CometModernHomeFeedQuery';
|
|
6
|
+
const PAGE = 'CometNewsFeedPaginationQuery';
|
|
7
|
+
|
|
8
|
+
export function mapFeedEdges(edges) {
|
|
9
|
+
return edges.flatMap((edge) => {
|
|
10
|
+
const node = edge?.node;
|
|
11
|
+
if (node?.__typename !== 'Story') return [];
|
|
12
|
+
const content = node.comet_sections?.content?.story || node;
|
|
13
|
+
const message = content?.comet_sections?.message?.story || content;
|
|
14
|
+
const id = String(node.post_id || content.post_id || node.id || '').trim();
|
|
15
|
+
if (!id) return [];
|
|
16
|
+
const timestamp = Number(node.creation_time || content.creation_time || 0);
|
|
17
|
+
const actor = content.actors?.[0] || node.actors?.[0];
|
|
18
|
+
return [{
|
|
19
|
+
id,
|
|
20
|
+
author: String(actor?.name || '').trim() || null,
|
|
21
|
+
author_id: String(actor?.id || '').trim() || null,
|
|
22
|
+
text: String(content.message?.text || message?.message?.text || '').trim().slice(0, 5000) || null,
|
|
23
|
+
url: safeFacebookUrl(node.permalink_url || content.wwwURL || content.permalink_url),
|
|
24
|
+
created_at: Number.isFinite(timestamp) && timestamp > 0
|
|
25
|
+
? new Date(timestamp < 1e10 ? timestamp * 1000 : timestamp).toISOString() : null,
|
|
26
|
+
}];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function bootstrap(tab) {
|
|
31
|
+
await tab.goto(`${ORIGIN}/`, { waitUntil: 'load' });
|
|
32
|
+
await tab.network.start('/api/graphql/');
|
|
33
|
+
let cursor = (await tab.network.read({ pattern: '/api/graphql/', limit: 1000 })).cursor;
|
|
34
|
+
await tab.goto(`${ORIGIN}/?browsermcp_feed=${Date.now()}`, { waitUntil: 'load' });
|
|
35
|
+
let entry;
|
|
36
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
37
|
+
const page = await tab.network.read({ pattern: '/api/graphql/', afterSequence: cursor, limit: 100 });
|
|
38
|
+
cursor = page.cursor;
|
|
39
|
+
entry = page.entries.find((item) => item.method === 'POST' && !item.requestBodyTruncated &&
|
|
40
|
+
new URLSearchParams(item.requestBodyPreview || '').get('doc_id'));
|
|
41
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
42
|
+
}
|
|
43
|
+
if (!entry) throw errors.upstream('Facebook did not issue a GraphQL request needed for the feed');
|
|
44
|
+
const form = new URLSearchParams(entry.requestBodyPreview);
|
|
45
|
+
const operations = await tab.evaluate(`(async () => {
|
|
46
|
+
const initial = require('${INITIAL}_facebookRelayOperation');
|
|
47
|
+
const page = await new Promise((resolve, reject) => {
|
|
48
|
+
const timer = setTimeout(() => reject(Error('pagination operation unavailable')), 5000);
|
|
49
|
+
require('Bootloader').loadModules(['${PAGE}_facebookRelayOperation'], (id) => { clearTimeout(timer); resolve(id); }, 'browsermcp-feed');
|
|
50
|
+
});
|
|
51
|
+
const variables = require('CometHomeRootEntryPointVariables').getCometFeedVariablesForSk(undefined, undefined, 'COLD_START');
|
|
52
|
+
const providers = require('${INITIAL}$Parameters').params.providedVariables;
|
|
53
|
+
for (const [key, provider] of Object.entries(providers)) variables[key] = provider.get();
|
|
54
|
+
return { initial, page, variables };
|
|
55
|
+
})()`);
|
|
56
|
+
if (!/^\d+$/.test(String(operations?.initial)) || !/^\d+$/.test(String(operations?.page))) {
|
|
57
|
+
throw errors.upstream('Facebook did not expose the current feed GraphQL operations');
|
|
58
|
+
}
|
|
59
|
+
return { form, ...operations };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function fetchFeed(tab, form, name, id, variables) {
|
|
63
|
+
form.set('fb_api_req_friendly_name', name);
|
|
64
|
+
form.set('doc_id', id);
|
|
65
|
+
form.set('variables', JSON.stringify(variables));
|
|
66
|
+
const body = form.toString();
|
|
67
|
+
const response = await tab.evaluate(`(async () => {
|
|
68
|
+
const response = await fetch('/api/graphql/', { method: 'POST', credentials: 'include',
|
|
69
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: ${JSON.stringify(body)} });
|
|
70
|
+
if (!response.ok) return { httpError: response.status };
|
|
71
|
+
const text = await response.text();
|
|
72
|
+
const chunks = text.split(String.fromCharCode(10)).filter(Boolean).map((line) => {
|
|
73
|
+
try { return JSON.parse(line.replace(/^for\\s*\\(;;\\);/, '')); } catch { return null; }
|
|
74
|
+
}).filter(Boolean);
|
|
75
|
+
const firstError = chunks.find((chunk) => chunk.errors?.length)?.errors?.[0]?.message;
|
|
76
|
+
const initialEdges = chunks[0]?.data?.viewer?.news_feed?.edges;
|
|
77
|
+
if (!Array.isArray(initialEdges)) return { malformed: true, firstError };
|
|
78
|
+
const edges = [...initialEdges];
|
|
79
|
+
let pageInfo = chunks[0]?.data?.viewer?.news_feed?.page_info || null;
|
|
80
|
+
for (const chunk of chunks.slice(1)) {
|
|
81
|
+
const path = chunk.path || [];
|
|
82
|
+
if (path.length === 4 && path[0] === 'viewer' && path[1] === 'news_feed' && path[2] === 'edges' && Number.isInteger(path[3])) {
|
|
83
|
+
edges[path[3]] = chunk.data;
|
|
84
|
+
} else if (path.length === 2 && path[0] === 'viewer' && path[1] === 'news_feed' && chunk.data?.page_info) {
|
|
85
|
+
pageInfo = chunk.data.page_info;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return { edges: edges.filter(Boolean).map((edge) => ({ node: (() => {
|
|
89
|
+
const node = edge.node;
|
|
90
|
+
if (!node) return null;
|
|
91
|
+
const content = node.comet_sections?.content?.story;
|
|
92
|
+
const nested = content?.comet_sections?.message?.story;
|
|
93
|
+
return { __typename: node.__typename, id: node.id, post_id: node.post_id,
|
|
94
|
+
permalink_url: node.permalink_url, creation_time: node.creation_time,
|
|
95
|
+
actors: node.actors, comet_sections: { content: { story: content ? {
|
|
96
|
+
id: content.id, post_id: content.post_id, wwwURL: content.wwwURL,
|
|
97
|
+
permalink_url: content.permalink_url, actors: content.actors,
|
|
98
|
+
message: content.message, comet_sections: { message: { story: nested ? { message: nested.message } : null } },
|
|
99
|
+
} : null } }, };
|
|
100
|
+
})() })), pageInfo, firstError };
|
|
101
|
+
})()`);
|
|
102
|
+
if (response?.httpError) throw errors.upstream(`Facebook feed API returned HTTP ${response.httpError}`);
|
|
103
|
+
if (response?.firstError) throw errors.upstream(`Facebook feed API: ${response.firstError}`);
|
|
104
|
+
if (response?.malformed || !Array.isArray(response?.edges) || !response.pageInfo) {
|
|
105
|
+
throw errors.upstream('Facebook feed API response changed shape');
|
|
106
|
+
}
|
|
107
|
+
return response;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default defineAdapter({
|
|
111
|
+
description: 'Read Facebook news feed posts through its GraphQL API.',
|
|
112
|
+
access: 'read', domain: 'facebook.com',
|
|
113
|
+
result: { kind: 'rows', description: 'News feed posts', paginated: true },
|
|
114
|
+
args: [
|
|
115
|
+
{ name: 'limit', type: 'int', default: 10, min: 1, max: 50 },
|
|
116
|
+
{ name: 'cursor', type: 'string', help: 'nextCursor from a previous page' },
|
|
117
|
+
],
|
|
118
|
+
async run({ tab, args }) {
|
|
119
|
+
const limit = Number(args.limit ?? 10);
|
|
120
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 50) throw errors.argument('limit must be between 1 and 50');
|
|
121
|
+
const { form, initial, page, variables } = await bootstrap(tab);
|
|
122
|
+
const seen = new Set();
|
|
123
|
+
const rows = [];
|
|
124
|
+
let cursor = args.cursor ? String(args.cursor) : null;
|
|
125
|
+
let more = true;
|
|
126
|
+
for (let request = 0; request < 20 && rows.length < limit && more; request++) {
|
|
127
|
+
let result;
|
|
128
|
+
if (!cursor) {
|
|
129
|
+
result = await fetchFeed(tab, form, INITIAL, initial, { ...variables, feedInitialFetchSize: 4 });
|
|
130
|
+
} else {
|
|
131
|
+
const { feedInitialFetchSize, ...rest } = variables;
|
|
132
|
+
result = await fetchFeed(tab, form, PAGE, page, { ...rest, count: Math.min(10, limit - rows.length + 2),
|
|
133
|
+
cursor, clientQueryId: crypto.randomUUID(), clientSession: null,
|
|
134
|
+
experimentalValues: null, focusCommentID: null, referringStoryRenderLocation: null,
|
|
135
|
+
refreshMode: 'AUTO' });
|
|
136
|
+
}
|
|
137
|
+
for (const row of mapFeedEdges(result.edges)) {
|
|
138
|
+
if (!seen.has(row.id)) { rows.push(row); seen.add(row.id); }
|
|
139
|
+
}
|
|
140
|
+
const next = result.pageInfo?.end_cursor || null;
|
|
141
|
+
more = Boolean(result.pageInfo?.has_next_page && next && next !== cursor);
|
|
142
|
+
cursor = next;
|
|
143
|
+
}
|
|
144
|
+
return { rows: rows.slice(0, limit), ...(more && cursor ? { nextCursor: cursor } : {}) };
|
|
145
|
+
},
|
|
146
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
|
|
3
|
+
const PAGE = 'FriendingCometPYMKGridPaginationQuery';
|
|
4
|
+
|
|
5
|
+
export function mapFriendSuggestions(response) {
|
|
6
|
+
if (response?.errors?.length) throw errors.upstream(`Facebook friend suggestions API: ${String(response.errors[0]?.message || 'GraphQL error')}`);
|
|
7
|
+
const grid = response?.data?.viewer?.pymk_grid;
|
|
8
|
+
if (!Array.isArray(grid?.edges)) throw errors.upstream('Facebook friend suggestions API response changed shape');
|
|
9
|
+
const rows = grid.edges.flatMap(({ node }) => {
|
|
10
|
+
const id = String(node?.id || '').trim();
|
|
11
|
+
const name = String(node?.name || '').trim();
|
|
12
|
+
if (!id || !name) return [];
|
|
13
|
+
const context = String(node.social_context?.text || '').trim();
|
|
14
|
+
const count = context.match(/([\d,]+)\s*(?:位)?(?:共同|mutual)/i)?.[1];
|
|
15
|
+
return [{ id, name, url: `https://www.facebook.com/profile.php?id=${encodeURIComponent(id)}`,
|
|
16
|
+
mutual_count: count ? Number(count.replaceAll(',', '')) : null,
|
|
17
|
+
mutual_context: context || null,
|
|
18
|
+
picture: typeof node.profile_picture?.uri === 'string' ? node.profile_picture.uri : null,
|
|
19
|
+
friendship_status: String(node.friendship_status || '') || null }];
|
|
20
|
+
});
|
|
21
|
+
return { rows, pageInfo: grid.page_info || null };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function friendsTemplate(tab) {
|
|
25
|
+
await tab.goto('https://www.facebook.com/friends', { waitUntil: 'load' });
|
|
26
|
+
await tab.network.start('/api/graphql/');
|
|
27
|
+
let cursor = (await tab.network.read({ pattern: '/api/graphql/', limit: 1000 })).cursor;
|
|
28
|
+
await tab.goto(`https://www.facebook.com/friends?browsermcp_probe=${Date.now()}`, { waitUntil: 'load' });
|
|
29
|
+
let entry;
|
|
30
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
31
|
+
const page = await tab.network.read({ pattern: '/api/graphql/', afterSequence: cursor, limit: 100 });
|
|
32
|
+
cursor = page.cursor;
|
|
33
|
+
entry = page.entries.find((item) => item.method === 'POST' && !item.requestBodyTruncated &&
|
|
34
|
+
new URLSearchParams(item.requestBodyPreview || '').get('fb_api_req_friendly_name'));
|
|
35
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
36
|
+
}
|
|
37
|
+
if (!entry) throw errors.upstream('Facebook did not issue a GraphQL request needed for friend suggestions');
|
|
38
|
+
const id = await tab.evaluate(`require('${PAGE}_facebookRelayOperation')`);
|
|
39
|
+
if (!/^\d+$/.test(String(id || ''))) throw errors.upstream('Facebook did not load the friending GraphQL operation');
|
|
40
|
+
return { form: new URLSearchParams(entry.requestBodyPreview), id };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default defineAdapter({
|
|
44
|
+
description: 'Get Facebook friend suggestions through its GraphQL API.',
|
|
45
|
+
access: 'read', domain: 'facebook.com',
|
|
46
|
+
result: { kind: 'rows', description: 'Friend suggestions', paginated: true },
|
|
47
|
+
args: [
|
|
48
|
+
{ name: 'limit', type: 'int', default: 10, min: 1, max: 20 },
|
|
49
|
+
{ name: 'cursor', type: 'string', help: 'nextCursor from a previous page' },
|
|
50
|
+
],
|
|
51
|
+
async run({ tab, args }) {
|
|
52
|
+
const limit = Number(args.limit ?? 10);
|
|
53
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 20) throw errors.argument('limit must be between 1 and 20');
|
|
54
|
+
const cursor = args.cursor ? String(args.cursor) : null;
|
|
55
|
+
const { form, id } = await friendsTemplate(tab);
|
|
56
|
+
form.set('fb_api_req_friendly_name', PAGE);
|
|
57
|
+
form.set('doc_id', id);
|
|
58
|
+
form.set('variables', JSON.stringify({ count: limit, cursor, location: 'FRIENDS_HOME_MAIN', scale: 1 }));
|
|
59
|
+
const response = await tab.evaluate(`(async () => {
|
|
60
|
+
const response = await fetch('/api/graphql/', { method: 'POST', credentials: 'include',
|
|
61
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: ${JSON.stringify(form.toString())} });
|
|
62
|
+
if (!response.ok) return { httpError: response.status };
|
|
63
|
+
const text = await response.text();
|
|
64
|
+
try { return JSON.parse(text.split(String.fromCharCode(10))[0].replace(/^for\\s*\\(;;\\);/, '')); }
|
|
65
|
+
catch { return { malformed: true }; }
|
|
66
|
+
})()`);
|
|
67
|
+
if (response?.httpError) throw errors.upstream(`Facebook friends GraphQL returned HTTP ${response.httpError}`);
|
|
68
|
+
if (response?.malformed) throw errors.upstream('Facebook friends GraphQL returned malformed JSON');
|
|
69
|
+
const { rows, pageInfo } = mapFriendSuggestions(response);
|
|
70
|
+
return { rows, ...(pageInfo?.has_next_page && pageInfo?.end_cursor ? { nextCursor: pageInfo.end_cursor } : {}) };
|
|
71
|
+
},
|
|
72
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { defineAdapter, errors } from '@jackwener/browsermcp/adapter-sdk';
|
|
2
|
+
import { safeFacebookUrl } from './_shared.js';
|
|
3
|
+
|
|
4
|
+
const OPERATION = 'GroupsCometLeftRailContainerQuery';
|
|
5
|
+
|
|
6
|
+
export function mapGroups(response, kind = 'all') {
|
|
7
|
+
if (response?.errors?.length) throw errors.upstream(`Facebook groups API: ${String(response.errors[0]?.message || 'GraphQL error')}`);
|
|
8
|
+
const collections = [
|
|
9
|
+
['member', response?.data?.nonAdminGroups?.groups_tab?.tab_groups_list?.edges],
|
|
10
|
+
['managed', response?.data?.adminGroups?.groups_tab?.tab_groups_list?.edges],
|
|
11
|
+
];
|
|
12
|
+
if (collections.some(([, edges]) => !Array.isArray(edges))) throw errors.upstream('Facebook groups API response changed shape');
|
|
13
|
+
return collections.flatMap(([groupKind, edges]) => kind !== 'all' && kind !== groupKind ? [] : edges.flatMap(({ node }) => {
|
|
14
|
+
const id = String(node?.id || '').trim();
|
|
15
|
+
const name = String(node?.name || '').trim();
|
|
16
|
+
if (!id || !name) return [];
|
|
17
|
+
const timestamp = Number(node.last_post_time);
|
|
18
|
+
const ms = timestamp < 1e10 ? timestamp * 1000 : timestamp;
|
|
19
|
+
return [{ id, name, kind: groupKind,
|
|
20
|
+
url: safeFacebookUrl(node.url) || `https://www.facebook.com/groups/${encodeURIComponent(id)}/`,
|
|
21
|
+
last_post_at: Number.isFinite(ms) && ms > 0 ? new Date(ms).toISOString() : null }];
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default defineAdapter({
|
|
26
|
+
description: 'List joined and managed Facebook groups through its GraphQL API.',
|
|
27
|
+
access: 'read', domain: 'facebook.com',
|
|
28
|
+
result: { kind: 'rows', description: 'Groups' },
|
|
29
|
+
args: [
|
|
30
|
+
{ name: 'limit', type: 'int', default: 20, min: 1, max: 50 },
|
|
31
|
+
{ name: 'kind', type: 'string', default: 'all', choices: ['all', 'member', 'managed'] },
|
|
32
|
+
],
|
|
33
|
+
async run({ tab, args }) {
|
|
34
|
+
const limit = Number(args.limit ?? 20);
|
|
35
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 50) throw errors.argument('limit must be between 1 and 50');
|
|
36
|
+
const kind = String(args.kind || 'all');
|
|
37
|
+
if (!['all', 'member', 'managed'].includes(kind)) throw errors.argument('kind must be all, member, or managed');
|
|
38
|
+
await tab.goto('https://www.facebook.com/groups/feed/', { waitUntil: 'load' });
|
|
39
|
+
await tab.network.start('/api/graphql/');
|
|
40
|
+
let cursor = (await tab.network.read({ pattern: '/api/graphql/', limit: 1000 })).cursor;
|
|
41
|
+
await tab.goto(`https://www.facebook.com/groups/feed/?browsermcp_probe=${Date.now()}`, { waitUntil: 'load' });
|
|
42
|
+
let entry;
|
|
43
|
+
for (let attempt = 0; attempt < 24 && !entry; attempt++) {
|
|
44
|
+
const page = await tab.network.read({ pattern: '/api/graphql/', afterSequence: cursor, limit: 100 });
|
|
45
|
+
cursor = page.cursor;
|
|
46
|
+
entry = page.entries.find((item) => item.method === 'POST' && !item.requestBodyTruncated &&
|
|
47
|
+
new URLSearchParams(item.requestBodyPreview || '').get('fb_api_req_friendly_name'));
|
|
48
|
+
if (!entry) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
49
|
+
}
|
|
50
|
+
if (!entry) throw errors.upstream('Facebook did not issue a GraphQL request needed for groups');
|
|
51
|
+
const id = await tab.evaluate(`require('${OPERATION}_facebookRelayOperation')`);
|
|
52
|
+
if (!/^\d+$/.test(String(id || ''))) throw errors.upstream('Facebook did not load the groups GraphQL operation');
|
|
53
|
+
const form = new URLSearchParams(entry.requestBodyPreview);
|
|
54
|
+
form.set('fb_api_req_friendly_name', OPERATION);
|
|
55
|
+
form.set('doc_id', id);
|
|
56
|
+
form.set('variables', JSON.stringify({ adminGroupsCount: limit, memberGroupsCount: limit, scale: 1 }));
|
|
57
|
+
const response = await tab.evaluate(`(async () => {
|
|
58
|
+
const response = await fetch('/api/graphql/', { method: 'POST', credentials: 'include',
|
|
59
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: ${JSON.stringify(form.toString())} });
|
|
60
|
+
if (!response.ok) return { httpError: response.status };
|
|
61
|
+
const text = await response.text();
|
|
62
|
+
try { return JSON.parse(text.split(String.fromCharCode(10))[0].replace(/^for\\s*\\(;;\\);/, '')); }
|
|
63
|
+
catch { return { malformed: true }; }
|
|
64
|
+
})()`);
|
|
65
|
+
if (response?.httpError) throw errors.upstream(`Facebook groups GraphQL returned HTTP ${response.httpError}`);
|
|
66
|
+
if (response?.malformed) throw errors.upstream('Facebook groups GraphQL returned malformed JSON');
|
|
67
|
+
return { rows: mapGroups(response, kind).slice(0, limit) };
|
|
68
|
+
},
|
|
69
|
+
});
|