53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
// function injectedFunction(color) {
|
|
// document.body.style.backgroundColor = color;
|
|
// }
|
|
|
|
|
|
// chrome.runtime.onMessage.addListener(async ({ name, options }) => {
|
|
// if (name === 'addRecord') {
|
|
// console.log('Hello World!');
|
|
|
|
// // get active tab
|
|
// // const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
// // chrome.scripting.executeScript({
|
|
// // target: { tabId: tab.id },
|
|
// // func: injectedFunction,
|
|
// // args: ["orange"],
|
|
// // });
|
|
|
|
// const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
// const response = await chrome.tabs.sendMessage(tab.id, { greeting: "hello" });
|
|
// // do something with response here, not outside the function
|
|
// console.log(response);
|
|
|
|
// }
|
|
// });
|
|
|
|
let port;
|
|
|
|
function sendMessage(message) {
|
|
if (!port) {
|
|
// this will run the first time to launch the native host, and save the connection to that instance in the port
|
|
port = chrome.runtime.connectNative('com.woni.chrome.botext');
|
|
}
|
|
// Convert the message to a Uint8Array.
|
|
var encoder = new TextEncoder();
|
|
var messageBytes = encoder.encode(message);
|
|
// Send the message length as a 32-bit integer.
|
|
var buffer = new ArrayBuffer(4);
|
|
var view = new DataView(buffer);
|
|
view.setUint32(0, messageBytes.length, true);
|
|
port.postMessage(buffer);
|
|
|
|
// Send the message content.
|
|
port.postMessage(messageBytes);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sendMessage('message 1');
|
|
sendMessage('message 2');
|
|
sendMessage('message 3');
|