Skip to content

Commit

Permalink
WIP: eval snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
muodov committed Aug 17, 2023
1 parent 68280db commit c700b5e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
24 changes: 16 additions & 8 deletions lib/cmps/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { enableLogs } from "../config";
import { click, elementExists, elementVisible, hide, wait, waitForElement, waitForThenClick, waitForVisible } from "../rule-executors";
import { requestEval } from "../eval-handler";
import AutoConsent from "../web";
import { getFunctionBody, snippets } from "../eval-snippets";

export async function success(action: Promise<boolean>): Promise<boolean> {
const result = await action;
Expand Down Expand Up @@ -42,22 +43,29 @@ export default class AutoConsentCMPBase implements AutoCMP {
throw new Error('Not Implemented');
}

mainWorldEval(expr: string): Promise<boolean> {
mainWorldEval(snippetId: keyof typeof snippets): Promise<boolean> {
const snippet = snippets[snippetId];
if (!snippet) {
console.warn('Snippet not found', snippetId);
return Promise.resolve(false);
}

if (this.autoconsent.config.isMainWorld) {
enableLogs && console.log('inline eval:', expr);
enableLogs && console.log('inline eval:', snippetId, snippet);
let result = false;
try {
result = !!globalThis.eval(expr);
result = !!snippet.call(globalThis);
} catch (e) {
// sometimes CSP blocks eval
enableLogs && console.error('error evaluating rule', expr, e);
// ignore exceptions
enableLogs && console.error('error evaluating rule', snippetId, e);
}
return Promise.resolve(result);
}

enableLogs && console.log('async eval:', expr);
return requestEval(expr).catch((e) => {
enableLogs && console.error('error evaluating rule', expr, e);
const snippetSrc = getFunctionBody(snippet);
enableLogs && console.log('async eval:', snippetId, snippetSrc);
return requestEval(snippetSrc).catch((e) => {
enableLogs && console.error('error evaluating rule', snippetId, e);
return false;
});
}
Expand Down
3 changes: 2 additions & 1 deletion lib/cmps/consentmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default class ConsentManager extends AutoConsentCMPBase {
}

async detectCmp() {
this.apiAvailable = await this.mainWorldEval('window.__cmp && typeof __cmp("getCMPData") === "object"');
this.apiAvailable = await this.mainWorldEval('EVAL_1');
console.log("ConsentManager API available: ", this.apiAvailable);
if (!this.apiAvailable) {
return elementExists("#cmpbox");
} else {
Expand Down
15 changes: 15 additions & 0 deletions lib/eval-snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const snippets = {
EVAL_0() {
return console.log(1);
},
EVAL_1() {
// eslint-disable-next-line
// @ts-ignore
return window.__cmp && typeof __cmp("getCMPData") === "object";
}
}

export function getFunctionBody(snippetFunc) {
const snippetStr = snippetFunc.toString();
return snippetStr.substring(snippetStr.indexOf("{") + 1, snippetStr.lastIndexOf("}"));
}

0 comments on commit c700b5e

Please sign in to comment.