I have read docs, but still cannot realize. I have desktop application written in C and Chrome extension. I know how to receive this message in my chrome extension:
port.onMessage.addListener(function(msg) {
console.log("Received" + msg);
});
What should I write in my C application to send a message to my chrome extension? Python/NodeJS examples are also appropriate.
Native messaging enables an extension to exchange messages with a native application, installed on the user's computer.
If you want to export Chrome extensions manually, you have to enable 'Developer mode' in the browser and pack the extension in a CRX file. CRX is a file that Chrome automatically downloads and installs when you add an extension.
When sending a message to the content script, we need to specify which tab to send it to. Therefore, we need to retrieve the active tab information first, and then use tabs. sendMessage . To use the tabs API and to have access to the active tab, you need to add tabs and activeTab under permissions in your manifest.
In order for a native messaging host to send data back to Chrome, you must first send four bytes of length information and then send the JSON formatted message as a string/char-array.
Below are two examples for C and C++ respectively that do the same thing in slightly different ways.
C example:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
// Define our message
char message[] = "{\"text\": \"This is a response message\"}";
// Collect the length of the message
unsigned int len = strlen(message);
// We need to send the 4 bytes of length information
printf("%c%c%c%c", (char) (len & 0xff),
(char) ((len>>8) & 0xFF),
(char) ((len>>16) & 0xFF),
(char) ((len>>24) & 0xFF));
// Now we can output our message
printf("%s", message);
return 0;
}
C++ example:
#include <string.h>
int main(int argc, char* argv[]) {
// Define our message
std::string message = "{\"text\": \"This is a response message\"}";
// Collect the length of the message
unsigned int len = message.length();
// We need to send the 4 bytes of length information
std::cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
// Now we can output our message
std::cout << message;
return 0;
}
(The actual message can be sent at the same time as the length information; it is merely broken out for clarity.)
So following the OP Chrome example, here is how to output the message:
port.onMessage.addListener(function(msg) {
console.log("Received" + msg.text);
});
In reality, there is no requirement to use "text" as the key returned from your native messaging app; it could be anything. The JSON string passed to the listener from your native messaging app is converted to a JavaScript Object.
For a C++ example of a native messaging app that uses the above technique in combination with jsoncpp (C++ JSON library) and also parses the request sent to the app, see here: https://github.com/kylehuff/libwebpg/blob/22d4843f41670d4fd7c4cc7ea3cf833edf8f1baf/webpg.cc#L4501
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With