chrome-extensions-samples icon indicating copy to clipboard operation
chrome-extensions-samples copied to clipboard

Unchecked runtime.lastError: Error when communicating with the native messaging host.

Open 1004589696 opened this issue 2 years ago • 15 comments

1004589696 avatar Nov 10 '21 09:11 1004589696

Can you post the code, here?

guest271314 avatar Jan 02 '22 00:01 guest271314

// 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");
        });

1004589696 avatar Apr 28 '22 02:04 1004589696

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.

guest271314 avatar Apr 28 '22 02:04 guest271314

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

1004589696 avatar Apr 28 '22 03:04 1004589696

      {
        "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/*"]
        }
      }

1004589696 avatar Apr 28 '22 03:04 1004589696

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?

guest271314 avatar Apr 28 '22 03:04 guest271314

port.postMessage("a,b,c,d");

1004589696 avatar Apr 28 '22 03:04 1004589696

What does the Native Messaging host expected to output to Native Messaging client?

guest271314 avatar Apr 28 '22 03:04 guest271314

{a: b, c: d}

1004589696 avatar Apr 28 '22 03:04 1004589696

That does not appear to be valid JSON. Is the requirement to remove double quotes?

'{"a":"b","c":"d"}'

guest271314 avatar Apr 28 '22 03:04 guest271314

What does the Native Messaging host expected to output to Native Messaging client?

object

1004589696 avatar Apr 28 '22 03:04 1004589696

What does the Native Messaging host expected to output to Native Messaging client?

{a:"b", c:"d"}

1004589696 avatar Apr 28 '22 03:04 1004589696

Have you tried removing + '\n' following JSON.strinigfy()?

guest271314 avatar Apr 28 '22 03:04 guest271314

Have you tried removing + '\n' following JSON.strinigfy()?

Still not

1004589696 avatar Apr 28 '22 03:04 1004589696

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.

guest271314 avatar Apr 28 '22 04:04 guest271314

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

oliverdunk avatar Feb 19 '24 18:02 oliverdunk