Closed moshOntong-IT closed 2 years ago
If I'm understanding your correctly, you want to render a custom React component on a webpage of your choice. All you need to do is use ReactDOM.render
. Here's a full example (not tested):
Create your React component:
MyComponent.js
import React from 'react';
const MyComponent = () => <div>This is my component</div>
export default MyComponent
Then in your Content script file, you need to (1) import the component, (2) create or find an element on the webpage to manipulate, and (3) render your React component in that element:
Content/index.js
// (1) Import component
import MyComponent from '..../MyComponent';
...
// (2) Create `myElement` to manipulate
const myElement = document.createElement('div');
myElement.setAttribute('id', 'MY-COMPONENT-ID');
myElement.setAttribute('style', 'all:unset');
// (2) Find an element in the DOM to manipulate
const foundExistingElement = document.querySelector('SOME-EXISTING-ELEMENT-HERE');
// (2) Here we're injecting `myElement` into the found existing element on the page. (Alternatively we can manipulate the existing element directly - Explained below on (4))
foundExistingElement.prepend(myElement)
// (3) Render your React component inside the `myElement` that you injected into the webpage in step (2).
ReactDOM.render(
<MyComponent />,
// (4) Alternatively, you can just target an existing element on the page here without needing to create `myElement`.
document.getElementById('MY-COMPONENT-ID')
);
The above should render <MyComponent />
on your webpage on every successful load (Given that you target the correct elements).
Now you can add custom events and listeners to make it render on command.
Hope this helps!
Thank you so much
I think this should be added in the documentation or added to the content script example code.
So I was planning to create a popover when the user select some text in web content.
Before I manually coded like this
`;
const popWrapper = document.createElement("div"); popWrapper.id = "kit-translator-button"; const popIcon = document.createElement("div"); popIcon.classList.add("kit-pop-icon"); popIcon.innerHTML =
<svg version="1.1" id="anna_vital_language_icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="15px" height="15px" fill="#ffff" viewBox="0 0 256 256" enable-background="new 0 0 256 256" xml:space="preserve">`
As you can see this is not kinda readable thing in our eyes, right. I want to render this element by using the react-dom.