-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdSkipButton.ts
145 lines (130 loc) · 5.19 KB
/
AdSkipButton.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as shadyCss from '@webcomponents/shadycss';
import { Button, buttonTemplate } from '../Button';
import adSkipButtonCss from './AdSkipButton.css';
import skipNextIcon from '../../icons/skip-next.svg';
import { StateReceiverMixin } from '../StateReceiverMixin';
import { Attribute } from '../../util/Attribute';
import type { Ads, ChromelessPlayer } from 'theoplayer/chromeless';
import { arrayFind, setTextContent } from '../../util/CommonUtils';
import { isLinearAd } from '../../util/AdUtils';
import { createTemplate } from '../../util/TemplateUtils';
const template = createTemplate(
'theoplayer-ad-skip-button',
buttonTemplate(
`<span part="countdown">Skip in 0 seconds</span>` +
`<span part="skip">` +
`<span part="skip-text"><slot name="skip-text">Skip Ad</slot></span> ` +
`<span part="skip-icon"><slot name="skip-icon">${skipNextIcon}</slot></span>` +
`</span>`,
adSkipButtonCss
)
);
const AD_EVENTS = ['adbegin', 'adend', 'adloaded', 'updatead', 'adskip'] as const;
/**
* `<theoplayer-ad-skip-button>` - A button that skips the current advertisement (if skippable).
* If the ad cannot be skipped yet, it shows the remaining time until it can be skipped.
*
* @group Components
*/
export class AdSkipButton extends StateReceiverMixin(Button, ['player']) {
private readonly _countdownEl: HTMLElement;
private readonly _skipEl: HTMLElement;
private _player: ChromelessPlayer | undefined;
private _ads: Ads | undefined;
static get observedAttributes() {
return [...Button.observedAttributes];
}
constructor() {
super({ template: template() });
this._countdownEl = this.shadowRoot!.querySelector('[part="countdown"]')!;
this._skipEl = this.shadowRoot!.querySelector('[part="skip"]')!;
this._upgradeProperty('player');
}
override connectedCallback(): void {
super.connectedCallback();
this._update();
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
this._player?.removeEventListener('timeupdate', this._update);
this._ads?.removeEventListener(AD_EVENTS, this._onAdChange);
this._player = player;
this._ads = player?.ads;
this._onAdChange();
this._ads?.addEventListener(AD_EVENTS, this._onAdChange);
}
protected override handleClick(): void {
this._player?.ads?.skip();
}
attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
super.attributeChangedCallback(attrName, oldValue, newValue);
if (AdSkipButton.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
private readonly _onAdChange = () => {
if (this._player?.ads?.playing) {
this._player.removeEventListener('timeupdate', this._update);
this._player.addEventListener('timeupdate', this._update);
} else {
this._player?.removeEventListener('timeupdate', this._update);
}
this._update();
};
private readonly _update = (): void => {
const ads = this._player?.ads;
if (!ads || !ads.playing) {
// No ads playing.
this.style.display = 'none';
return;
}
const linearAd = arrayFind(ads.currentAds ?? [], isLinearAd);
if (linearAd === undefined) {
// No linear ad.
this.style.display = 'none';
return;
}
if (linearAd.integration === 'google-ima') {
// Google IMA always shows their own skip button.
this.style.display = 'none';
return;
}
const skipOffset = linearAd.skipOffset;
if (skipOffset === undefined || skipOffset < 0 || (linearAd.duration !== undefined && skipOffset >= linearAd.duration)) {
// Linear ad is not skippable.
this.style.display = 'none';
return;
}
const currentTime = this._player!.currentTime;
if (currentTime < skipOffset) {
// Show countdown.
const timeToSkip = Math.ceil(skipOffset - currentTime);
setTextContent(this._countdownEl, `Skip in ${timeToSkip}s`);
this._countdownEl.style.visibility = 'visible';
this._skipEl.style.visibility = 'hidden';
this._skipEl.style.pointerEvents = 'none';
this.style.display = '';
this.setAttribute(Attribute.DISABLED, '');
this.setAttribute(Attribute.ARIA_LIVE, 'off');
} else {
// Show skip button.
this._countdownEl.style.visibility = 'hidden';
this._skipEl.style.visibility = 'visible';
this._skipEl.style.pointerEvents = '';
this.style.display = '';
this.removeAttribute(Attribute.DISABLED);
this.setAttribute(Attribute.ARIA_LIVE, 'polite');
}
};
}
customElements.define('theoplayer-ad-skip-button', AdSkipButton);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-ad-skip-button': AdSkipButton;
}
}