Skip to content

Commit 9530a87

Browse files
committed
Fixes the Gist style will be blocked by CSP.
1 parent dc0b93e commit 9530a87

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
## master
44

5+
- Fixes the gist style will be blocked by CSP.
6+
- Fixed CORS issues (#9).
7+
58
## 0.9.1
69

7-
- Fixes gist won't render when the username contains character `-`. (Fixes #7)
10+
- Fixes gist won't render when the username contains character `-`. (Fixes #7).
811

912
## 0.9.0
1013

src/gist_processor.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { nanoid } from 'nanoid';
22

33
import GistPluginSettings from './settings';
4-
import {request, RequestUrlParam} from "obsidian";
4+
import { request, RequestUrlParam } from "obsidian";
55

66
type GistJSON = {
77
description: string,
@@ -96,6 +96,7 @@ class GistProcessor {
9696
container.classList.add(`${pluginName}-container`)
9797
container.setAttribute('sandbox', 'allow-scripts allow-top-navigation-by-user-activation')
9898
container.setAttribute('loading', 'lazy')
99+
// container.setAttribute('csp', "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://github.githubassets.com;")
99100

100101
// reset the default things on HTML
101102
const resetStylesheet = `
@@ -127,15 +128,13 @@ class GistProcessor {
127128
</script>
128129
`
129130

130-
// build stylesheet link
131-
const stylesheetLink = document.createElement('link');
132-
stylesheetLink.rel = "stylesheet";
133-
stylesheetLink.href = gistJSON.stylesheet
134-
135131
// hack to make links open in the parent
136132
const parentLinkHack = document.createElement('base')
137133
parentLinkHack.target = "_parent"
138134

135+
// load stylesheet as text
136+
const stylesheetText = await this._loadStylesheet(el, gistID, gistJSON.stylesheet);
137+
139138
// custom stylesheet
140139
let customStylesheet = ""
141140
if (this.settings.styleSheet && this.settings.styleSheet.length > 0) {
@@ -151,8 +150,10 @@ class GistProcessor {
151150
${parentLinkHack.outerHTML}
152151
${heightAdjustmentScript}
153152
154-
<!-- gist style -->
155-
${stylesheetLink.outerHTML}
153+
<!-- gist embedded style -->
154+
<style>
155+
${stylesheetText}
156+
</style>
156157
157158
<!-- custom style -->
158159
<style>
@@ -181,6 +182,23 @@ Error:
181182

182183
el.createEl('pre', { text: errorText })
183184
}
185+
186+
private async _loadStylesheet(el: HTMLElement, gistString: String, url: string) {
187+
const urlParam: RequestUrlParam = {
188+
url: url,
189+
method: "GET",
190+
headers: {
191+
"Accept": "text/css"
192+
}
193+
}
194+
195+
try {
196+
const res = await request(urlParam)
197+
return res;
198+
} catch (error) {
199+
return this._showError(el, gistString, `Could not fetch the Gist Style from GitHub server. (Error: ${error})`)
200+
}
201+
}
184202
}
185203

186204
export { GistProcessor }

0 commit comments

Comments
 (0)