Skip to content

Commit 12120c3

Browse files
committed
Update content scripts for all other working templates
1 parent 04f94b2 commit 12120c3

File tree

13 files changed

+87
-76
lines changed

13 files changed

+87
-76
lines changed

examples/content-esm/content/scripts.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ function initial() {
2626
// prevents conflicts with the host page's styles.
2727
// This way, styles from the extension won't leak into the host page.
2828
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
29-
const style = new CSSStyleSheet()
30-
shadowRoot.adoptedStyleSheets = [style]
31-
fetchCSS().then((response) => style.replace(response))
29+
30+
31+
const styleElement = document.createElement('style')
32+
shadowRoot.appendChild(styleElement)
33+
fetchCSS().then((response) => (styleElement.textContent = response))
3234

3335
if (import.meta.webpackHot) {
3436
import.meta.webpackHot?.accept('./styles.css', () => {
35-
fetchCSS().then((response) => style.replace(response))
37+
fetchCSS().then((response) => (styleElement.textContent = response))
3638
})
3739
}
3840

@@ -46,7 +48,6 @@ function initial() {
4648

4749
return () => {
4850
rootDiv.remove()
49-
5051
}
5152
}
5253

examples/content-extension-config/content/scripts.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@ function initial() {
2828
// prevents conflicts with the host page's styles.
2929
// This way, styles from the extension won't leak into the host page.
3030
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
31-
const style = new CSSStyleSheet()
32-
shadowRoot.adoptedStyleSheets = [style]
33-
fetchCSS().then((response) => style.replace(response))
31+
32+
const styleElement = document.createElement('style')
33+
shadowRoot.appendChild(styleElement)
34+
fetchCSS().then((response) => (styleElement.textContent = response))
3435

3536
if (import.meta.webpackHot) {
3637
import.meta.webpackHot?.accept('./styles.css', () => {
37-
fetchCSS().then((response) => style.replace(response))
38+
fetchCSS().then((response) => (styleElement.textContent = response))
3839
})
3940
}
4041

41-
const mountingPoint = ReactDOM.createRoot(shadowRoot)
42-
mountingPoint.render(
43-
<div className="content_script">
44-
<ContentApp />
45-
</div>
46-
)
42+
// Create container for React app
43+
const container = document.createElement('div')
44+
container.className = 'content_script'
45+
shadowRoot.appendChild(container)
46+
47+
const mountingPoint = ReactDOM.createRoot(container)
48+
mountingPoint.render(<ContentApp />)
4749
return () => {
4850
mountingPoint.unmount()
4951
rootDiv.remove()

examples/content-less/content/scripts.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ function initial() {
2727
// prevents conflicts with the host page's styles.
2828
// This way, styles from the extension won't leak into the host page.
2929
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
30-
const style = new CSSStyleSheet()
31-
shadowRoot.adoptedStyleSheets = [style]
30+
31+
const styleElement = document.createElement('style')
32+
shadowRoot.appendChild(styleElement)
3233

3334
// Fetch and apply LESS styles
34-
fetchLessStyles().then((response) => style.replace(response))
35+
fetchLessStyles().then((response) => (styleElement.textContent = response))
3536

3637
if (import.meta.webpackHot) {
3738
import.meta.webpackHot?.accept('./styles.less', () => {
38-
fetchLessStyles().then((response) => style.replace(response))
39+
fetchLessStyles().then(
40+
(response) => (styleElement.textContent = response)
41+
)
3942
})
4043
}
4144

examples/content-main-world/content/scripts.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ function initial() {
2525
// prevents conflicts with the host page's styles.
2626
// This way, styles from the extension won't leak into the host page.
2727
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
28-
const style = new CSSStyleSheet()
29-
shadowRoot.adoptedStyleSheets = [style]
30-
fetchCSS().then((response) => style.replace(response))
28+
29+
const styleElement = document.createElement('style')
30+
shadowRoot.appendChild(styleElement)
31+
fetchCSS().then((response) => (styleElement.textContent = response))
3132

3233
if (import.meta.webpackHot) {
3334
import.meta.webpackHot?.accept('./styles.css', () => {
34-
fetchCSS().then((response) => style.replace(response))
35+
fetchCSS().then((response) => (styleElement.textContent = response))
3536
})
3637
}
3738

examples/content-preact/content/scripts.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,23 @@ function initial() {
2828
// prevents conflicts with the host page's styles.
2929
// This way, styles from the extension won't leak into the host page.
3030
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
31-
const style = new CSSStyleSheet()
32-
shadowRoot.adoptedStyleSheets = [style]
33-
fetchCSS().then((response) => style.replace(response))
31+
32+
const styleElement = document.createElement('style')
33+
shadowRoot.appendChild(styleElement)
34+
fetchCSS().then((response) => (styleElement.textContent = response))
3435

3536
if (import.meta.webpackHot) {
3637
import.meta.webpackHot?.accept('./styles.css', () => {
37-
fetchCSS().then((response) => style.replace(response))
38+
fetchCSS().then((response) => (styleElement.textContent = response))
3839
})
3940
}
4041

41-
render(
42-
<div className="content_script">
43-
<ContentApp />
44-
</div>,
45-
shadowRoot
46-
)
42+
// Create container for Preact app
43+
const container = document.createElement('div')
44+
container.className = 'content_script'
45+
shadowRoot.appendChild(container)
46+
47+
render(<ContentApp />, container)
4748

4849
return () => {
4950
rootDiv.remove()

examples/content-react-svgr/content/scripts.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@ function initial() {
2828
// prevents conflicts with the host page's styles.
2929
// This way, styles from the extension won't leak into the host page.
3030
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
31-
const style = new CSSStyleSheet()
32-
shadowRoot.adoptedStyleSheets = [style]
33-
fetchCSS().then((response) => style.replace(response))
31+
32+
const styleElement = document.createElement('style')
33+
shadowRoot.appendChild(styleElement)
34+
fetchCSS().then((response) => (styleElement.textContent = response))
3435

3536
if (import.meta.webpackHot) {
3637
import.meta.webpackHot?.accept('./styles.css', () => {
37-
fetchCSS().then((response) => style.replace(response))
38+
fetchCSS().then((response) => (styleElement.textContent = response))
3839
})
3940
}
4041

41-
const mountingPoint = ReactDOM.createRoot(shadowRoot)
42-
mountingPoint.render(
43-
<div className="content_script">
44-
<ContentApp />
45-
</div>
46-
)
42+
// Create container for React app
43+
const container = document.createElement('div')
44+
container.className = 'content_script'
45+
shadowRoot.appendChild(container)
46+
47+
const mountingPoint = ReactDOM.createRoot(container)
48+
mountingPoint.render(<ContentApp />)
4749
return () => {
4850
mountingPoint.unmount()
4951
rootDiv.remove()

examples/content-react/content/scripts.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ function initial() {
2828
// prevents conflicts with the host page's styles.
2929
// This way, styles from the extension won't leak into the host page.
3030
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
31-
const style = new CSSStyleSheet()
32-
shadowRoot.adoptedStyleSheets = [style]
33-
fetchCSS().then((response) => style.replace(response))
31+
32+
const styleElement = document.createElement('style')
33+
shadowRoot.appendChild(styleElement)
34+
fetchCSS().then((response) => (styleElement.textContent = response))
3435

3536
if (import.meta.webpackHot) {
3637
import.meta.webpackHot?.accept('./styles.css', () => {
37-
fetchCSS().then((response) => style.replace(response))
38+
fetchCSS().then((response) => (styleElement.textContent = response))
3839
})
3940
}
4041

41-
const mountingPoint = ReactDOM.createRoot(shadowRoot)
42+
// Create a container for React to render into
43+
const container = document.createElement('div')
44+
shadowRoot.appendChild(container)
45+
46+
const mountingPoint = ReactDOM.createRoot(container)
4247
mountingPoint.render(
4348
<div className="content_script">
4449
<ContentApp />

examples/content-sass/content/scripts.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ function initial() {
2727
// prevents conflicts with the host page's styles.
2828
// This way, styles from the extension won't leak into the host page.
2929
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
30-
const style = new CSSStyleSheet()
31-
shadowRoot.adoptedStyleSheets = [style]
30+
31+
const styleElement = document.createElement('style')
32+
shadowRoot.appendChild(styleElement)
3233

3334
// Fetch and apply CSS styles
34-
fetchCssStyles().then((response) => style.replace(response))
35+
fetchCssStyles().then((response) => (styleElement.textContent = response))
3536

3637
// Create container div
3738
const contentDiv = document.createElement('div')

examples/content-svelte/content/scripts.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ function initial() {
2727
// prevents conflicts with the host page's styles.
2828
// This way, styles from the extension won't leak into the host page.
2929
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
30-
const style = new CSSStyleSheet()
31-
shadowRoot.adoptedStyleSheets = [style]
32-
fetchCSS().then((response) => style.replace(response))
30+
31+
const styleElement = document.createElement('style')
32+
shadowRoot.appendChild(styleElement)
33+
fetchCSS().then((response) => (styleElement.textContent = response))
3334

3435
if (import.meta.webpackHot) {
3536
import.meta.webpackHot?.accept('./styles.css', () => {
36-
fetchCSS().then((response) => style.replace(response))
37+
fetchCSS().then((response) => (styleElement.textContent = response))
3738
})
3839
}
3940

examples/content-tailwind/content/scripts.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ function initial() {
2626
// prevents conflicts with the host page's styles.
2727
// This way, styles from the extension won't leak into the host page.
2828
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
29-
const style = new CSSStyleSheet()
30-
shadowRoot.adoptedStyleSheets = [style]
31-
fetchCSS().then((response) => style.replace(response))
29+
30+
const styleElement = document.createElement('style')
31+
shadowRoot.appendChild(styleElement)
32+
fetchCSS().then((response) => (styleElement.textContent = response))
3233

3334
if (import.meta.webpackHot) {
3435
import.meta.webpackHot?.accept('./styles.css', () => {
35-
fetchCSS().then((response) => style.replace(response))
36+
fetchCSS().then((response) => (styleElement.textContent = response))
3637
})
3738
}
3839

examples/content-typescript/content/scripts.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
// @ts-expect-error - Import handled by webpack
22
import logo from '../images/logo.svg'
33

4-
declare global {
5-
interface ImportMeta {
6-
webpackHot?: {
7-
accept: (path?: string, callback?: (newModule: any) => void) => void
8-
dispose: (callback: () => void) => void
9-
}
10-
}
11-
}
12-
134
let unmount: () => void
145

156
if (import.meta.webpackHot) {

examples/content-vue/content/scripts.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ function initial() {
2727
// prevents conflicts with the host page's styles.
2828
// This way, styles from the extension won't leak into the host page.
2929
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
30-
const style = new CSSStyleSheet()
31-
shadowRoot.adoptedStyleSheets = [style]
32-
fetchCSS().then((response) => style.replace(response))
30+
31+
const styleElement = document.createElement('style')
32+
shadowRoot.appendChild(styleElement)
33+
fetchCSS().then((response) => (styleElement.textContent = response))
3334

3435
if (import.meta.webpackHot) {
3536
import.meta.webpackHot?.accept('./styles.css', () => {
36-
fetchCSS().then((response) => style.replace(response))
37+
fetchCSS().then((response) => (styleElement.textContent = response))
3738
})
3839
}
3940

examples/content/content/scripts.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ function initial() {
2626
// prevents conflicts with the host page's styles.
2727
// This way, styles from the extension won't leak into the host page.
2828
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
29-
const style = new CSSStyleSheet()
30-
shadowRoot.adoptedStyleSheets = [style]
31-
fetchCSS().then((response) => style.replace(response))
29+
30+
const styleElement = document.createElement('style')
31+
shadowRoot.appendChild(styleElement)
32+
fetchCSS().then((response) => (styleElement.textContent = response))
3233

3334
if (import.meta.webpackHot) {
3435
import.meta.webpackHot?.accept('./styles.css', () => {
35-
fetchCSS().then((response) => style.replace(response))
36+
fetchCSS().then((response) => (styleElement.textContent = response))
3637
})
3738
}
3839

0 commit comments

Comments
 (0)