Skip to content

Commit 6646943

Browse files
author
Sentience Dev
committed
prep extension
1 parent 3ff1f14 commit 6646943

File tree

9 files changed

+1168
-0
lines changed

9 files changed

+1168
-0
lines changed

sentience/extension/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Sentience Chrome Extension (Embedded)
2+
3+
This directory contains the Sentience Chrome extension files bundled with the Python SDK.
4+
5+
## Auto-Sync
6+
7+
These files are automatically synced from the [sentience-chrome](https://github.com/rcholic/Sentience-Geometry-Chrome-Extension) repository when new releases are published.
8+
9+
## Files
10+
11+
- `manifest.json` - Chrome extension manifest
12+
- `content.js` - Content script injected into web pages
13+
- `background.js` - Background service worker
14+
- `injected_api.js` - API injected into page context
15+
- `pkg/` - WebAssembly bindings and core logic
16+
- `sentience_core.js` - WASM JavaScript bindings
17+
- `sentience_core_bg.wasm` - Compiled WebAssembly binary
18+
- `*.d.ts` - TypeScript definitions
19+
20+
## Usage
21+
22+
The extension is automatically loaded by `SentienceBrowser` when you create a browser instance:
23+
24+
```python
25+
from sentience import SentienceBrowser
26+
27+
with SentienceBrowser() as browser:
28+
browser.page.goto("https://example.com")
29+
# Extension is automatically loaded and active
30+
```
31+
32+
## Development
33+
34+
For local development, you can use the development extension at `../sentience-chrome/` instead. The SDK will automatically detect and use it if available.
35+
36+
## Manual Update
37+
38+
If you need to manually update the extension files:
39+
40+
```bash
41+
# Copy from sentience-chrome repo
42+
cp ../sentience-chrome/manifest.json sentience/extension/
43+
cp ../sentience-chrome/*.js sentience/extension/
44+
cp -r ../sentience-chrome/pkg sentience/extension/
45+
```
46+
47+
Or trigger the sync workflow manually:
48+
49+
```bash
50+
gh workflow run sync-extension.yml -f release_tag=v1.0.0
51+
```

sentience/extension/background.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// background.js - Service Worker for screenshot capture
2+
// Chrome extensions can only capture screenshots from the background script
3+
// Listen for screenshot requests from content script
4+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
5+
if (request.action === 'captureScreenshot') {
6+
handleScreenshotCapture(sender.tab.id, request.options)
7+
.then(screenshot => {
8+
sendResponse({ success: true, screenshot });
9+
})
10+
.catch(error => {
11+
console.error('[Sentience] Screenshot capture failed:', error);
12+
sendResponse({
13+
success: false,
14+
error: error.message || 'Screenshot capture failed'
15+
});
16+
});
17+
18+
// Return true to indicate we'll send response asynchronously
19+
return true;
20+
}
21+
});
22+
23+
/**
24+
* Capture screenshot of the active tab
25+
* @param {number} tabId - Tab ID to capture
26+
* @param {Object} options - Screenshot options
27+
* @returns {Promise<string>} Base64-encoded PNG data URL
28+
*/
29+
async function handleScreenshotCapture(tabId, options = {}) {
30+
try {
31+
const {
32+
format = 'png', // 'png' or 'jpeg'
33+
quality = 90 // JPEG quality (0-100), ignored for PNG
34+
} = options;
35+
36+
// Capture visible tab as data URL
37+
const dataUrl = await chrome.tabs.captureVisibleTab(null, {
38+
format: format,
39+
quality: quality
40+
});
41+
42+
console.log(`[Sentience] Screenshot captured: ${format}, size: ${dataUrl.length} bytes`);
43+
44+
return dataUrl;
45+
} catch (error) {
46+
console.error('[Sentience] Screenshot error:', error);
47+
throw new Error(`Failed to capture screenshot: ${error.message}`);
48+
}
49+
}
50+
51+
/**
52+
* Optional: Add viewport-specific capture (requires additional setup)
53+
* This would allow capturing specific regions, not just visible area
54+
*/
55+
async function captureRegion(tabId, region) {
56+
// For region capture, you'd need to:
57+
// 1. Capture full visible tab
58+
// 2. Use Canvas API to crop to region
59+
// 3. Return cropped image
60+
61+
// Not implemented in this basic version
62+
throw new Error('Region capture not yet implemented');
63+
}

sentience/extension/content.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// content.js - ISOLATED WORLD
2+
console.log('[Sentience] Bridge loaded.');
3+
4+
// 1. Pass Extension ID to Main World (So WASM knows where to load from)
5+
document.documentElement.dataset.sentienceExtensionId = chrome.runtime.id;
6+
7+
// 2. Proxy for Screenshots (The only thing Isolated World needs to do)
8+
window.addEventListener('message', (event) => {
9+
// Security check: only accept messages from same window
10+
if (event.source !== window || event.data.type !== 'SENTIENCE_SCREENSHOT_REQUEST') return;
11+
12+
chrome.runtime.sendMessage(
13+
{ action: 'captureScreenshot', options: event.data.options },
14+
(response) => {
15+
window.postMessage({
16+
type: 'SENTIENCE_SCREENSHOT_RESULT',
17+
requestId: event.data.requestId,
18+
screenshot: response?.success ? response.screenshot : null
19+
}, '*');
20+
}
21+
);
22+
});

0 commit comments

Comments
 (0)