lainon
lainon
PD🧩 Plasmo Developers
Created by lainon on 1/4/2024 in #🔰newbie
Communicating between extensions and other applications
Hey everyone, I wanted to see if anyone has any experience getting a plasmo extension to communicate with another process running on the same machine. I know I could build a simple web server and communicate with the external application via HTTP, but considering they're both hosted on the same machine, I figured there might be a faster way to communicate between them. ChatGPT said I could use the following code sample, but I'm currently yet to try it, and I don't know if there's some API I could use instead.
use std::io::{self, BufRead, Write};

fn main() {
let stdin = io::stdin();
let mut reader = stdin.lock();
let stdout = io::stdout();
let mut writer = stdout.lock();

loop {
let mut message = String::new();
reader.read_line(&mut message).expect("Failed to read line");
let response = process_message(&message);
writer.write_all(response.as_bytes()).expect("Failed to write response");
writer.flush().expect("Failed to flush");
}
}

fn process_message(message: &str) -> String {
// Implement your logic to process messages here
// You can use serde for JSON serialization/deserialization
// Example: Convert message to JSON and return a response
let response = format!("{{\"result\": \"Processed: {}\"}}", message.trim());
response
}
use std::io::{self, BufRead, Write};

fn main() {
let stdin = io::stdin();
let mut reader = stdin.lock();
let stdout = io::stdout();
let mut writer = stdout.lock();

loop {
let mut message = String::new();
reader.read_line(&mut message).expect("Failed to read line");
let response = process_message(&message);
writer.write_all(response.as_bytes()).expect("Failed to write response");
writer.flush().expect("Failed to flush");
}
}

fn process_message(message: &str) -> String {
// Implement your logic to process messages here
// You can use serde for JSON serialization/deserialization
// Example: Convert message to JSON and return a response
let response = format!("{{\"result\": \"Processed: {}\"}}", message.trim());
response
}
// For Chrome
const port = chrome.runtime.connectNative('com.example.native');

// For Firefox
// const port = browser.runtime.connectNative('com.example.native');

port.postMessage({ message: 'Hello from extension!' });

port.onMessage.addListener((response) => {
console.log('Received response:', response);
});

port.onDisconnect.addListener(() => {
console.log('Port disconnected');
});
// For Chrome
const port = chrome.runtime.connectNative('com.example.native');

// For Firefox
// const port = browser.runtime.connectNative('com.example.native');

port.postMessage({ message: 'Hello from extension!' });

port.onMessage.addListener((response) => {
console.log('Received response:', response);
});

port.onDisconnect.addListener(() => {
console.log('Port disconnected');
});
2 replies
PD🧩 Plasmo Developers
Created by lainon on 8/21/2023 in #🔰newbie
Targeting Firefox MV2 instead of Chrome MV3 by default
Hey everyone, I was wondering how I could configure plasmo so it targets firefox manifest v2 instead of chrome?
2 replies