Skip to content

Commit 96339b6

Browse files
author
SentienceDEV
committed
Merge pull request #38 from SentienceAPI/sync-extension-v0.10.3
Sync Extension: v0.10.3
2 parents f5cbd73 + 3e849c3 commit 96339b6

File tree

6 files changed

+673
-0
lines changed

6 files changed

+673
-0
lines changed

sentience-chrome/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-chrome/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)