Manifest < /p>
Код: Выделить всё
[...]
"manifest_version": 3,
"permissions": [
"storage",
"tabs",
"background",
"notifications",
"nativeMessaging",
"scripting",
"debugger"
],
"host_permissions": [
"https://*/*",
"http://*/*"
],
"background": {
"service_worker": "scripts/background.js"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"scripts/content.js"
]
}
],
[...]
< /code>
фоновый скрипт (образец) < /p>
const nativeID = "com.nmhost";
var nmhostPort = chrome.runtime.connectNative(nativeID);
const onNativeMessage = function (msg) {
// if (dbgFlag && dbgLvl == 3) {
// console.log("Received: \n" + JSON.stringify(msg, null, 4));
// }
let ss = JSON.stringify(msg);
console.log(`Received:[${ss.length}]`);
return;
}
nmhostPort.onMessage.addListener(onNativeMessage);
nmhostPort.onDisconnect.addListener(function () {
console.error("NMHost Disconnected");
});
< /code>
Native Host (C ++) (образец) < /p>
/**
* @brief Get the Message from Chromium. If the length of the message is zero or less it does not read the message!
*
* @param buff The message buffer to return
* @return int32_t
*/
int32_t GetExtMessage(char *buff) {
// if stdin fails retry 3 times
for (int i = 0; i < STDIN_RETRY; i++) {
int32_t reqLen = 0;
std::cin.read(reinterpret_cast(&reqLen), 4);
if (reqLen > 0) {
std::cin.read(buff, reqLen);
return reqLen;
} else {
if (std::cin.fail()) {
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79492312/chrome-fails-to-read-message-fron-native-host-if-the-message-length-is-multiple[/url]