Skip to content

Commit 4906963

Browse files
committed
Improve coverage
1 parent 136de9a commit 4906963

File tree

17 files changed

+819
-223
lines changed

17 files changed

+819
-223
lines changed

examples/new/newtab/scripts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('Hello from the new tab page!')
1+
console.log('Hello from the new tab page')
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import logo from '../images/logo.svg'
2+
3+
let unmount
4+
5+
if (import.meta.webpackHot) {
6+
import.meta.webpackHot?.accept()
7+
import.meta.webpackHot?.dispose(() => unmount?.())
8+
}
9+
10+
console.log('hello from content_scripts')
11+
12+
if (document.readyState === 'complete') {
13+
unmount = initial() || (() => {})
14+
} else {
15+
document.addEventListener('readystatechange', () => {
16+
if (document.readyState === 'complete') unmount = initial() || (() => {})
17+
})
18+
}
19+
20+
function initial() {
21+
const rootDiv = document.createElement('div')
22+
rootDiv.id = 'extension-root'
23+
document.body.appendChild(rootDiv)
24+
25+
// Injecting content_scripts inside a shadow dom
26+
// prevents conflicts with the host page's styles.
27+
// This way, styles from the extension won't leak into the host page.
28+
const shadowRoot = rootDiv.attachShadow({mode: 'open'})
29+
30+
const styleElement = document.createElement('style')
31+
shadowRoot.appendChild(styleElement)
32+
fetchCSS().then((response) => (styleElement.textContent = response))
33+
34+
if (import.meta.webpackHot) {
35+
import.meta.webpackHot?.accept('./styles.css', () => {
36+
fetchCSS().then((response) => (styleElement.textContent = response))
37+
})
38+
}
39+
40+
// Create container div
41+
const contentDiv = document.createElement('div')
42+
contentDiv.className = 'content_script'
43+
44+
// Create and append logo image
45+
const img = document.createElement('img')
46+
img.className = 'content_logo'
47+
img.src = logo
48+
contentDiv.appendChild(img)
49+
50+
// Create and append title
51+
const title = document.createElement('h1')
52+
title.className = 'content_title'
53+
title.textContent = 'Welcome to your Special Folders (Scripts) Extension'
54+
contentDiv.appendChild(title)
55+
56+
// Create and append description paragraph
57+
const desc = document.createElement('p')
58+
desc.className = 'content_description'
59+
desc.innerHTML = 'Learn more about creating cross-browser extensions at '
60+
61+
const link = document.createElement('a')
62+
63+
link.href = 'https://extension.js.org'
64+
link.target = '_blank'
65+
link.textContent = 'https://extension.js.org'
66+
67+
desc.appendChild(link)
68+
contentDiv.appendChild(desc)
69+
70+
// Append the content div to shadow root
71+
shadowRoot.appendChild(contentDiv)
72+
73+
return () => {
74+
rootDiv.remove()
75+
}
76+
}
77+
78+
async function fetchCSS() {
79+
const cssUrl = new URL('./styles.css', import.meta.url)
80+
const response = await fetch(cssUrl)
81+
const text = await response.text()
82+
return response.ok ? text : Promise.reject(text)
83+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.content_script {
2+
color: #c9c9c9;
3+
background-color: #0a0c10;
4+
position: fixed;
5+
right: 0;
6+
bottom: 0;
7+
z-index: 9;
8+
width: 315px;
9+
margin: 1rem;
10+
padding: 2rem 1rem;
11+
display: flex;
12+
flex-direction: column;
13+
gap: 1em;
14+
border-radius: 6px;
15+
z-index: 9999;
16+
}
17+
18+
.content_logo {
19+
width: 72px;
20+
}
21+
22+
.content_title {
23+
font-size: 1.85em;
24+
line-height: 1.1;
25+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
26+
'Helvetica Neue', Arial, 'Noto Sans', sans-serif;
27+
font-weight: 700;
28+
margin: 0;
29+
}
30+
31+
.content_description {
32+
font-size: small;
33+
margin: 0;
34+
}
35+
36+
.content_description a {
37+
text-decoration: none;
38+
border-bottom: 2px solid #c9c9c9;
39+
color: #e5e7eb;
40+
margin: 0;
41+
}
Lines changed: 3 additions & 0 deletions
Loading

examples/special-folders-scripts/manifest.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
"icons": {
88
"48": "images/extension_48.png"
99
},
10+
"content_scripts": [
11+
{
12+
"matches": ["<all_urls>"],
13+
"js": ["content/scripts.js"]
14+
}
15+
],
1016
"background": {
1117
"chromium:service_worker": "background.js",
1218
"firefox:scripts": ["background.js"]
1319
},
1420
"permissions": ["scripting", "webNavigation", "storage"],
1521
"host_permissions": ["https://extension.js.org/*"],
16-
"action": {}
22+
"action": {},
23+
"user_scripts": {
24+
"api_script": "user_scripts/api-script.js"
25+
}
1726
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
2-
"private": true,
3-
"name": "special-folders-scripts",
4-
"description": "An Extension.js example.",
5-
"version": "0.0.1",
6-
"author": {
7-
"name": "Cezar Augusto",
8-
"email": "[email protected]",
9-
"url": "https://cezaraugusto.com"
2+
"name": "scripts-plugin-test",
3+
"version": "1.0.0",
4+
"description": "Test fixture for ScriptsPlugin",
5+
"scripts": {
6+
"build": "extension.js build",
7+
"dev": "extension.js dev"
108
},
11-
"license": "MIT",
12-
"type": "module"
9+
"devDependencies": {}
1310
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
// Included script with HMR
2+
if (module.hot) {
3+
module.hot.accept()
4+
}
5+
6+
console.log('Included script loaded')
7+
18
const text = `Your browser extension injected this script`
29
alert(text)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// User script with HMR
2+
if (module.hot) {
3+
module.hot.accept()
4+
}
5+
6+
console.log('User script loaded')

programs/cli/install_scripts.sh

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,41 @@ CLI_DIR="$(dirname "$SCRIPT_DIR")"
1515
SOURCE_README="$ROOT_DIR/README.md"
1616
TARGET_README="$CLI_DIR/README.md"
1717

18-
# Function to copy file if content is different
19-
copy_if_different() {
18+
# Function to copy and modify file if content is different
19+
copy_and_modify_if_different() {
2020
local source="$1"
2121
local target="$2"
2222

2323
if [ -f "$source" ]; then
2424
if [ -f "$target" ]; then
2525
# Compare files
2626
if ! cmp -s "$source" "$target"; then
27-
cp "$source" "$target"
28-
echo "[Extension.js setup] File $(basename "$source") copied to $target"
27+
# Create a temporary file
28+
TMP_FILE=$(mktemp)
29+
# Copy and modify the content
30+
sed -e 's/width="20%"/width="15.5%"/' \
31+
-e '/\[coverage-image\].*coverage-url\]/d' \
32+
"$source" > "$TMP_FILE"
33+
# Move the modified content to target
34+
mv "$TMP_FILE" "$target"
35+
echo "[Extension.js setup] File $(basename "$source") copied and modified to $target"
2936
else
3037
echo "[Extension.js setup] File $(basename "$source") haven't changed. Skipping copy..."
3138
fi
3239
else
33-
# Target doesn't exist, copy directly
34-
cp "$source" "$target"
35-
echo "[Extension.js setup] File $(basename "$source") copied to $target"
40+
# Target doesn't exist, create with modifications
41+
sed -e 's/width="20%"/width="15.5%"/' \
42+
-e '/\[coverage-image\].*coverage-url\]/d' \
43+
"$source" > "$target"
44+
echo "[Extension.js setup] File $(basename "$source") copied and modified to $target"
3645
fi
3746
else
3847
echo "Error: Source file $source not found"
3948
exit 1
4049
fi
4150
}
4251

43-
# Copy README.md
44-
copy_if_different "$SOURCE_README" "$TARGET_README"
52+
# Copy and modify README.md
53+
copy_and_modify_if_different "$SOURCE_README" "$TARGET_README"
4554

46-
echo '►►► All tasks completed'
55+
echo '►►► All tasks completed'

programs/develop/rslib.config.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ export default defineConfig({
1010
__dirname,
1111
'./webpack/plugin-extension/feature-html/steps/ensure-hmr-for-scripts.ts'
1212
),
13-
// Scripts Plugin Loaders
14-
'add-hmr-accept-code': path.resolve(
15-
__dirname,
16-
'./webpack/plugin-extension/feature-scripts/steps/add-hmr-accept-code.ts'
17-
),
1813
'deprecated-shadow-root': path.resolve(
1914
__dirname,
2015
'./webpack/plugin-extension/feature-scripts/steps/deprecated-shadow-root.ts'

0 commit comments

Comments
 (0)