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');
|
2020-10-20 21:50:21 +00:00
|
|
|
|
return result?.acceptedCookieLevels;
|
2020-09-19 15:18:28 +00:00
|
|
|
|
}
|
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
|
|
|
|
|
2020-10-20 21:02:14 +00:00
|
|
|
|
public async getAndRunConsentTuples(domainArg?: string) {
|
2020-09-22 14:49:13 +00:00
|
|
|
|
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');
|
2020-10-20 21:02:14 +00:00
|
|
|
|
const domainToRequest = domainArg || window.location.hostname;
|
2020-09-22 14:49:13 +00:00
|
|
|
|
const response = await csGetDomainSettingsRequest.fire({
|
2020-10-20 21:02:14 +00:00
|
|
|
|
domain: domainToRequest
|
2020-09-22 14:49:13 +00:00
|
|
|
|
});
|
|
|
|
|
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
|
2020-10-20 23:52:31 +00:00
|
|
|
|
const tupleFunction = new Function(`return (${scriptString})()`);
|
2020-10-20 22:09:35 +00:00
|
|
|
|
await tupleFunction(consentTuple.scriptExecutionDataArg);
|
2020-09-22 14:49:13 +00:00
|
|
|
|
console.log (`Successfully executed ConsentTuple >>${consentTuple.name}<< -> ${consentTuple.description}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|