-
-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Jonghakseo/content-script-react
Add content view in content script
- Loading branch information
Showing
5 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
try { | ||
console.log("content loaded"); | ||
injectContentViewScript(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
/** | ||
* @description | ||
* Chrome extensions don't support modules in content scripts. | ||
* Bundling solves this problem, but not in the case of React, which is imported as JSX automatic conversion. | ||
* So, I created a tag that brings the 'content view' from the 'content script', and implemented it by bypassing the tag in the form of inserting it into the html. | ||
* If there is a better implementation (bundling option or other method, etc.), please suggest it. | ||
*/ | ||
function injectContentViewScript() { | ||
const script = createContentViewScript(); | ||
const target = getTargetElement(); | ||
|
||
target.insertBefore(script, target.lastChild); | ||
} | ||
|
||
function createContentViewScript(): HTMLScriptElement { | ||
const script = document.createElement("script"); | ||
script.setAttribute("type", "module"); | ||
script.setAttribute( | ||
"src", | ||
chrome.runtime.getURL("src/pages/contentView/index.js") | ||
); | ||
return script; | ||
} | ||
|
||
function getTargetElement(): HTMLElement { | ||
return ( | ||
document.head || | ||
document.getElementsByTagName("head")[0] || | ||
document.documentElement | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useEffect } from "react"; | ||
|
||
export default function App() { | ||
useEffect(() => { | ||
console.log("content view loaded"); | ||
}, []); | ||
|
||
return <div>content view</div>; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createRoot } from "react-dom/client"; | ||
import App from "@pages/contentView/app"; | ||
|
||
const div = document.createElement("div"); | ||
document.body.append(div); | ||
|
||
createRoot(div).render(<App />); |
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