Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "frontend-packages/cookie-consent-web-client/src/api/wrapFn"

Index

Type aliases

Functions

Type aliases

Apis

Apis: { consent: (...args: OmitFirst<Parameters<typeof consent>>) => ReturnType<typeof consent>; consentAll: (...args: OmitFirst<Parameters<typeof consentAll>>) => ReturnType<typeof consentAll>; consentSync: (...args: OmitFirst<Parameters<typeof consentSync>>) => ReturnType<typeof consentSync>; unblock: (...args: OmitFirst<Parameters<typeof unblock>>) => ReturnType<typeof unblock> }

Type declaration

Fn

Fn: (...args: any[]) => any

Type declaration

    • (...args: any[]): any
    • Parameters

      • Rest ...args: any[]

      Returns any

FnOverwriteObject

FnOverwriteObject: { key: string; object: any; overwritten?: boolean }

Type declaration

  • key: string
  • object: any

    If you pass in a function, it automatically gets retried to overwrite in interactive and complete document.readyState.

  • Optional overwritten?: boolean

    Only for internal usage.

Functions

wrapFn

  • wrapFn(apis: Apis, blocker: Parameters<typeof unblock>[0], manager: CookieConsentManager, fn: Fn | FnOverwriteObject | Array<FnOverwriteObject>, checkExecution: ((args: { args: any[]; blocker: Parameters<typeof unblock>[0]; callOriginal: () => any; manager: CookieConsentManager; objectResolved?: any; original: Fn; that: any }) => boolean | Promise<void>) | ["consent", Object] | ["consentAll", Object] | ["consentSync", Object] | ["unblock", Object] | "functionBody" | Promise<any>, settings?: { failedSyncReturnValue?: boolean; skipRetry?: boolean }): Fn | Fn[]
  • Wrap any function into a consent-awaiting function. This allows you to e.g. overwrite every function a WordPress theme or plugin is exposing to window object.

    Learn usage by example

    A plugin exposes the following function which loads a Google Map to window:

    <script consent-skip-blocker="1">
      window.mynested = {
        another: {
          loadGoogleMaps: (when) => {
            console.log("I am loading https://google.com/maps");
          }
        }
      }
    
      jQuery(() => window.mynested.another.loadGoogleMaps("now"));
    </script>
    

    We can now override this method with the help of idx and delay of the console.log until consent for Google Maps is given:

    wrapFn(
        {
            object: idx(window, (window) => window.mynested.another),
            key: "loadGoogleMaps"
        },
        ["unblock", "https://google.com/maps"]
    );
    

    The example above can also be used in the following way to check for blocker rules for the given function body:

    wrapFn(
        {
            object: idx(window, (window) => window.mynested.another),
            key: "loadGoogleMaps"
        },
        "functionBody"
    );
    

    The above examples determine the consent by a defined Content Blocker. But you can also wait for consent by a defined service with the help of the consent API to check for a technical definition (HTTP Cookie, LocalStorage, ...):

    wrapFn(
        {
            object: idx(window, (window) => window.mynested.another),
            key: "loadGoogleMaps"
        },
        ["consent", "http", "__SECURE", "*"]
    );
    

    And for the advanced usages, you can simply return your own boolean or Promise by passing your custom checker:

    wrapFn(
        {
            object: idx(window, (window) => window.mynested.another),
            key: "loadGoogleMaps"
        },
        () => {
            return new Promise((resolve) => {
                // [...]
            });
        }
    );
    

    If you are invoking wrapFn at the very beginning of your HTML and you do not know, when the function which should be overwritten is available, you can pass a function to object so it gets retried to overwrite on interactive and complete state:

    wrapFn(
        {
            object: () => idx(window, (window) => window.mynested.another),
            key: "loadGoogleMaps"
        },
        () => {
            return new Promise((resolve) => {
                // [...]
            });
        }
    );
    

    Parameters

    • apis: Apis
    • blocker: Parameters<typeof unblock>[0]
    • manager: CookieConsentManager
    • fn: Fn | FnOverwriteObject | Array<FnOverwriteObject>

      The function(s) we want to override. If you pass a function you need to implement the overriding for the original function yourself. Otherwise you can pass an object defining the object itself and the appropriate key which holds the function so it gets overwritten automatically.

    • checkExecution: ((args: { args: any[]; blocker: Parameters<typeof unblock>[0]; callOriginal: () => any; manager: CookieConsentManager; objectResolved?: any; original: Fn; that: any }) => boolean | Promise<void>) | ["consent", Object] | ["consentAll", Object] | ["consentSync", Object] | ["unblock", Object] | "functionBody" | Promise<any>

      Allows you to define a function which needs to return a boolean which allows synced execution. If the return is a Promise it will return the Promise immediately, when the Promise gets resolved the original function gets invoked.

    • Default value settings: { failedSyncReturnValue?: boolean; skipRetry?: boolean } = {}
      • Optional failedSyncReturnValue?: boolean
      • Optional skipRetry?: boolean

    Returns Fn | Fn[]

    Function which calls original function when consent is given (depending on checkExecution) or undefined if an undefined function was passed as argument.

Generated using TypeDoc