Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
listen for CHAT_NEW_MESSAGE message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayla Fitzsimmons authored and Kayla Fitzsimmons committed Sep 4, 2024
1 parent 157aa7e commit 66854a0
Showing 1 changed file with 7 additions and 38 deletions.
45 changes: 7 additions & 38 deletions apps/mocksi-lite-next/src/pages/content/mocksi-extension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,6 @@ function getIframeStyles(message: string): Partial<CSSStyleDeclaration> {
}
}

export const innerHTMLToJson = (innerHTML: string): string => {
const parser = new DOMParser();
const doc = parser.parseFromString(innerHTML, "text/html");

function elementToJson(element: Element): object {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const obj: any = {};

obj.tag = element.tagName.toLowerCase();

if (element.attributes.length > 0) {
obj.attributes = {};
for (const attr of Array.from(element.attributes)) {
obj.attributes[attr.name] = attr.value;
}
}

if (element.children.length > 0) {
obj.children = Array.from(element.children).map((child) =>
elementToJson(child),
);
} else {
obj.text = element.textContent;
}

return obj;
}

// Convert the body of the parsed document to JSON
const json = Array.from(doc.body.children).map((child) =>
elementToJson(child),
);
const body = json.length === 1 ? json[0] : json;

return JSON.stringify(body);
};
chrome.runtime.onMessage.addListener((request) => {
if (request.message === "mount-extension") {
const rootContainer = document.querySelector("#__mocksi__root");
Expand Down Expand Up @@ -153,12 +117,17 @@ chrome.runtime.onMessage.addListener((request) => {

if (request.message === "CHAT") {
sendResponse({
data: innerHTMLToJson(document.body.innerHTML),
data: reactor.exportDOM(),
message: request.message,
status: "ok",
});
}
if (request.message === "CHAT_NEW_MESSAGE") {
sendResponse({
message: request.message,
status: "ok",
});
}

// reactor
if (request.message === "EDITING" || request.message === "PLAY") {
for (const mod of request.data.edits) {
Expand Down

0 comments on commit 66854a0

Please sign in to comment.