Skip to content

Commit 4fcc39d

Browse files
committed
PoC - content-sass
1 parent 0a2f7cf commit 4fcc39d

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

examples/content-react/content/scripts.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ function initial() {
5353
}
5454

5555
async function fetchCSS() {
56-
// console.log('import.meta.url', import.meta.url)
5756
const response = await fetch(new URL('./styles.css', import.meta.url))
58-
// const response = await fetch(new URL('./content_scripts/content-0.css', chrome.runtime.getURL('')))
5957
const text = await response.text()
60-
// console.log('response', text)
6158
return response.ok ? text : Promise.reject(text)
6259
}
Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import './styles.scss?inline_style'
21
import logo from '../images/logo.svg'
32

43
console.log('hello from content_scripts')
54

5+
let unmount
6+
import.meta.webpackHot.accept()
7+
import.meta.webpackHot.dispose(() => unmount())
8+
69
if (document.readyState === 'complete') {
7-
initial()
10+
unmount = initial() || (() => {})
811
} else {
912
document.addEventListener('readystatechange', () => {
10-
if (document.readyState === 'complete') initial()
13+
if (document.readyState === 'complete') unmount = initial() || (() => {})
1114
})
1215
}
1316

@@ -21,24 +24,52 @@ function initial() {
2124
// This way, styles from the extension won't leak into the host page.
2225
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
2326

24-
// Inform Extension.js that the shadow root is available.
25-
window.__EXTENSION_SHADOW_ROOT__ = shadowRoot
26-
27-
shadowRoot.innerHTML = `
28-
<div class="content_script">
29-
<img class="content_logo" src="${logo}" />
30-
<h1 class="content_title">
31-
Welcome to your Sass Extension
32-
</h1>
33-
<p class="content_description">
34-
Learn more about creating cross-browser extensions at <a
35-
className="underline hover:no-underline"
36-
href="https://extension.js.org"
37-
target="_blank"
38-
>
39-
https://extension.js.org
40-
</a>
41-
</p>
42-
</div>
43-
`
27+
const style = document.createElement('style')
28+
shadowRoot.appendChild(style)
29+
30+
// Create content container div
31+
const contentDiv = document.createElement('div')
32+
contentDiv.className = 'content_script'
33+
shadowRoot.appendChild(contentDiv)
34+
35+
// Create logo image
36+
const logoImg = document.createElement('img')
37+
logoImg.className = 'content_logo'
38+
logoImg.src = logo
39+
contentDiv.appendChild(logoImg)
40+
41+
// Create title
42+
const title = document.createElement('h1')
43+
title.className = 'content_title'
44+
title.textContent = 'Welcome to your Sass Extension'
45+
contentDiv.appendChild(title)
46+
47+
// Create description
48+
const description = document.createElement('p')
49+
description.className = 'content_description'
50+
description.innerHTML =
51+
'Learn more about creating cross-browser extensions at <a class="underline hover:no-underline" href="https://extension.js.org" target="_blank">https://extension.js.org</a>'
52+
contentDiv.appendChild(description)
53+
54+
// Handle CSS injection
55+
fetchCSS().then((response) => {
56+
style.textContent = response
57+
})
58+
59+
import.meta.webpackHot?.accept('./styles.scss', () => {
60+
fetchCSS().then((response) => {
61+
style.textContent = response
62+
})
63+
})
64+
65+
return () => {
66+
rootDiv.remove()
67+
}
4468
}
69+
70+
async function fetchCSS() {
71+
const response = await fetch(new URL('./styles.scss', import.meta.url))
72+
const text = await response.text()
73+
return response.ok ? text : Promise.reject(text)
74+
}
75+

examples/content-sass/content/styles.scss

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@
1919
}
2020

2121
.content_title {
22-
font-size: 1.85em;
22+
font: {
23+
size: 1.85em;
24+
family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
25+
'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
26+
weight: 700;
27+
}
2328
line-height: 1.1;
24-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
25-
'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
26-
font-weight: 700;
2729
margin: 0;
2830
}
2931

3032
.content_description {
3133
font-size: small;
3234
margin: 0;
33-
}
3435

35-
.content_description a {
36-
text-decoration: none;
37-
border-bottom: 2px solid #c9c9c9;
38-
color: #e5e7eb;
39-
margin: 0;
36+
a {
37+
text-decoration: none;
38+
border-bottom: 2px solid #c9c9c9;
39+
color: #e5e7eb;
40+
margin: 0;
41+
}
4042
}

0 commit comments

Comments
 (0)