Skip to content

Commit

Permalink
Added optional argument refWindow when creating banners to allow ma…
Browse files Browse the repository at this point in the history
…nipulation with same-site iframe contents
  • Loading branch information
tg666 committed Sep 3, 2024
1 parent ea5f2e8 commit 383e144
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/banner/banner-manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class BannerManager {
this.STATE = State;
}

addExternalBanner(element) {
element = getHtmlElement(element);
addExternalBanner(element, refWindow = window) {
element = getHtmlElement(element, refWindow);

element.setAttribute('data-amp-attached', '');

Expand All @@ -49,12 +49,12 @@ export class BannerManager {
return banner;
}

addManagedBanner(element, position, resources = {}, options = {}) {
addManagedBanner(element, position, resources = {}, options = {}, refWindow = window) {
if (null === this.#bannerRenderer) {
throw new Error(`Unable to add managed banner, renderer is not provided.`);
}

element = getHtmlElement(element);
element = getHtmlElement(element, refWindow);

element.setAttribute('data-amp-attached', '');

Expand All @@ -75,8 +75,8 @@ export class BannerManager {
}

addEmbedBanner(element, iframe, position, options) {
element = getHtmlElement(element);
iframe = getHtmlElement(iframe);
element = getHtmlElement(element, window);
iframe = getHtmlElement(iframe, window);

element.setAttribute('data-amp-attached', '');

Expand Down
12 changes: 6 additions & 6 deletions src/client/standard/client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ export class Client {
return this.#gateway;
}

createBanner(element, position, resources = {}, options = {}, mode = 'managed') {
element = getHtmlElement(element);
createBanner(element, position, resources = {}, options = {}, mode = 'managed', refWindow = window) {
element = getHtmlElement(element, refWindow);

if ('embed' === mode) {
const iframe = this.#createIframe(element, position, resources, options);
Expand All @@ -172,14 +172,14 @@ export class Client {
return banner;
}

return this.#bannerManager.addManagedBanner(element, position, resources, options);
return this.#bannerManager.addManagedBanner(element, position, resources, options, refWindow);
}

closeBanner(bannerId) {
this.#closingManager.closeBanner(bannerId);
}

attachBanners(snippet = document) {
attachBanners(snippet = document, refWindow = window) {
const elements = snippet.querySelectorAll('[data-amp-banner]:not([data-amp-attached])');

for (let element of elements) {
Expand All @@ -194,13 +194,13 @@ export class Client {
let banner;

if ('ampBannerExternal' in element.dataset) {
banner = this.#bannerManager.addExternalBanner(element);
banner = this.#bannerManager.addExternalBanner(element, refWindow);
} else {
const resources = AttributesParser.parseResources(element);
const options = AttributesParser.parseOptions(element);
const mode = element.dataset.ampMode || 'managed';

banner = this.createBanner(element, position, resources, options, mode);
banner = this.createBanner(element, position, resources, options, mode, refWindow);
}

this.#eventBus.dispatch(this.EVENTS.ON_BANNER_ATTACHED, { banner });
Expand Down
12 changes: 9 additions & 3 deletions src/utils/dom-helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export function getHtmlElement(el) {
if (el instanceof HTMLElement) {
/**
*
* @param {string|HTMLElement} el
* @param {Window} refWindow
* @returns {HTMLElement}
*/
export function getHtmlElement(el, refWindow) {
if (el instanceof refWindow.HTMLElement) {
return el;
}

Expand All @@ -15,7 +21,7 @@ export function getHtmlElement(el) {
htmlEl = document.querySelector(el);
}

if (!(htmlEl instanceof HTMLElement)) {
if (!(htmlEl instanceof refWindow.HTMLElement)) {
throw new TypeError('Selector ' + el + ' is invalid.');
}

Expand Down

0 comments on commit 383e144

Please sign in to comment.