Skip to content

Commit

Permalink
fix(#3): button isn't showing with multiple 'new message' tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolev Franco committed Jan 9, 2023
1 parent 2c89d0b commit c809beb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/content-script/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'github-markdown-css'
import { render } from 'preact'
import { onChanged, getUserConfig } from '../config'
import { NewMessageObserver } from './new_message_observer'
import ChatGPTCard from './ChatGPTCard'
import './styles.scss'

Expand All @@ -9,7 +10,7 @@ import { BUTTON, SUGGESTIONS_BOX, REWRITE_DIALOG,
URL_PATTERN, REPLAY_MESSAGE_INPUT } from './consts'

enable = true;
observer_on_new_message = null;
observer_on_new_messages = []; // list of NewMessageObserver

function createBaseElement(elementType = "div", className) {
const container = document.createElement(elementType);
Expand Down Expand Up @@ -158,37 +159,38 @@ function handleChatGPTButton(bodyInput) {
}
}

function isInNodeList(node, nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (nodeList[i] === node) return true;
}
return false;
}

function handleMutations(mutations) {
mutations.forEach(async function(mutation) {
if (!enable) {
removeChatGPTButton();
} else {
if (URL_PATTERN.test(window.location.href)) {
if (observer_on_new_message == null) {
const bodyInput = document.querySelectorAll(NEW_MESSAGE_INPUT, REPLAY_MESSAGE_INPUT);
if (bodyInput.length == 0) return;
observer_on_new_message = new MutationObserver(function(mutations) {
mutations.forEach(async function(mutation) {
if (enable) {
handleChatGPTButton(bodyInput[0]);
}
});
});
observer_on_new_message.observe(bodyInput[0], {
attributes: true,
childList: true,
characterData: true
});
const bodyInput = document.querySelectorAll(NEW_MESSAGE_INPUT, REPLAY_MESSAGE_INPUT); //:Node[]
if (observer_on_new_messages.length < bodyInput.length) {
observer_on_new_messages.push(new NewMessageObserver(handleChatGPTButton, bodyInput[bodyInput.length-1]));
}

}
}

if (document.querySelectorAll(NEW_MESSAGE_INPUT, REPLAY_MESSAGE_INPUT).length == 0 && observer_on_new_message != null) {
observer_on_new_message.disconnect();
removeChatGPTButton();
removeChatGPTSuggestionBox();
observer_on_new_message = null;
const bodyInput = document.querySelectorAll(NEW_MESSAGE_INPUT, REPLAY_MESSAGE_INPUT); //:Node[]

if (bodyInput.length < observer_on_new_messages.length) {
for (let i=0; i<observer_on_new_messages.length; i++) {
if (!isInNodeList(observer_on_new_messages[i].getTarget(), bodyInput)) {
observer_on_new_messages[i].disconnect();
observer_on_new_messages.splice(i, 1);
removeChatGPTButton();
removeChatGPTSuggestionBox();
break;
}
}
}
});
}
Expand Down
27 changes: 27 additions & 0 deletions src/content-script/new_message_observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class NewMessageObserver {
private observer: MutationObserver;
private target: Node;

constructor(callback: (element: Node) => void, target: Node) {
this.target = target;
this.observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
callback(target);
});
});
const observerOptions = {
attributes: true,
childList: true,
characterData: true,
};
this.observer.observe(target, observerOptions);
}

getTarget(): Node {
return this.target;
}

disconnect(): void {
this.observer.disconnect();
}
}

0 comments on commit c809beb

Please sign in to comment.