Skip to content

Commit 51ed130

Browse files
committed
Update types for content-security-policy-parser and preact
1 parent f13f4fb commit 51ed130

File tree

7 files changed

+297
-326
lines changed

7 files changed

+297
-326
lines changed

packages/run-chrome-extension/steps/SetupReloadStrategy/ApplyManifestDevDefaults/patchCSP.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ export function patchV2CSP(manifest: ManifestBase) {
77
if (!policy) {
88
return (
99
"script-src 'self' 'unsafe-eval' blob: filesystem:; " +
10-
"object-src 'self' blob: filesystem:; " // +
10+
"object-src 'self' blob: filesystem:; "
1111
)
1212
}
1313

14-
const csp = parse(policy)
14+
let csp = parse(policy)
1515
policy = ''
1616

1717
// Modification logic remains the same
18-
if (!csp['script-src']) {
19-
csp['script-src'] = ["'self' 'unsafe-eval' blob: filesystem:"]
18+
if (!csp.get('script-src')) {
19+
csp.set('script-src', ["'self' 'unsafe-eval' blob: filesystem:"])
2020
}
21-
if (!csp['script-src'].includes("'unsafe-eval'")) {
22-
csp['script-src'].push("'unsafe-eval'")
21+
if (!csp.get('script-src')?.includes("'unsafe-eval'")) {
22+
csp.set('script-src', ['unsafe-eval'])
2323
}
2424

2525
for (const k in csp) {
26-
policy += `${k} ${csp[k].join(' ')};`
26+
policy += `${k} ${csp.get(k)?.join(' ')};`
2727
}
2828

29-
// Update the CSP in the manifest
3029
return policy
3130
}
3231

@@ -35,23 +34,19 @@ export function patchV3CSP(manifest: ManifestBase) {
3534
const policy: {extension_pages: string} | undefined =
3635
manifest.content_security_policy
3736

38-
// Check if a policy exists, if not, apply a default one
3937
if (!policy) {
4038
return {
41-
extension_pages: "script-src 'self'; " + "object-src 'self'; " // + "connect-src 'self' ws:;"
39+
extension_pages: "script-src 'self'; " + "object-src 'self'; "
4240
}
4341
}
4442

45-
// Parse the CSP to a manageable format
4643
const csp = parse(policy.extension_pages)
4744
let extensionPagesPolicy = ''
4845

49-
// Rebuild the CSP string
5046
for (const directive in csp) {
51-
extensionPagesPolicy += `${directive} ${csp[directive].join(' ')}; `
47+
extensionPagesPolicy += `${directive} ${csp.get(directive)?.join(' ')}; `
5248
}
5349

54-
// Update the manifest's CSP
5550
return {
5651
extension_pages: extensionPagesPolicy.trim()
5752
}

packages/run-edge-extension/steps/SetupReloadStrategy/ApplyManifestDevDefaults/patchCSP.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ export function patchV2CSP(manifest: ManifestBase) {
1111
)
1212
}
1313

14-
const csp = parse(policy)
14+
let csp = parse(policy)
1515
policy = ''
1616

1717
// Modification logic remains the same
18-
if (!csp['script-src']) {
19-
csp['script-src'] = ["'self' 'unsafe-eval' blob: filesystem:"]
18+
if (!csp.get('script-src')) {
19+
csp.set('script-src', ["'self' 'unsafe-eval' blob: filesystem:"])
2020
}
21-
if (!csp['script-src'].includes("'unsafe-eval'")) {
22-
csp['script-src'].push("'unsafe-eval'")
21+
if (!csp.get('script-src')?.includes("'unsafe-eval'")) {
22+
csp.set('script-src', ['unsafe-eval'])
2323
}
2424

2525
for (const k in csp) {
26-
policy += `${k} ${csp[k].join(' ')};`
26+
policy += `${k} ${csp.get(k)?.join(' ')};`
2727
}
2828

2929
return policy
@@ -44,7 +44,7 @@ export function patchV3CSP(manifest: ManifestBase) {
4444
let extensionPagesPolicy = ''
4545

4646
for (const directive in csp) {
47-
extensionPagesPolicy += `${directive} ${csp[directive].join(' ')}; `
47+
extensionPagesPolicy += `${directive} ${csp.get(directive)?.join(' ')}; `
4848
}
4949

5050
return {

packages/run-firefox-addon/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"dependencies": {
5050
"@colors/colors": "^1.6.0",
5151
"browser-extension-manifest-fields": "*",
52-
"content-security-policy-parser": "^0.4.1",
52+
"content-security-policy-parser": "^0.6.0",
5353
"firefox-location": "^1.0.2",
5454
"firefox-profile": "^4.6.0",
5555
"fx-runner": "1.4.0",

packages/run-firefox-addon/steps/SetupReloadStrategy/ApplyManifestDevDefaults/patchCSP.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ export function patchV2CSP(manifest: ManifestBase) {
77
if (!policy) {
88
return (
99
"script-src 'self' 'unsafe-eval' blob: filesystem:; " +
10-
"object-src 'self' blob: filesystem:; " // +
10+
"object-src 'self' blob: filesystem:; "
1111
)
1212
}
1313

14-
const csp = parse(policy)
14+
let csp = parse(policy)
1515
policy = ''
1616

1717
// Modification logic remains the same
18-
if (!csp['script-src']) {
19-
csp['script-src'] = ["'self' 'unsafe-eval' blob: filesystem:"]
18+
if (!csp.get('script-src')) {
19+
csp.set('script-src', ["'self' 'unsafe-eval' blob: filesystem:"])
2020
}
21-
if (!csp['script-src'].includes("'unsafe-eval'")) {
22-
csp['script-src'].push("'unsafe-eval'")
21+
if (!csp.get('script-src')?.includes("'unsafe-eval'")) {
22+
csp.set('script-src', ['unsafe-eval'])
2323
}
2424

2525
for (const k in csp) {
26-
policy += `${k} ${csp[k].join(' ')};`
26+
policy += `${k} ${csp.get(k)?.join(' ')};`
2727
}
2828

29-
// Update the CSP in the manifest
3029
return policy
3130
}
3231

@@ -35,23 +34,19 @@ export function patchV3CSP(manifest: ManifestBase) {
3534
const policy: {extension_pages: string} | undefined =
3635
manifest.content_security_policy
3736

38-
// Check if a policy exists, if not, apply a default one
3937
if (!policy) {
4038
return {
41-
extension_pages: "script-src 'self'; " + "object-src 'self'; " // + "connect-src 'self' ws:;"
39+
extension_pages: "script-src 'self'; " + "object-src 'self'; "
4240
}
4341
}
4442

45-
// Parse the CSP to a manageable format
4643
const csp = parse(policy.extension_pages)
4744
let extensionPagesPolicy = ''
4845

49-
// Rebuild the CSP string
5046
for (const directive in csp) {
51-
extensionPagesPolicy += `${directive} ${csp[directive].join(' ')}; `
47+
extensionPagesPolicy += `${directive} ${csp.get(directive)?.join(' ')}; `
5248
}
5349

54-
// Update the manifest's CSP
5550
return {
5651
extension_pages: extensionPagesPolicy.trim()
5752
}

programs/create/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
"prefers-yarn": "^1.0.1"
3535
},
3636
"devDependencies": {
37+
"@preact/signals": "^1.2.3",
3738
"@types/cross-spawn": "^6.0.6",
3839
"@types/react-dom": "^18.2.18",
40+
"preact": "^10.22.0",
3941
"tsconfig": "*",
4042
"tsup": "^8.0.1",
4143
"typescript": "5.3.3"

programs/create/templates/preact-typescript/template/content/ContentApp.tsx

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,90 +7,87 @@ import typescriptLogo from '../public/typescript.png'
77
import tailwindLogo from '../public/tailwind.png'
88
import chromeWindowBg from '../public/chromeWindow.png'
99

10-
export default class ContentApp extends Component {
11-
private isdialogOpen = signal(true)
10+
export default function ContentApp() {
11+
const isdialogOpen = signal(true)
1212

13-
constructor() {
14-
super()
15-
this.setIsDialogOpen(true)
13+
const setIsDialogOpen = (bool: boolean) => {
14+
isdialogOpen.value = bool
1615
}
1716

18-
setIsDialogOpen(bool: boolean) {
19-
this.isdialogOpen.value = bool
17+
if (!isdialogOpen) {
18+
return (
19+
<div className="mx-auto p-6">
20+
<button
21+
onClick={() => setIsDialogOpen(true)}
22+
className="bg-white rounded-md p-3 text-sm font-semibold text-gray-900 shadow-sm hover:bg-gray-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white"
23+
>
24+
🧩 Open content script hint <span aria-hidden="true">+</span>
25+
</button>
26+
</div>
27+
)
2028
}
2129

22-
render() {
23-
return this.isdialogOpen.value ? (
24-
<div className="mx-auto max-w-7xl md:px-0 lg:p-6">
25-
<div className="relative isolate overflow-hidden bg-gray-900 px-6 pt-16 shadow-2xl lg:rounded-3xl md:pt-24 md:h-full sm:h-[100vh] lg:flex lg:gap-x-20 lg:px-24 lg:pt-0">
26-
<div className="absolute z-20 top-0 inset-x-0 flex justify-center overflow-hidden pointer-events-none">
27-
<div className="w-[108rem] flex-none flex justify-end">
28-
<picture>
29-
<img
30-
src={tailwindBg}
31-
alt=""
32-
className="w-[90rem] flex-none max-w-none hidden dark:block"
33-
decoding="async"
34-
/>
35-
</picture>
36-
</div>
37-
</div>
38-
<div className="mx-auto max-w-md text-center lg:py-12 lg:mx-0 lg:flex-auto lg:text-left">
39-
<div className="flex items-center justify-center space-x-4 my-4 mx-auto">
40-
<img
41-
alt="Preact logo"
42-
src={preactLogo}
43-
className="relative inline-block w-12"
44-
/>
45-
<div className="text-3xl text-white">+</div>
30+
return (
31+
<div className="mx-auto max-w-7xl md:px-0 lg:p-6">
32+
<div className="relative isolate overflow-hidden bg-gray-900 px-6 pt-16 shadow-2xl lg:rounded-3xl md:pt-24 md:h-full sm:h-[100vh] lg:flex lg:gap-x-20 lg:px-24 lg:pt-0">
33+
<div className="absolute z-20 top-0 inset-x-0 flex justify-center overflow-hidden pointer-events-none">
34+
<div className="w-[108rem] flex-none flex justify-end">
35+
<picture>
4636
<img
47-
alt="TypeScript logo"
48-
src={typescriptLogo}
49-
className="relative inline-block w-12"
37+
src={tailwindBg}
38+
alt=""
39+
className="w-[90rem] flex-none max-w-none hidden dark:block"
40+
decoding="async"
5041
/>
51-
<div className="text-3xl text-white">+</div>
52-
<img
53-
alt="Tailwind logo"
54-
src={tailwindLogo}
55-
className="relative inline-block w-12"
56-
/>
57-
</div>
58-
<h2 className="text-3xl font-bold tracking-tight text-white sm:text-4xl">
59-
This is a content script running Preact, TypeScript, and
60-
Tailwind.css.
61-
</h2>
62-
<p className="mt-6 text-lg leading-8 text-gray-300">
63-
Learn more about creating cross-browser extensions by{' '}
64-
<button
65-
onClick={() => this.setIsDialogOpen(false)}
66-
className="underline hover:no-underline
67-
"
68-
>
69-
closing this hint
70-
</button>
71-
.
72-
</p>
42+
</picture>
7343
</div>
74-
<div className="relative mt-16 h-80 lg:mt-8">
44+
</div>
45+
<div className="mx-auto max-w-md text-center lg:py-12 lg:mx-0 lg:flex-auto lg:text-left">
46+
<div className="flex items-center justify-center space-x-4 my-4 mx-auto">
7547
<img
76-
className="absolute left-0 top-0 w-[57rem] max-w-none rounded-md bg-white/5 ring-1 ring-white/10"
77-
src={chromeWindowBg}
78-
alt="Chrome window screenshot"
79-
width="1824"
80-
height="1080"
48+
alt="Preact logo"
49+
src={preactLogo}
50+
className="relative inline-block w-12"
51+
/>
52+
<div className="text-3xl text-white">+</div>
53+
<img
54+
alt="TypeScript logo"
55+
src={typescriptLogo}
56+
className="relative inline-block w-12"
57+
/>
58+
<div className="text-3xl text-white">+</div>
59+
<img
60+
alt="Tailwind logo"
61+
src={tailwindLogo}
62+
className="relative inline-block w-12"
8163
/>
8264
</div>
65+
<h2 className="text-3xl font-bold tracking-tight text-white sm:text-4xl">
66+
This is a content script running React, TypeScript, and
67+
Tailwind.css.
68+
</h2>
69+
<p className="mt-6 text-lg leading-8 text-gray-300">
70+
Learn more about creating cross-browser extensions by{' '}
71+
<button
72+
onClick={() => setIsDialogOpen(false)}
73+
className="underline hover:no-underline
74+
"
75+
>
76+
closing this hint
77+
</button>
78+
.
79+
</p>
80+
</div>
81+
<div className="relative mt-16 h-80 lg:mt-8">
82+
<img
83+
className="absolute left-0 top-0 w-[57rem] max-w-none rounded-md bg-white/5 ring-1 ring-white/10"
84+
src={chromeWindowBg}
85+
alt="Chrome window screenshot"
86+
width="1824"
87+
height="1080"
88+
/>
8389
</div>
8490
</div>
85-
) : (
86-
<div className="mx-auto p-6">
87-
<button
88-
onClick={() => this.setIsDialogOpen(true)}
89-
className="bg-white rounded-md p-3 text-sm font-semibold text-gray-900 shadow-sm hover:bg-gray-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white"
90-
>
91-
🧩 Open content script hint <span aria-hidden="true">+</span>
92-
</button>
93-
</div>
94-
)
95-
}
91+
</div>
92+
)
9693
}

0 commit comments

Comments
 (0)