ken107 / read-aloud

An awesome browser extension that reads aloud webpage content with one click
https://readaloud.app
MIT License
1.31k stars 227 forks source link

Update on events.js (Access from external extensions) #368

Closed bluesidra closed 6 months ago

bluesidra commented 6 months ago

Added code to make ReadAloud able to listen to external messages/command. It listens to all messages that start with "play ", "pause", "stop", "resume" and execute the corresponding command. Example message: "play ReadAloud is awesome!" If ReadAloud receives this message, it would lead to it reading "ReadAloud is awesome!"

Example for calling ReadAloud from a different extension's content script:

const readAloudID = "hdhinadidafjejdhmfkjgnolgimiaplp"; 
document.addEventListener('Read Command', function (e) {
    const data = "play " + e.detail;
    chrome.runtime.sendMessage(readAloudID, {message: data});
});

Only for Chrome, bc using "browser" yielded an error :( (Sorry for any gihub or coding screw-ups, I'm fairly new to all of it)

ken107 commented 6 months ago

I modified the API so you would call it as follows:

const readAloudID = "hdhinadidafjejdhmfkjgnolgimiaplp"; 
document.addEventListener('Read Command', function (e) {
    const data = {method:"play", text:e.detail};
    chrome.runtime.sendMessage(readAloudID, {message: data});
});