Skip to content

Commit 4033d06

Browse files
Merge pull request #148 from opencomponents/add-nonce
add nonce property
2 parents ac03d51 + 67c0c45 commit 4033d06

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"[javascript]": {
3+
"editor.defaultFormatter": "biomejs.biome"
4+
}
5+
}

src/oc-client.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export function createOc(oc) {
3838
let renderedComponents = oc.renderedComponents;
3939
let dataRenderedAttribute = "data-rendered";
4040
let dataRenderingAttribute = "data-rendering";
41+
let nonce = $document.currentScript?.nonce;
42+
4143
let logError = (msg) => console.log(msg);
4244
let logInfo = (msg) => ocConf.debug && console.log(msg);
4345
let handleFetchResponse = (response) => {
@@ -124,6 +126,9 @@ export function createOc(oc) {
124126
for (let attribute of Array.from(script.attributes)) {
125127
newScript.setAttribute(attribute.name, attribute.value);
126128
}
129+
if (nonce) {
130+
newScript.setAttribute("nonce", nonce);
131+
}
127132
script.parentNode?.replaceChild(newScript, script);
128133
}
129134
};
@@ -140,6 +145,9 @@ export function createOc(oc) {
140145
oc.addStylesToHead = (styles) => {
141146
let style = $document.createElement("style");
142147
style.textContent = styles;
148+
if (nonce) {
149+
style.setAttribute("nonce", nonce);
150+
}
143151
$document.head.appendChild(style);
144152
};
145153

0 commit comments

Comments
 (0)