Hello Chrome Ext noob here. I tried everything to have Chrome send a message to a native messaging host, but failed miserably on both mac and windows. I tried running the ext below and I keep getting “Error when communicating with the native messaging host.”.
background.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Entered addListener");
if (message.action === 'fetchInfo') {
chrome.runtime.sendNativeMessage('com.yourcompany.tf', message.data, response => {
if (chrome.runtime.lastError) {
console.log("Error communicating with native messaging host:", chrome.runtime.lastError.message);
sendResponse({ error: chrome.runtime.lastError.message });
} else {
console.log("Response from native messaging host:", response);
sendResponse(response);
}
});
return true; // This keeps the message channel open for asynchronous response
}
});
manifest.js
{
"manifest_version": 3,
"name": "WhoDidIt Extension",
"version": "1.0",
"permissions": ["activeTab", "scripting", "nativeMessaging"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["*://*.<deprecated>/tf/*/attributes"],
"js": ["content.js"]
}
],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"web_accessible_resources": [{
"resources": ["icon48.png"],
"matches": ["https://*/*"]
}]
}
com.yourcompany.tf.json
{
"name": "com.yourcompany.tf",
"description": "WhoDidIt Extension Native Messaging Host",
"path": "/opt/homebrew/bin/pwsh",
"args": [
"/Users/test/Downloads/TF/host.ps1"
],
"type": "stdio",
"allowed_origins": [
"chrome-extension://<deprecated>/"
]
}
Tried registering the native messaging host on both mac (by putting it in the NativeMessaging folder) and windows (by adding a registry key as specified).
New contributor
iTaher is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.