Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nonce to be added to injected stylesheets. #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ export default class Tooltip {

/**
* 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});
Expand Down Expand Up @@ -233,8 +235,10 @@ export default class Tooltip {

/**
* Append CSS file
*
* @param {string} nonce - The nonce to apply to the injected styles.
*/
private loadStyles(): void {
private loadStyles(nonce?: string): void {
const id = 'codex-tooltips-style';

if (document.getElementById(id)) {
Expand All @@ -247,6 +251,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
*/
Expand Down