|
| 1 | +// Copyright (c) 2020 The Brave Authors. All rights reserved. |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | +// you can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +import injectToDocument from '../common/contentScript/inject-to-document' |
| 7 | +import BATAd from './bat-ad' |
| 8 | +import fetchAdCreatives from './creativeFetch/same-context' |
| 9 | +import { AdSize, stringToAdSize } from './' |
| 10 | +import runOnPageLoaded from '../common/pageLifecycle/run-on-loaded' |
| 11 | + |
| 12 | +console.log("content script") |
| 13 | +type SlotDefinition = { |
| 14 | + adUnitPath: string, |
| 15 | + size: googletag.GeneralSize |
| 16 | + elementId?: string |
| 17 | +} |
| 18 | + |
| 19 | +type PageSlotsEventData = { pageSlots: SlotDefinition[] } |
| 20 | + |
| 21 | +const eventName = `brave-gpt-slots-ready` |
| 22 | + |
| 23 | +function googleSizeToAdSize (sizeParam: googletag.GeneralSize): AdSize[] | null { |
| 24 | + if (typeof sizeParam === 'string') { |
| 25 | + const size = stringToAdSize(sizeParam) |
| 26 | + if (!size) { |
| 27 | + return [] |
| 28 | + } |
| 29 | + return [ size ] |
| 30 | + } else if (Array.isArray(sizeParam)) { |
| 31 | + if (sizeParam.every((i: any) => Array.isArray(i))) { |
| 32 | + return sizeParam as AdSize[] |
| 33 | + } |
| 34 | + if (sizeParam.length === 2 && sizeParam.every((i: any) => typeof i === 'number')) { |
| 35 | + return [sizeParam as AdSize] |
| 36 | + } |
| 37 | + if (sizeParam.every((i: any) => typeof i === 'string')) { |
| 38 | + const stringSizes: string[] = sizeParam as string[] |
| 39 | + const adSizes: AdSize[] = stringSizes |
| 40 | + .map(stringToAdSize) |
| 41 | + .filter((size: AdSize | null) => size !== null) as AdSize[] |
| 42 | + return adSizes |
| 43 | + } |
| 44 | + } |
| 45 | + // TODO(petemill): There seem to be more types that googletag.GeneralSize can be that |
| 46 | + // we are not handling here. Implement conversions as we encounter those types. |
| 47 | + return null |
| 48 | +} |
| 49 | + |
| 50 | +function elementIdFromAdUnitPath (adUnitPath: string): string { |
| 51 | + const segments = adUnitPath.split('/') |
| 52 | + return segments[segments.length - 1] |
| 53 | +} |
| 54 | + |
| 55 | +// Content script receives information about the ads from the Page |
| 56 | +window.addEventListener(eventName, function (e: CustomEvent<PageSlotsEventData>) { |
| 57 | + console.log('Content script received page slots', e.detail.pageSlots) |
| 58 | + const { detail: { pageSlots }} = e |
| 59 | + runOnPageLoaded(function () { |
| 60 | + for (const slot of pageSlots) { |
| 61 | + const sizes = googleSizeToAdSize(slot.size) |
| 62 | + if (!sizes) { |
| 63 | + console.error('Brave Publisher Ads: could not find sizes for slot', slot) |
| 64 | + continue |
| 65 | + } |
| 66 | + const selector = slot.elementId || elementIdFromAdUnitPath(slot.adUnitPath) |
| 67 | + const element = document.querySelector<HTMLElement>(`#${selector}`) |
| 68 | + if (!element) { |
| 69 | + console.error('Brave Publisher Ads: could not find element for slot', slot) |
| 70 | + continue |
| 71 | + } |
| 72 | + new BATAd(element, fetchAdCreatives).sizes = sizes |
| 73 | + } |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +// Page script sends information about the ads to the Content Script |
| 78 | +injectToDocument(function ($eventName: string) { |
| 79 | + console.log('inject') |
| 80 | + function observeGoogleTag(googletag: googletag.Googletag) { |
| 81 | + // @ts-ignore |
| 82 | + googletag.__isObserved = true |
| 83 | + console.log('observe') |
| 84 | + // Page usually defines googletag before this script runs, |
| 85 | + // then the stub adds the api functions, usually after this script runs. |
| 86 | + // If we simple define the functions then they will be |
| 87 | + // overwritten. |
| 88 | + // Instead, Proxy them. |
| 89 | + let actualDefineSlots = googletag.defineSlot |
| 90 | + let actualEnableServices = googletag.enableServices |
| 91 | + const pageSlots: SlotDefinition[] = [] |
| 92 | + |
| 93 | + googletag.defineSlot = function (adUnitPath, size, elementId) { |
| 94 | + pageSlots.push({ adUnitPath, size, elementId }) |
| 95 | + return actualDefineSlots(adUnitPath, size) |
| 96 | + } |
| 97 | + |
| 98 | + googletag.enableServices = function () { |
| 99 | + window.dispatchEvent(new CustomEvent<PageSlotsEventData>($eventName, { |
| 100 | + detail: { |
| 101 | + pageSlots |
| 102 | + }, |
| 103 | + bubbles: true |
| 104 | + })) |
| 105 | + actualEnableServices() |
| 106 | + } |
| 107 | + |
| 108 | + return new Proxy(googletag, { |
| 109 | + set (googletag, propName, value) { |
| 110 | + if (propName === 'defineSlot') { |
| 111 | + actualDefineSlots = value |
| 112 | + } else if (propName === 'enableServices') { |
| 113 | + actualEnableServices = value |
| 114 | + } else { |
| 115 | + googletag[propName] = value |
| 116 | + } |
| 117 | + return true |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + if (window['googletag']) { |
| 123 | + googletag = observeGoogleTag(googletag) |
| 124 | + } else { |
| 125 | + console.log('googletag was not defined') |
| 126 | + // Maybe the Page didn't define it, so wait in case it gets defined. |
| 127 | + let _value: googletag.Googletag |
| 128 | + Object.defineProperty(window, 'googletag', { |
| 129 | + configurable: true, |
| 130 | + set: function (value) { |
| 131 | + console.log("googletag = set") |
| 132 | + _value = (value.__isObserved ? value : observeGoogleTag(value)) |
| 133 | + }, |
| 134 | + get: function () { |
| 135 | + return _value |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +}, eventName) |
0 commit comments