webclient/ts/index.ts

88 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-08-01 18:02:06 +02:00
import * as plugins from './webclient.plugins.js';
2020-09-18 15:15:37 +00:00
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-24 12:35:31 +00:00
const domainToRequest = domainArg || window.location.hostname;
2020-09-22 14:49:13 +00:00
const response = await csGetDomainSettingsRequest.fire({
2020-10-24 12:35:31 +00:00
domain: domainToRequest,
2020-09-22 14:49:13 +00:00
});
for (const consentTuple of response.consentTuples) {
2020-10-24 12:35:31 +00:00
if (
consentTuple.level === 'functional' ||
acceptedCookieLevels.includes(consentTuple.level)
) {
2020-09-22 14:49:13 +00:00
const scriptString = consentTuple.script as string;
// tslint:disable-next-line: function-constructor
2020-10-24 12:35:31 +00:00
const tupleFunction: plugins.csInterfaces.IConsentTuple['script'] = new Function(
'dataArg',
'cookieLevelsArg',
`return (${scriptString})(dataArg, cookieLevelsArg)`
) as plugins.csInterfaces.IConsentTuple['script'];
if (typeof tupleFunction === 'function') {
await tupleFunction(consentTuple.scriptExecutionDataArg, await this.getCookieLevels());
} else {
const errorText = 'got malformed script to execuute';
console.error(errorText);
throw new Error(errorText);
}
console.log(
`Successfully executed ConsentTuple >>${consentTuple.name}<< -> ${consentTuple.description}`
);
2020-09-22 14:49:13 +00:00
}
}
}
}