Skip to content

highimpact.js / advantage/messaging/publisher-side / AdvantageAdSlotResponder

Class: AdvantageAdSlotResponder ​

advantage/messaging/publisher-side.AdvantageAdSlotResponder

AdvantageAdSlotResponder can be used by website owners/publishers if they already have their own custom implementations of high impact ad formats or if they do not want to use the AdvantageWrapper component for some reason. It takes care of listening for messages from Advantage ads and handling them. It will handle the creation of a new session, format requests, and format confirmations.

Remarks

This class is internally used by the AdvantageWrapper component to handle messages from Advantage ads. It can also be used by website owners/publishers to handle messages from ads if they have their own custom implementations of high-impact ad formats.

Example

To handle messages from Advantage ads in a custom implementation, you can create an instance of the AdvantageAdSlotResponder class and pass in the configuration object.

typescript
new AdvantageAdSlotResponder({
     adSlotElement: document.querySelector("#the-ad-slot-element")!,
     formatRequestHandler: (format, parentElement) => {
          return new Promise((resolve, reject) => {
             // handle the format request here, e.g. by transforming the parent element into the requested format
             // resolve the promise if the format transformation was succesful or reject it if it failed
         });
});

Constructors ​

constructor ​

• new AdvantageAdSlotResponder(config): AdvantageAdSlotResponder

Constructs a new instance of the AdvantageAdSlotResponder, initializing it with the provided configuration.

Parameters ​

NameTypeDescription
configObjectThe configuration object for the class instance.
config.adSlotElementHTMLElementThe HTML element that is/contains the ad slot where Advantage ads will loaded/displayed.
config.formatRequestHandler?(format: string, parentElement: HTMLElement) => Promise<void>An optional function that handles format requests. It takes a format string and a parent element as arguments. This function can be used to customize the handling of different ad formats.
config.messageValidator?(parentElement: HTMLElement | IAdvantageWrapper, message: MessageEvent<any>) => booleanAn optional function that validates incoming messages. It takes a parent element (which can be an HTMLElement or an IAdvantageWrapper) and the message event as arguments. It should return a boolean indicating whether the message is valid.

Returns ​

AdvantageAdSlotResponder

Defined in ​

src/advantage/messaging/publisher-side.ts:92

Properties ​

#element ​

• Private #element: HTMLElement | IAdvantageWrapper

Defined in ​

src/advantage/messaging/publisher-side.ts:50


#formatRequestHandler ​

• Private #formatRequestHandler: undefined | (format: string, parentElement: HTMLElement) => Promise<void> = undefined

Defined in ​

src/advantage/messaging/publisher-side.ts:80


#isWrapper ​

• Private #isWrapper: boolean

Defined in ​

src/advantage/messaging/publisher-side.ts:78


#messagePort ​

• Private #messagePort: null | MessagePort = null

Defined in ​

src/advantage/messaging/publisher-side.ts:79


#messageValidator ​

• Private #messageValidator: undefined | (adSlotElement: HTMLElement | IAdvantageWrapper, message: MessageEvent<any>) => boolean = undefined

Defined in ​

src/advantage/messaging/publisher-side.ts:51


• ad: null | AdvantageAd = null

Defined in ​

src/advantage/messaging/publisher-side.ts:83

Methods ​

#childAdIsAlreadyRegistered ​

▸ #childAdIsAlreadyRegistered(source): null | boolean

Checks if the message is from an ad that is already registered

Parameters ​

NameType
sourcenull | MessageEventSource

Returns ​

null | boolean

Defined in ​

src/advantage/messaging/publisher-side.ts:202


#defaultMessageValidator ​

▸ #defaultMessageValidator(_adSlotElement, event): boolean

Parameters ​

NameType
_adSlotElementHTMLElement | IAdvantageWrapper
eventMessageEvent<AdvantageMessage>

Returns ​

boolean

Defined in ​

src/advantage/messaging/publisher-side.ts:57


#handleMessage ​

▸ #handleMessage(event): Promise<void>

This method handles incoming messages from Advantage ads and processes them accordingly.

Parameters ​

NameType
eventMessageEvent<AdvantageMessage>

Returns ​

Promise<void>

Defined in ​

src/advantage/messaging/publisher-side.ts:124


#isAdvantageWrapper ​

▸ #isAdvantageWrapper(element): element is IAdvantageWrapper

Checks if the provided element is an instance of IAdvantageWrapper.

Parameters ​

NameType
elementHTMLElement | IAdvantageWrapper

Returns ​

element is IAdvantageWrapper

Defined in ​

src/advantage/messaging/publisher-side.ts:311


#listenForMessages ​

▸ #listenForMessages(event): void

This method listens for incoming messages from Advantage ads and processes them accordingly.

Parameters ​

NameType
eventMessageEvent<any>

Returns ​

void

Defined in ​

src/advantage/messaging/publisher-side.ts:213


sendMessage ​

▸ sendMessage(message): boolean

Sends a message to the creative through the established MessagePort channel. This method ensures messages are delivered even when the creative is nested in multiple iframes.

Parameters ​

NameTypeDescription
messagePartial<AdvantageMessage>The message to send to the creative

Returns ​

boolean

true if the message was sent successfully, false if no message channel is established

Example

typescript
// Send a scroll progress update to the creative
responder.sendMessage({
    type: 'advantage',
    action: 'SCROLL_PROGRESS',
    progress: 0.5
});

Defined in ​

src/advantage/messaging/publisher-side.ts:340