-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1063a07
commit d3eee59
Showing
13 changed files
with
4,203 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
/node_modules | ||
/lib | ||
/build | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { merge } from 'lodash'; | ||
import CookieConsentStore from './CookieConsentStore'; | ||
import CookieConsentNotification from './CookieConsentNotification'; | ||
import AutoAcceptRequestWatcher from './AutoAcceptRequestWatcher'; | ||
import { isCurrentPageExcluded } from './helpers'; | ||
import { OptionsType, UserOptionsType } from './types'; | ||
|
||
class FHCookieGuard { | ||
public cookieConsentNotificationElement: HTMLElement | null; | ||
public cookieConsentNotification ?: CookieConsentNotification; | ||
public cookieConsentStore: CookieConsentStore; | ||
private options: OptionsType; | ||
|
||
constructor(notificationElementSelector = '.js-cookie-alert', options: UserOptionsType = {}) { | ||
this.cookieConsentNotificationElement = document.querySelector(notificationElementSelector); | ||
|
||
const defaultOptions: OptionsType = { | ||
selectors: { | ||
accept: '.js-cookie-alert-accept', | ||
refuse: '.js-cookie-alert-refuse', | ||
cookieGuard: '.js-cookie-guarded', | ||
parentContainer: 'body' | ||
}, | ||
cookieName: 'cookies-accepted', | ||
cookieAttributes: { | ||
expires: 90, | ||
domain: window.location.hostname, | ||
path: '/', | ||
}, | ||
autoAcceptCookieConsentAfterRequestCount: null, | ||
activeClass: 'cookie-alert-is-active', | ||
excludedPageUrls: [], | ||
callbacks: { | ||
onOpenCookieAlert: null, | ||
onCloseCookieAlert: null, | ||
} | ||
}; | ||
|
||
// merge default options with user options coming from initialisation and data attributes on notification element | ||
this.options = merge<OptionsType, Partial<OptionsType>, Partial<OptionsType>>( | ||
defaultOptions, | ||
options, | ||
this.cookieConsentNotificationElement ? this.cookieConsentNotificationElement.dataset : {} | ||
); | ||
|
||
this.cookieConsentStore = new CookieConsentStore( | ||
this.options.cookieName, | ||
this.options.cookieAttributes, | ||
); | ||
|
||
this.init(); | ||
} | ||
|
||
public init() { | ||
this.initCookieConsentNotificationIfNeeded(); | ||
this.initAutoAcceptCookieConsentIfNeeded(); | ||
} | ||
|
||
private initCookieConsentNotificationIfNeeded() { | ||
if (!this.cookieConsentNotificationElement || this.cookieConsentStore.hasBeenSet() || isCurrentPageExcluded(this.options.excludedPageUrls)) { | ||
return; | ||
} | ||
|
||
this.cookieConsentNotification = new CookieConsentNotification( | ||
this.cookieConsentNotificationElement, | ||
this.cookieConsentStore, | ||
this.options, | ||
this.enableCookieGuardedContent | ||
); | ||
|
||
if (this.cookieConsentNotification) { | ||
this.cookieConsentNotification.show(); | ||
} | ||
} | ||
|
||
private initAutoAcceptCookieConsentIfNeeded() { | ||
const { autoAcceptCookieConsentAfterRequestCount, cookieAttributes } = this.options; | ||
|
||
if (autoAcceptCookieConsentAfterRequestCount === null || this.cookieConsentStore.hasBeenSet() || isCurrentPageExcluded(this.options.excludedPageUrls)) { | ||
return; | ||
} | ||
|
||
new AutoAcceptRequestWatcher( | ||
this.cookieConsentStore, | ||
autoAcceptCookieConsentAfterRequestCount, | ||
cookieAttributes, | ||
() => { | ||
this.enableCookieGuardedContent(); | ||
|
||
if (this.cookieConsentNotification) { | ||
this.cookieConsentNotification.close(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public enableCookieGuardedContent() { | ||
const { selectors } = this.options; | ||
|
||
const cookieGuardedElements: NodeList = document.querySelectorAll(selectors.cookieGuard); | ||
const parser = new DOMParser(); | ||
|
||
if (!cookieGuardedElements.length) { | ||
return; | ||
} | ||
|
||
Array.prototype.forEach.call(cookieGuardedElements, (element: HTMLElement) => { | ||
// element must have a content data attribute | ||
if (!element.dataset.content) { | ||
return; | ||
} | ||
|
||
// Get script from content attribute | ||
let content = parser.parseFromString(element.dataset.content, 'text/html'); | ||
let guardedScript = content.querySelector('script'); | ||
|
||
if (!guardedScript) { | ||
return; | ||
} | ||
|
||
// Create a new script element so DOM can execute | ||
let newScriptElement = document.createElement('script'); | ||
|
||
// Check if the new script tag has a external src | ||
if (guardedScript.src) { | ||
newScriptElement.src = guardedScript.src; | ||
} else { | ||
newScriptElement.innerHTML = guardedScript.innerHTML; | ||
} | ||
|
||
// Replace the meta tag with the new script element | ||
let parentNode = element.parentNode; | ||
|
||
if (parentNode) { | ||
parentNode.replaceChild(newScriptElement, element); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default FHCookieGuard; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
|
||
const CookiePopup = () => { | ||
return ( | ||
<div> | ||
Test fsdfds | ||
</div> | ||
); | ||
}; | ||
|
||
export default CookiePopup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1 @@ | ||
import { merge } from 'lodash'; | ||
import CookieConsentStore from './CookieConsentStore'; | ||
import CookieConsentNotification from './CookieConsentNotification'; | ||
import AutoAcceptRequestWatcher from './AutoAcceptRequestWatcher'; | ||
import { isCurrentPageExcluded } from './helpers'; | ||
import { OptionsType, UserOptionsType } from './types'; | ||
|
||
class FHCookieGuard { | ||
public cookieConsentNotificationElement: HTMLElement | null; | ||
public cookieConsentNotification ?: CookieConsentNotification; | ||
public cookieConsentStore: CookieConsentStore; | ||
private options: OptionsType; | ||
|
||
constructor(notificationElementSelector = '.js-cookie-alert', options: UserOptionsType = {}) { | ||
this.cookieConsentNotificationElement = document.querySelector(notificationElementSelector); | ||
|
||
const defaultOptions: OptionsType = { | ||
selectors: { | ||
accept: '.js-cookie-alert-accept', | ||
refuse: '.js-cookie-alert-refuse', | ||
cookieGuard: '.js-cookie-guarded', | ||
parentContainer: 'body' | ||
}, | ||
cookieName: 'cookies-accepted', | ||
cookieAttributes: { | ||
expires: 90, | ||
domain: window.location.hostname, | ||
path: '/', | ||
}, | ||
autoAcceptCookieConsentAfterRequestCount: null, | ||
activeClass: 'cookie-alert-is-active', | ||
excludedPageUrls: [], | ||
callbacks: { | ||
onOpenCookieAlert: null, | ||
onCloseCookieAlert: null, | ||
} | ||
}; | ||
|
||
// merge default options with user options coming from initialisation and data attributes on notification element | ||
this.options = merge<OptionsType, Partial<OptionsType>, Partial<OptionsType>>( | ||
defaultOptions, | ||
options, | ||
this.cookieConsentNotificationElement ? this.cookieConsentNotificationElement.dataset : {} | ||
); | ||
|
||
this.cookieConsentStore = new CookieConsentStore( | ||
this.options.cookieName, | ||
this.options.cookieAttributes, | ||
); | ||
|
||
this.init(); | ||
} | ||
|
||
public init() { | ||
this.initCookieConsentNotificationIfNeeded(); | ||
this.initAutoAcceptCookieConsentIfNeeded(); | ||
} | ||
|
||
private initCookieConsentNotificationIfNeeded() { | ||
if (!this.cookieConsentNotificationElement || this.cookieConsentStore.hasBeenSet() || isCurrentPageExcluded(this.options.excludedPageUrls)) { | ||
return; | ||
} | ||
|
||
this.cookieConsentNotification = new CookieConsentNotification( | ||
this.cookieConsentNotificationElement, | ||
this.cookieConsentStore, | ||
this.options, | ||
this.enableCookieGuardedContent | ||
); | ||
|
||
if (this.cookieConsentNotification) { | ||
this.cookieConsentNotification.show(); | ||
} | ||
} | ||
|
||
private initAutoAcceptCookieConsentIfNeeded() { | ||
const { autoAcceptCookieConsentAfterRequestCount, cookieAttributes } = this.options; | ||
|
||
if (autoAcceptCookieConsentAfterRequestCount === null || this.cookieConsentStore.hasBeenSet() || isCurrentPageExcluded(this.options.excludedPageUrls)) { | ||
return; | ||
} | ||
|
||
new AutoAcceptRequestWatcher( | ||
this.cookieConsentStore, | ||
autoAcceptCookieConsentAfterRequestCount, | ||
cookieAttributes, | ||
() => { | ||
this.enableCookieGuardedContent(); | ||
|
||
if (this.cookieConsentNotification) { | ||
this.cookieConsentNotification.close(); | ||
} | ||
} | ||
); | ||
} | ||
|
||
public enableCookieGuardedContent() { | ||
const { selectors } = this.options; | ||
|
||
const cookieGuardedElements: NodeList = document.querySelectorAll(selectors.cookieGuard); | ||
const parser = new DOMParser(); | ||
|
||
if (!cookieGuardedElements.length) { | ||
return; | ||
} | ||
|
||
Array.prototype.forEach.call(cookieGuardedElements, (element: HTMLElement) => { | ||
// element must have a content data attribute | ||
if (!element.dataset.content) { | ||
return; | ||
} | ||
|
||
// Get script from content attribute | ||
let content = parser.parseFromString(element.dataset.content, 'text/html'); | ||
let guardedScript = content.querySelector('script'); | ||
|
||
if (!guardedScript) { | ||
return; | ||
} | ||
|
||
// Create a new script element so DOM can execute | ||
let newScriptElement = document.createElement('script'); | ||
|
||
// Check if the new script tag has a external src | ||
if (guardedScript.src) { | ||
newScriptElement.src = guardedScript.src; | ||
} else { | ||
newScriptElement.innerHTML = guardedScript.innerHTML; | ||
} | ||
|
||
// Replace the meta tag with the new script element | ||
let parentNode = element.parentNode; | ||
|
||
if (parentNode) { | ||
parentNode.replaceChild(newScriptElement, element); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default FHCookieGuard; | ||
export * from './components/CookiePopup'; |
Oops, something went wrong.