Created
July 2, 2022 19:23
-
-
Save guest271314/42ef867085f06b7b66a4fe0c7f289b98 to your computer and use it in GitHub Desktop.
Node.js Native Messaging Host Memory Usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
port = chrome.runtime.connectNative('node_native_messaging_host_memory_usage'); | |
port.onMessage.addListener((message)=>{ | |
console.log(message); | |
}); | |
port.onDisconnect.addListener(()=>{ | |
if (chrome.runtime.lastError) { | |
console.log(chrome.runtime.lastError.message) | |
} | |
console.log('Disconnected') | |
}); | |
port.postMessage('test'); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!node | |
// https://github.com/simov/native-messaging/blob/master/protocol.js | |
// https://github.com/simov/native-messaging/blob/master/nodejs/example.js | |
// Might be good to use an explicit path to node on the shebang line | |
// in case it isn't in PATH when launched by Chrome | |
// let sendMessage = require('./protocol')(handleMessage); | |
process.stdin.on('readable', () => { | |
let input = []; | |
let chunk; | |
while ((chunk = process.stdin.read())) { | |
input.push(chunk); | |
} | |
input = Buffer.concat(input); | |
let msgLen = input.readUInt32LE(0); | |
let dataLen = msgLen + 4; | |
if (input.length >= dataLen) { | |
let content = input.slice(4, dataLen); | |
let json = JSON.parse(content.toString()); | |
handleMessage(json); | |
} | |
}); | |
function sendMessage(msg) { | |
let buffer = Buffer.from(JSON.stringify(msg)); | |
let header = Buffer.alloc(4); | |
header.writeUInt32LE(buffer.length, 0); | |
let data = Buffer.concat([header, buffer]); | |
process.stdout.write(data); | |
} | |
process.on('uncaughtException', (err) => { | |
sendMessage({ error: err.toString() }); | |
}); | |
function handleMessage(input) { | |
while (true) { | |
sendMessage([...new Uint8Array(1764).fill(255)]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node-native-messaging-host-memory-usage", | |
"version": "1.0", | |
"manifest_version": 3, | |
"permissions": [ | |
"nativeMessaging" | |
], | |
"background": { | |
"service_worker": "background.js" | |
}, | |
"action": {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "node_native_messaging_host_memory_usage", | |
"description": "Node.js Native Messaging Host Memory Usage", | |
"path": "/path/to/node-native-messaging-host-memory-usage/example.js", | |
"type": "stdio", | |
"allowed_origins": [ | |
"chrome-extension://<id>/" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment