2020-09-18 15:15:37 +00:00
|
|
|
import * as plugins from './webclient.plugins';
|
|
|
|
|
2020-09-18 16:13:57 +00:00
|
|
|
export class CsWebclient {
|
2020-09-19 15:18:28 +00:00
|
|
|
webstore: plugins.webstore.WebStore<plugins.csInterfaces.IWebclientSettings>;
|
2020-09-18 16:13:57 +00:00
|
|
|
|
|
|
|
constructor() {
|
2020-09-19 15:18:28 +00:00
|
|
|
this.webstore = new plugins.webstore.WebStore<plugins.csInterfaces.IWebclientSettings>({
|
2020-09-18 16:13:57 +00:00
|
|
|
dbName: 'consentsoftware',
|
2020-09-22 14:49:13 +00:00
|
|
|
storeName: 'webclient',
|
2020-09-18 16:13:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async isCookieLevelSet(): Promise<boolean> {
|
|
|
|
const result = await this.webstore.get('acceptedCookieLevels');
|
|
|
|
return !!result;
|
|
|
|
}
|
|
|
|
|
2020-09-22 14:49:13 +00:00
|
|
|
public async setCookieLevels(
|
|
|
|
cookieLevelsArg: plugins.csInterfaces.TCookieLevel[]
|
|
|
|
): Promise<boolean> {
|
2020-09-19 15:18:28 +00:00
|
|
|
await this.webstore.set('acceptedCookieLevels', {
|
|
|
|
acceptanceTimestamp: Date.now(),
|
2020-09-22 14:49:13 +00:00
|
|
|
acceptedCookieLevels: cookieLevelsArg,
|
2020-09-19 15:18:28 +00:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 14:49:13 +00:00
|
|
|
public async getCookieLevels(): Promise<
|
|
|
|
plugins.csInterfaces.IWebclientSettings['acceptedCookieLevels']
|
|
|
|
> {
|
2020-09-19 15:18:28 +00:00
|
|
|
const result = await this.webstore.get('acceptedCookieLevels');
|
|
|
|
return result.acceptedCookieLevels;
|
|
|
|
}
|
2020-09-18 16:13:57 +00:00
|
|
|
|
2020-09-19 15:18:28 +00:00
|
|
|
public async isCookieLevelStillValid(): Promise<boolean> {
|
2020-09-22 14:49:13 +00:00
|
|
|
if (!this.isCookieLevelSet) {
|
2020-09-19 15:18:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const result = await this.webstore.get('acceptedCookieLevels');
|
2020-09-22 14:49:13 +00:00
|
|
|
if (
|
|
|
|
result.acceptanceTimestamp <
|
|
|
|
Date.now() - plugins.smarttime.getMilliSecondsFromUnits({ months: 3 })
|
|
|
|
) {
|
2020-09-19 15:18:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-09-18 16:13:57 +00:00
|
|
|
}
|
2020-09-22 14:49:13 +00:00
|
|
|
|
|
|
|
public async getAndRunConsentTuples() {
|
|
|
|
const acceptedCookieLevels = await this.getCookieLevels();
|
|
|
|
if (!acceptedCookieLevels) {
|
|
|
|
console.log('You need to set accepted cookielevels first');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const csGetDomainSettingsRequest = new plugins.typedrequest.TypedRequest<
|
|
|
|
plugins.csInterfaces.IRequest_Client_ConsentSoftwareServer_GetDomainSettings
|
|
|
|
>('https://connect.api.global/consentsoftware', 'getDomainSettings');
|
|
|
|
const response = await csGetDomainSettingsRequest.fire({
|
|
|
|
domain: window.location.hostname
|
|
|
|
});
|
|
|
|
for (const consentTuple of response.consentTuples) {
|
|
|
|
if (consentTuple.level === 'functional' || acceptedCookieLevels.includes(consentTuple.level)) {
|
|
|
|
const scriptString = consentTuple.script as string;
|
|
|
|
// tslint:disable-next-line: function-constructor
|
|
|
|
const tupleFunction = new Function(scriptString);
|
|
|
|
await tupleFunction();
|
|
|
|
console.log (`Successfully executed ConsentTuple >>${consentTuple.name}<< -> ${consentTuple.description}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|