Skip to content

Commit cd3e1a6

Browse files
committed
feat: add option to remove CSP in webview
1 parent f82c8ba commit cd3e1a6

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ According to [#34](https://github.com/subframe7536/vscode-custom-ui-style/issues
248248
| `custom-ui-style.external.imports` | External CSS or JS resources, support variable: [${userHome}, ${env:your_env_name:optional_fallback_value}], support protocol: 'https://', 'file://' | `array` | `` |
249249
| `custom-ui-style.stylesheet` | Custom css for editor, support nest selectors | `object` | `{}` |
250250
| `custom-ui-style.webview.enable` | Enable style patch in webview | `boolean` | `true` |
251+
| `custom-ui-style.webview.removeCSP` | Remove Content-Security-Policy restrict in webview | `boolean` | `true` |
251252
| `custom-ui-style.webview.monospaceSelector` | Custom monospace selector in webview | `array` | `` |
252253
| `custom-ui-style.webview.sansSerifSelector` | Custom sans-serif selector in webview | `array` | `` |
253254
| `custom-ui-style.webview.stylesheet` | Custom css for webview, support nest selectors | `object` | `{}` |

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@
176176
"default": true,
177177
"description": "Enable style patch in webview"
178178
},
179+
"custom-ui-style.webview.removeCSP": {
180+
"scope": "application",
181+
"type": "boolean",
182+
"default": true,
183+
"description": "Remove Content-Security-Policy restrict in webview"
184+
},
179185
"custom-ui-style.webview.monospaceSelector": {
180186
"scope": "application",
181187
"type": "array",

src/manager/webview.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function getCSS() {
2727
return result
2828
}
2929

30-
export function fixSha256(html: string) {
30+
export function fixCSP(html: string, remove: boolean) {
31+
if (remove) {
32+
return html.replace('meta http-equiv="Content-Security-Policy"', 'meta http-equiv=""')
33+
}
3134
const [, scriptString] = html.match(/<script async=?"{0,2} type="module">([\s\S]*?)<\/script>/) || []
3235
if (!scriptString) {
3336
return html
@@ -46,11 +49,12 @@ export class WebViewFileManager extends BaseFileManager {
4649
if (!config['webview.enable']) {
4750
return content
4851
}
49-
return fixSha256(
52+
return fixCSP(
5053
content.replace(
5154
entry,
5255
`${getCSS()}\n\n\t\t\t${entry}`,
5356
),
57+
config['webview.removeCSP'],
5458
)
5559
}
5660
}

0 commit comments

Comments
 (0)