Last active
March 3, 2022 19:16
-
-
Save Moizsohail/0324cba3ca0ef537e52d05e3ace556b6 to your computer and use it in GitHub Desktop.
Chrome Extension In React With NPM Modules: Part 3
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
import { | |
ChromeMessage, | |
ChromeMessageExecute, | |
MessageResponse, | |
MessageTypes, | |
} from "../types"; | |
import { say } from "cowsay"; | |
const messagesFromReactAppListener = ( | |
message: ChromeMessage, | |
sender: chrome.runtime.MessageSender, | |
sendResponse: MessageResponse | |
) => { | |
switch (message.type) { | |
case MessageTypes.execute: | |
const { text } = message as ChromeMessageExecute; | |
const newText = say({ | |
text: text, | |
e: "oO", | |
T: "U ", | |
}); | |
const images = document.querySelectorAll("img"); | |
[...images].forEach((node) => { | |
const p = document.createElement("p"); | |
p.innerHTML = newText; | |
p.style.whiteSpace = "pre"; | |
node.replaceWith(p); | |
}); | |
break; | |
} | |
}; | |
const main = () => { | |
console.log("[content.ts] Running"); | |
chrome.runtime.onMessage.addListener(messagesFromReactAppListener); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment