From 30548ab2556d9431fb02f87ee92a47f21d6b40f8 Mon Sep 17 00:00:00 2001 From: Martijn van Kekem Date: Mon, 23 Oct 2023 13:01:02 +0200 Subject: [PATCH 1/3] Allow nonce to be added to injected styles. --- src/index.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0a3fbac..926760d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,11 +76,18 @@ export default class Tooltip { */ private hidingTimeout; + /** + * The Content-Security-Policy nonce to apply on injected styles. + */ + private nonce: string; + /** * Module constructor + * + * @param {string} nonce - The nonce to apply to the injected styles. */ - constructor() { - this.loadStyles(); + constructor(nonce?: string) { + this.loadStyles(nonce); this.prepare(); window.addEventListener('scroll', this.handleWindowScroll, {passive: true}); @@ -234,7 +241,7 @@ export default class Tooltip { /** * Append CSS file */ - private loadStyles(): void { + private loadStyles(nonce?: string): void { const id = 'codex-tooltips-style'; if (document.getElementById(id)) { @@ -247,6 +254,11 @@ export default class Tooltip { id, }); + // Apply nonce to injected styles. + if (nonce) { + tag.setAttribute("nonce", nonce); + } + /** * Append styles at the top of HEAD tag */ From 7e111268e790314eee28a8b28a853548592f56a0 Mon Sep 17 00:00:00 2001 From: Martijn van Kekem Date: Mon, 23 Oct 2023 13:04:23 +0200 Subject: [PATCH 2/3] Update JSdoc --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index 926760d..98238b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -240,6 +240,8 @@ export default class Tooltip { /** * Append CSS file + * + * @param {string} nonce - The nonce to apply to the injected styles. */ private loadStyles(nonce?: string): void { const id = 'codex-tooltips-style'; From 06a1be67a561a14c4effa8cb3efb74fc0dc7171b Mon Sep 17 00:00:00 2001 From: Martijn van Kekem Date: Mon, 23 Oct 2023 13:33:19 +0200 Subject: [PATCH 3/3] Code optimization --- src/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 98238b2..345a837 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,11 +76,6 @@ export default class Tooltip { */ private hidingTimeout; - /** - * The Content-Security-Policy nonce to apply on injected styles. - */ - private nonce: string; - /** * Module constructor * @@ -258,7 +253,7 @@ export default class Tooltip { // Apply nonce to injected styles. if (nonce) { - tag.setAttribute("nonce", nonce); + tag.setAttribute('nonce', nonce); } /**