chrome-extensions-samples
chrome-extensions-samples copied to clipboard
Unchecked runtime.lastError: Error when communicating with the native messaging host.
Can you post the code, here?
// json
{
"name": "com.google.chrome.securitycontrols.electron",
"description": "Chrome Native Messaging API Example Host",
"path": "/Library/Google/Chrome/NativeMessagingHosts/security-controls-pkg",
"type": "stdio",
"allowed_origins": [
"chrome-extension://mgcjafdofkofijefcpfcaefblidfabko/"
]
}
// pkg
const { Transform } = require('stream');
const commaSplitter = new Transform({
readableObjectMode: true,
transform(chunk, encoding, callback) {
this.push(chunk.toString().trim().split(','));
callback();
}
});
const arrayToObject = new Transform({
readableObjectMode: true,
writableObjectMode: true,
transform(chunk, encoding, callback) {
const obj = {};
for (let i = 0; i < chunk.length; i += 2) {
obj[chunk[i]] = chunk[i + 1];
}
this.push(obj);
callback();
}
});
const objectToString = new Transform({
writableObjectMode: true,
transform(chunk, encoding, callback) {
this.push(JSON.stringify(chunk) + '\n');
callback();
}
});
process.stdin
.pipe(commaSplitter)
.pipe(arrayToObject)
.pipe(objectToString)
.pipe(process.stdout)
//background.js
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
console.log('background:', request.content);
var hostName = "com.google.chrome.securitycontrols.electron";
var port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener((message) => {
console.log('onRecvNativeMessage');
console.log(message)
callback("background.js to main.js ===");
console.log('onRecvNativeMessage=====end');
});
port.onDisconnect.addListener(() => {
console.log('onDisconnected');
port = null;
});
port.postMessage("a,b,c,d");
callback("background.js to main.js");
});
Are you sure the Native Messaging host protocol is being implemented properly?
Are you using Node.js?
If yes, I would try using https://github.com/simov/native-messaging as a template.
process.stdin .pipe(commaSplitter) .pipe(arrayToObject) .pipe(objectToString) .pipe(process.stdout) // problem runtime.lastError: Error when communicating with the native messaging host
=>
process.stdin.pipe(process.stdout) // no problem
{
"name": "智秒安全插件",
"version": "6.0.0",
"manifest_version": 3,
"description": "使用本插件实现从指定PC登陆系统",
"icons": {
"16": "icon-16.png",
"128": "icon-128.png"
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": [
"*://*.100youdian.com/*",
"*://*.miaoerp.com/*"
],
"js": [
"main.js"
],
"run_at": "document_end"
}
],
"permissions": [
"storage",
"declarativeContent",
"nativeMessaging"
],
"externally_connectable": {
"matches": ["*://*.100youdian.com/*", "*://*.miaoerp.com/*"]
}
}
Native Messaging protocol expects JSON; escaped when necessary. What is the JSON that you are sending from Native Messaging host?
Is the message a string, array, or object?
If the message is a string, are there unescaped double quotes included in the string, or not surrounding the string?
port.postMessage("a,b,c,d");
What does the Native Messaging host expected to output to Native Messaging client?
{a: b, c: d}
That does not appear to be valid JSON. Is the requirement to remove double quotes?
'{"a":"b","c":"d"}'
What does the Native Messaging host expected to output to Native Messaging client?
object
What does the Native Messaging host expected to output to Native Messaging client?
{a:"b", c:"d"}
Have you tried removing + '\n'
following JSON.strinigfy()
?
Have you tried removing
+ '\n'
followingJSON.strinigfy()
?
Still not
Where do you send message length preceding message per the Native Messaging protocol?
https://developer.chrome.com/docs/apps/nativeMessaging/#native-messaging-host-protocol
The same format is used to send messages in both directions: each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#app_side
Each message is serialized using JSON, UTF-8 encoded and is preceded with an unsigned 32-bit value containing the message length in native byte order.
I'm going to close this for now. If you still need help, feel free to start a conversation here: https://groups.google.com/a/chromium.org/g/chromium-extensions