Created
November 7, 2024 22:43
-
-
Save greenido/207378a813cfbc658fcf716ddd7b95d2 to your computer and use it in GitHub Desktop.
Chrome Extension to Local Agent Communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// manifest.json | |
{ | |
"name": "Local Agent Connector", | |
"version": "1.0", | |
"manifest_version": 3, | |
"permissions": [ | |
"nativeMessaging", | |
"tabs" | |
], | |
"background": { | |
"service_worker": "background.js" | |
}, | |
"host_permissions": ["http://127.0.0.1:*/*"] | |
} | |
// background.js | |
let port = null; | |
let wsConnection = null; | |
// Method 1: Native Messaging | |
chrome.runtime.onStartup.addListener(() => { | |
// Connect to native host application | |
port = chrome.runtime.connectNative('com.example.local_agent'); | |
port.onMessage.addListener((message) => { | |
console.log('Received from native app:', message); | |
}); | |
port.onDisconnect.addListener(() => { | |
console.log('Disconnected from native app'); | |
port = null; | |
}); | |
}); | |
// Method 2: WebSocket Connection | |
function connectWebSocket() { | |
wsConnection = new WebSocket('ws://127.0.0.1:8080'); | |
wsConnection.onopen = () => { | |
console.log('WebSocket Connected'); | |
}; | |
wsConnection.onmessage = (event) => { | |
console.log('Received from WebSocket:', event.data); | |
}; | |
wsConnection.onclose = () => { | |
console.log('WebSocket closed, attempting reconnect...'); | |
setTimeout(connectWebSocket, 5000); | |
}; | |
} | |
// Method 3: HTTP Communication | |
async function sendToLocalAgent(data) { | |
try { | |
const response = await fetch('http://127.0.0.1:8080/api/data', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(data) | |
}); | |
return await response.json(); | |
} catch (error) { | |
console.error('Error communicating with local agent:', error); | |
} | |
} | |
// Example local agent (Node.js) | |
const WebSocket = require('ws'); | |
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
// HTTP Server Setup | |
app.use(cors()); | |
app.use(express.json()); | |
app.post('/api/data', (req, res) => { | |
console.log('Received data:', req.body); | |
res.json({ status: 'success' }); | |
}); | |
const httpServer = app.listen(8080); | |
// WebSocket Server Setup | |
const wss = new WebSocket.Server({ server: httpServer }); | |
wss.on('connection', (ws) => { | |
console.log('Client connected'); | |
ws.on('message', (message) => { | |
console.log('Received:', message); | |
// Process message and send response | |
ws.send(JSON.stringify({ status: 'received' })); | |
}); | |
}); | |
// Native Messaging Host (save as com.example.local_agent.json) | |
{ | |
"name": "com.example.local_agent", | |
"description": "Native messaging host for local agent", | |
"path": "PATH_TO_YOUR_HOST_SCRIPT", | |
"type": "stdio", | |
"allowed_origins": [ | |
"chrome-extension://YOUR_EXTENSION_ID/" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment