Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Content Script to Update DOM on active tab #31

Open
Rterrell25 opened this issue Jul 19, 2021 · 1 comment
Open

Using Content Script to Update DOM on active tab #31

Rterrell25 opened this issue Jul 19, 2021 · 1 comment

Comments

@Rterrell25
Copy link

I am attempting to use the built-in content script to update the dom on the active tab I am currently viewing. The issue I am running into is that the content scripts are updating the extension itself instead of the active Tab's DOM. Is there a specific way to use the content scripts so that I can inject JS into the active tab? Or are there any resources I can take a look at.

@vontell
Copy link

vontell commented Sep 12, 2021

Here is an example that I have created.
In my contentScript folder, I have an index.js file that manipulates the dom, and then renders a react component in the DOM.

contentScript/index.js:

export function attachButtonSwitchers() {
  //Code to manipulate the DOM, such as:
  const buttonContainer = document.createElement("div")
  buttonContainer.style.display = 'inline'
  buttonContainer.appendChild(...);
  chatHeader.parentNode.replaceChild(buttonContainer, chatHeader)
}

// Code to render an element in some div that I created and tagged
```js
export function mountReactPogger() {
  ReactDOM.render(
      <React.StrictMode>
        <Pogchat />
      </React.StrictMode>,
      document.getElementById("pogchat-container")
  );
}

My react component is then in this file, inside of the contentScript folder.

The final result is that I get, in my case, a modified twitch chat:
Screen Shot 2021-09-12 at 3 40 28 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants