btcp-browser-agent 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -9
- package/packages/core/dist/actions.d.ts +97 -0
- package/packages/core/dist/actions.js +940 -0
- package/packages/core/dist/errors.d.ts +138 -0
- package/packages/core/dist/errors.js +157 -0
- package/packages/core/dist/index.d.ts +120 -0
- package/packages/core/dist/index.js +134 -0
- package/packages/core/dist/ref-map.d.ts +16 -0
- package/packages/core/dist/ref-map.js +91 -0
- package/packages/core/dist/snapshot.d.ts +37 -0
- package/packages/core/dist/snapshot.js +751 -0
- package/packages/core/dist/types.d.ts +396 -0
- package/packages/core/dist/types.js +7 -0
- package/packages/extension/dist/background.d.ts +227 -0
- package/packages/extension/dist/background.js +737 -0
- package/packages/extension/dist/content.d.ts +18 -0
- package/packages/extension/dist/content.js +149 -0
- package/packages/extension/dist/index.d.ts +228 -0
- package/packages/extension/dist/index.js +350 -0
- package/packages/extension/dist/session-manager.d.ts +87 -0
- package/packages/extension/dist/session-manager.js +322 -0
- package/packages/extension/{src/session-types.ts → dist/session-types.d.ts} +113 -144
- package/packages/extension/dist/session-types.js +5 -0
- package/packages/extension/dist/types.d.ts +88 -0
- package/packages/extension/dist/types.js +7 -0
- package/CLAUDE.md +0 -230
- package/SKILL.md +0 -143
- package/SNAPSHOT_IMPROVEMENTS.md +0 -302
- package/USAGE.md +0 -146
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/docs/browser-cli-design.md +0 -500
- package/examples/chrome-extension/CHANGELOG.md +0 -210
- package/examples/chrome-extension/DEBUG.md +0 -231
- package/examples/chrome-extension/ERROR_FIXED.md +0 -147
- package/examples/chrome-extension/QUICK_TEST.md +0 -189
- package/examples/chrome-extension/README.md +0 -149
- package/examples/chrome-extension/SESSION_ONLY_MODE.md +0 -305
- package/examples/chrome-extension/TEST_WITH_YOUR_TABS.md +0 -97
- package/examples/chrome-extension/build.js +0 -43
- package/examples/chrome-extension/manifest.json +0 -37
- package/examples/chrome-extension/package-lock.json +0 -1063
- package/examples/chrome-extension/package.json +0 -21
- package/examples/chrome-extension/popup.html +0 -195
- package/examples/chrome-extension/src/background.ts +0 -12
- package/examples/chrome-extension/src/content.ts +0 -7
- package/examples/chrome-extension/src/popup.ts +0 -303
- package/examples/chrome-extension/src/scenario-google-github.ts +0 -389
- package/examples/chrome-extension/test-page.html +0 -127
- package/examples/chrome-extension/tests/README.md +0 -206
- package/examples/chrome-extension/tests/scenario-google-to-github-star.ts +0 -380
- package/examples/chrome-extension/tsconfig.json +0 -14
- package/examples/snapshots/README.md +0 -207
- package/examples/snapshots/amazon-com-detail.html +0 -9528
- package/examples/snapshots/amazon-com-detail.snapshot.txt +0 -997
- package/examples/snapshots/convert-snapshots.ts +0 -97
- package/examples/snapshots/edition-cnn-com.html +0 -13292
- package/examples/snapshots/edition-cnn-com.snapshot.txt +0 -562
- package/examples/snapshots/github-com-microsoft-vscode.html +0 -2916
- package/examples/snapshots/github-com-microsoft-vscode.snapshot.txt +0 -455
- package/examples/snapshots/google-search.html +0 -20012
- package/examples/snapshots/google-search.snapshot.txt +0 -195
- package/examples/snapshots/metadata.json +0 -86
- package/examples/snapshots/npr-org-templates.html +0 -2031
- package/examples/snapshots/npr-org-templates.snapshot.txt +0 -224
- package/examples/snapshots/stackoverflow-com.html +0 -5216
- package/examples/snapshots/stackoverflow-com.snapshot.txt +0 -2404
- package/examples/snapshots/test-all-mode.html +0 -46
- package/examples/snapshots/test-all-mode.snapshot.txt +0 -5
- package/examples/snapshots/validate.test.ts +0 -296
- package/packages/cli/package.json +0 -42
- package/packages/cli/src/__tests__/cli.test.ts +0 -434
- package/packages/cli/src/__tests__/errors.test.ts +0 -226
- package/packages/cli/src/__tests__/executor.test.ts +0 -275
- package/packages/cli/src/__tests__/formatter.test.ts +0 -260
- package/packages/cli/src/__tests__/parser.test.ts +0 -288
- package/packages/cli/src/__tests__/suggestions.test.ts +0 -255
- package/packages/cli/src/commands/back.ts +0 -22
- package/packages/cli/src/commands/check.ts +0 -33
- package/packages/cli/src/commands/clear.ts +0 -33
- package/packages/cli/src/commands/click.ts +0 -32
- package/packages/cli/src/commands/closetab.ts +0 -31
- package/packages/cli/src/commands/eval.ts +0 -41
- package/packages/cli/src/commands/fill.ts +0 -30
- package/packages/cli/src/commands/focus.ts +0 -33
- package/packages/cli/src/commands/forward.ts +0 -22
- package/packages/cli/src/commands/goto.ts +0 -34
- package/packages/cli/src/commands/help.ts +0 -162
- package/packages/cli/src/commands/hover.ts +0 -34
- package/packages/cli/src/commands/index.ts +0 -129
- package/packages/cli/src/commands/newtab.ts +0 -35
- package/packages/cli/src/commands/press.ts +0 -40
- package/packages/cli/src/commands/reload.ts +0 -25
- package/packages/cli/src/commands/screenshot.ts +0 -27
- package/packages/cli/src/commands/scroll.ts +0 -64
- package/packages/cli/src/commands/select.ts +0 -35
- package/packages/cli/src/commands/snapshot.ts +0 -21
- package/packages/cli/src/commands/tab.ts +0 -32
- package/packages/cli/src/commands/tabs.ts +0 -26
- package/packages/cli/src/commands/text.ts +0 -27
- package/packages/cli/src/commands/title.ts +0 -17
- package/packages/cli/src/commands/type.ts +0 -38
- package/packages/cli/src/commands/uncheck.ts +0 -33
- package/packages/cli/src/commands/url.ts +0 -17
- package/packages/cli/src/commands/wait.ts +0 -54
- package/packages/cli/src/errors.ts +0 -164
- package/packages/cli/src/executor.ts +0 -68
- package/packages/cli/src/formatter.ts +0 -215
- package/packages/cli/src/index.ts +0 -257
- package/packages/cli/src/parser.ts +0 -195
- package/packages/cli/src/suggestions.ts +0 -207
- package/packages/cli/src/terminal/Terminal.ts +0 -365
- package/packages/cli/src/terminal/index.ts +0 -5
- package/packages/cli/src/types.ts +0 -155
- package/packages/cli/tsconfig.json +0 -20
- package/packages/core/package.json +0 -35
- package/packages/core/src/actions.ts +0 -1210
- package/packages/core/src/errors.ts +0 -296
- package/packages/core/src/index.test.ts +0 -638
- package/packages/core/src/index.ts +0 -220
- package/packages/core/src/ref-map.ts +0 -107
- package/packages/core/src/snapshot.ts +0 -873
- package/packages/core/src/types.ts +0 -536
- package/packages/core/tsconfig.json +0 -23
- package/packages/extension/README.md +0 -129
- package/packages/extension/package.json +0 -43
- package/packages/extension/src/background.ts +0 -888
- package/packages/extension/src/content.ts +0 -172
- package/packages/extension/src/index.ts +0 -579
- package/packages/extension/src/session-manager.ts +0 -385
- package/packages/extension/src/types.ts +0 -162
- package/packages/extension/tsconfig.json +0 -28
- package/src/index.ts +0 -64
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -26
- package/vitest.config.ts +0 -13
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @btcp/extension - Content Script
|
|
3
|
-
*
|
|
4
|
-
* Runs in web pages, handles DOM commands from the background script.
|
|
5
|
-
* Uses ContentAgent from @btcp/core for DOM operations.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { createContentAgent, type ContentAgent, type Command as CoreCommand, type Response } from '@btcp/core';
|
|
9
|
-
import type { ExtensionMessage, ExtensionResponse, Command } from './types.js';
|
|
10
|
-
|
|
11
|
-
let agent: ContentAgent | null = null;
|
|
12
|
-
let isContentScriptReady = false;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Get or create the ContentAgent instance for this page
|
|
16
|
-
*/
|
|
17
|
-
function getContentAgent(): ContentAgent {
|
|
18
|
-
if (!agent) {
|
|
19
|
-
agent = createContentAgent(document, window);
|
|
20
|
-
isContentScriptReady = true;
|
|
21
|
-
console.log('[ContentScript] Agent initialized');
|
|
22
|
-
}
|
|
23
|
-
return agent;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Initialize agent immediately
|
|
27
|
-
getContentAgent();
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Check if a command is a core DOM command
|
|
31
|
-
*/
|
|
32
|
-
function isCoreCommand(command: Command): command is CoreCommand {
|
|
33
|
-
const extensionActions = [
|
|
34
|
-
'navigate', 'back', 'forward', 'reload',
|
|
35
|
-
'getUrl', 'getTitle', 'screenshot',
|
|
36
|
-
'tabNew', 'tabClose', 'tabSwitch', 'tabList',
|
|
37
|
-
];
|
|
38
|
-
return !extensionActions.includes(command.action);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Handle a command from the background script
|
|
43
|
-
*/
|
|
44
|
-
async function handleCommand(command: Command): Promise<Response> {
|
|
45
|
-
// Core DOM commands are handled by ContentAgent
|
|
46
|
-
if (isCoreCommand(command)) {
|
|
47
|
-
return getContentAgent().execute(command);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Extension commands that need content script execution
|
|
51
|
-
switch (command.action) {
|
|
52
|
-
case 'getUrl':
|
|
53
|
-
return {
|
|
54
|
-
id: command.id,
|
|
55
|
-
success: true,
|
|
56
|
-
data: { url: window.location.href },
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
case 'getTitle':
|
|
60
|
-
return {
|
|
61
|
-
id: command.id,
|
|
62
|
-
success: true,
|
|
63
|
-
data: { title: document.title },
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
default:
|
|
67
|
-
// Forward to background script
|
|
68
|
-
return {
|
|
69
|
-
id: command.id,
|
|
70
|
-
success: false,
|
|
71
|
-
error: `Command ${command.action} must be handled by background script`,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Listen for messages from background script
|
|
78
|
-
*/
|
|
79
|
-
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|
80
|
-
const msg = message as ExtensionMessage;
|
|
81
|
-
|
|
82
|
-
// Handle ping messages for heartbeat
|
|
83
|
-
if (msg.type === 'btcp:ping') {
|
|
84
|
-
sendResponse({ type: 'btcp:pong', ready: isContentScriptReady } satisfies ExtensionResponse);
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (msg.type !== 'btcp:command') {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
handleCommand(msg.command)
|
|
93
|
-
.then((response) => {
|
|
94
|
-
sendResponse({ type: 'btcp:response', response } satisfies ExtensionResponse);
|
|
95
|
-
})
|
|
96
|
-
.catch((error) => {
|
|
97
|
-
sendResponse({
|
|
98
|
-
type: 'btcp:response',
|
|
99
|
-
response: {
|
|
100
|
-
id: msg.command.id,
|
|
101
|
-
success: false,
|
|
102
|
-
error: error instanceof Error ? error.message : String(error),
|
|
103
|
-
},
|
|
104
|
-
} satisfies ExtensionResponse);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
// Return true to indicate async response
|
|
108
|
-
return true;
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Also listen for postMessage from page scripts
|
|
113
|
-
* This allows scripts injected into the page to use the agent
|
|
114
|
-
*/
|
|
115
|
-
window.addEventListener('message', async (event) => {
|
|
116
|
-
if (event.source !== window) return;
|
|
117
|
-
|
|
118
|
-
const msg = event.data as ExtensionMessage;
|
|
119
|
-
if (msg?.type !== 'btcp:command') return;
|
|
120
|
-
|
|
121
|
-
const response = await handleCommand(msg.command);
|
|
122
|
-
|
|
123
|
-
window.postMessage({
|
|
124
|
-
type: 'btcp:response',
|
|
125
|
-
response,
|
|
126
|
-
} satisfies ExtensionResponse, '*');
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Lifecycle event listeners for session keep-alive
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
// Detect page visibility changes
|
|
134
|
-
document.addEventListener('visibilitychange', () => {
|
|
135
|
-
if (!document.hidden) {
|
|
136
|
-
console.log('[ContentScript] Page became visible, checking connection...');
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// Detect freeze/resume events (Chrome 68+)
|
|
141
|
-
document.addEventListener('freeze', () => {
|
|
142
|
-
console.log('[ContentScript] Page frozen');
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
document.addEventListener('resume', () => {
|
|
146
|
-
console.log('[ContentScript] Page resumed, re-initializing agent...');
|
|
147
|
-
// Re-initialize agent to ensure fresh state
|
|
148
|
-
agent = null;
|
|
149
|
-
getContentAgent();
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// Clear refs on navigation (full page navigation)
|
|
153
|
-
window.addEventListener('beforeunload', () => {
|
|
154
|
-
console.log('[ContentScript] Page navigating, clearing refs...');
|
|
155
|
-
const currentAgent = getContentAgent();
|
|
156
|
-
currentAgent.clearRefs();
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
// Detect SPA navigation (URL changes without full page reload)
|
|
160
|
-
let lastUrl = window.location.href;
|
|
161
|
-
setInterval(() => {
|
|
162
|
-
const currentUrl = window.location.href;
|
|
163
|
-
if (currentUrl !== lastUrl) {
|
|
164
|
-
console.log('[ContentScript] URL changed, clearing refs...', { from: lastUrl, to: currentUrl });
|
|
165
|
-
lastUrl = currentUrl;
|
|
166
|
-
const currentAgent = getContentAgent();
|
|
167
|
-
currentAgent.clearRefs();
|
|
168
|
-
}
|
|
169
|
-
}, 1000);
|
|
170
|
-
|
|
171
|
-
// Export for programmatic use
|
|
172
|
-
export { getContentAgent, handleCommand };
|