|
1 | 1 | /* global chrome, getElement, setNextButton */ |
2 | 2 |
|
3 | | -getElement('player', ($player) => { |
4 | | - // Since Twitch VODs don't hae a proper title at page load on the tab, |
5 | | - // or even after the video load, we get the title ourselves. |
| 3 | +// Wait for player to load. |
| 4 | +getElement('.player', ($player) => { |
| 5 | + // Since Twitch VODs don't have a proper title at page load on the tab, |
| 6 | + // or even after the video loads, we get the title ourselves. |
6 | 7 | getElement('title', ($title) => { |
7 | 8 | chrome.runtime.sendMessage({ title: $title.textContent.trim() }); |
8 | 9 | }); |
9 | 10 |
|
10 | | - const $scroll = document.querySelector('#main_col .tse-scroll-content'); |
| 11 | + // Use the slider on the player to know when the video ends. |
| 12 | + const $slider = document.querySelector('.js-player-slider'); |
| 13 | + let valuemax; |
11 | 14 | const observer = new MutationObserver(() => { |
12 | | - // If the video has ended, not just paused, the player will have its |
13 | | - // `data-ended` attribute be equal to `true`. |
14 | | - if ($player.getAttribute('data-ended') === 'true') { |
| 15 | + if (!valuemax || valuemax <= 0) { |
| 16 | + valuemax = parseFloat($slider.getAttribute('aria-valuemax')); |
| 17 | + } |
| 18 | + let valuenow = parseFloat($slider.getAttribute('aria-valuenow')); |
| 19 | + if (valuemax && valuenow + 1 > valuemax) { |
15 | 20 | observer.disconnect(); |
| 21 | + $player.setAttribute('data-ended', 'true'); |
| 22 | + const $scroll = document.querySelector('main .simplebar-scroll-content'); |
| 23 | + const $tip = |
| 24 | + document.querySelector('.qa-theatre-mode-button span.player-tip'); |
16 | 25 | chrome.runtime.sendMessage({ |
17 | 26 | ended: true, |
18 | | - scrollTop: $scroll.scrollTop, |
| 27 | + scrollTop: $tip.getAttribute('data-tip') === 'Exit Theatre Mode' |
| 28 | + ? 0 : $scroll.scrollTop, |
19 | 29 | }); |
20 | 30 | } |
21 | 31 | }); |
22 | | - observer.observe($player, { attributes: true }); |
| 32 | + observer.observe($slider, { attributes: true }); |
23 | 33 |
|
24 | | - const $playButton = |
25 | | - document.getElementsByClassName('js-control-playpause-button')[0]; |
| 34 | + const $playButton = document.querySelector('button.qa-pause-play-button'); |
26 | 35 | setNextButton($playButton, 'player-button'); |
27 | 36 | }); |
0 commit comments