so i've been trying to set up communication between a python script (external process) and my chrome extension. i've tried implementing the native messaging, in the best way possible looking through the official docs and previous questions on here. but nothing seems to happen.
my goal is to send the url of the current page through my extension to the python script and do something of relevance, and perhaps send back some data to the extension.
i've tried changing the code a lot and now i wonder if this is even possible using native messaging.
the issue is that, when i reload my extension, (from the extensions page), a vs code window suddenly pops up with the host.py (the native host file) open, it doesn't even run it.
the connection is made, as, when i close the vscode window, the console.log statement in the onDisconnect handler in the background.js file, is displayed in the browser console. But the port.postMessage() doesn't do anything, i want to have the text sent from background.js, displayed onto the vscode console, so i can know that i have access to the sent data in the python script.
i've tried several approaches, like keeping the port.postMessage in a loop, i've also tried using a button which should trigger the port.postMessage() function, but again it just opens the stationary vscode window, nothing else happens.
below are the relevant files:
host.json
{
"name": "com.google.chrome.noodleextension",
"description": "chrome extension to display your favourite noodle",
"path": "host.py",
"type": "stdio",
"allowed_origins": [
"chrome-extension://ffnijbdimnceklfeaihfedaiouygfdxe/"
]
}
host.bat
@echo off
C:\Python311\python.exe host.py
host.py
import sys
import json
import struct
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
def getMessage():
# the code does stop here, unexpectedly.
rawLength = sys.stdin.buffer.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
return json.loads(message)
def encodeMessage(messageContent):
encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8')
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
def sendMessage(encodedMessage):
sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.buffer.write(encodedMessage['content'])
sys.stdout.buffer.flush()
while True:
receivedMessage = getMessage()
sendMessage(encodeMessage("pong"))
background.js
const hostName = "com.google.chrome.noodleextension";
let port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(function(text){
console.log("received", + text);
});
port.onDisconnect.addListener(function(){
// the connection is made, as when i close the vs code window, this error is printed to the browser console
console.error(`Failed to connect: "${chrome.runtime.lastError.message}"`)
});
const message = {text: "ping"};
port.postMessage(message);
any direction would be appreciated...