forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
105 lines (91 loc) · 2.79 KB
/
content.js
File metadata and controls
105 lines (91 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Agent OS Chrome Extension - Content Script
*
* Injected into pages to detect and communicate with Agent OS instances.
* Acts as a bridge between the page context and the extension.
*/
// Connect to background script
let port = null;
function connectToBackground() {
port = chrome.runtime.connect({ name: 'content-script' });
port.onMessage.addListener((message) => {
// Forward messages from DevTools to page
if (message.type === 'devtools-connected') {
window.postMessage({ source: 'agent-os-extension', type: 'devtools-connected' }, '*');
}
});
port.onDisconnect.addListener(() => {
// Reconnect after a delay
setTimeout(connectToBackground, 1000);
});
}
connectToBackground();
// Listen for messages from the page
window.addEventListener('message', (event) => {
// Only accept messages from our page
if (event.source !== window) return;
// Handle messages from Agent OS
if (event.data.source === 'agent-os-page') {
// Forward to background (and then to DevTools)
if (port) {
port.postMessage({
type: event.data.event,
data: event.data.data
});
}
}
});
// Handle extension messages
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'check-agent-os') {
// Check if Agent OS is present on the page
const checkScript = `
(function() {
if (window.__AGENT_OS__) {
return {
detected: true,
version: window.__AGENT_OS__.version || 'unknown'
};
}
return { detected: false };
})()
`;
// Inject and execute
const script = document.createElement('script');
script.textContent = `
window.postMessage({
source: 'agent-os-check',
result: ${checkScript}
}, '*');
`;
document.documentElement.appendChild(script);
script.remove();
// Listen for result
const handler = (event) => {
if (event.data.source === 'agent-os-check') {
window.removeEventListener('message', handler);
sendResponse(event.data.result);
}
};
window.addEventListener('message', handler);
return true; // Keep channel open
}
});
// Inject the page-level script that hooks into Agent OS
function injectPageScript() {
const script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
}
// Inject when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', injectPageScript);
} else {
injectPageScript();
}
console.log('[Agent OS] Content script loaded');