|
10 | 10 | } catch (error) { |
11 | 11 | SpotiShush.debug(error.message) |
12 | 12 | } |
13 | | - // Deezer |
14 | | - if (window.location.hostname === 'www.deezer.com') { |
15 | | - window.addEventListener('sasVideoStart', deezerRunBeforeAds) |
16 | | - window.addEventListener('sasVideoEnd', deezerRunAfterAds) |
17 | | - window.addEventListener('adError', deezerRunAfterAds) |
| 13 | + switch (window.location.hostname) { |
| 14 | + case 'www.deezer.com': { |
| 15 | + window.addEventListener('sasVideoStart', deezerRunBeforeAds) |
| 16 | + window.addEventListener('sasVideoEnd', deezerRunAfterAds) |
| 17 | + window.addEventListener('adError', deezerRunAfterAds) |
18 | 18 |
|
19 | | - // Well, that was easy... |
20 | | - SpotiShush.log('Monitoring ads now!') |
| 19 | + // Well, that was easy... |
| 20 | + SpotiShush.log('Monitoring ads now!') |
| 21 | + break |
| 22 | + } |
| 23 | + case 'open.spotify.com': { |
| 24 | + SpotiShush.log('Waiting for player controls to be ready...') |
21 | 25 |
|
22 | | - return |
23 | | - } |
24 | | - // Spotify |
25 | | - SpotiShush.log('Waiting for player controls to be ready...') |
| 26 | + // Spotify's "Now Playing" bar |
| 27 | + const nowPlaying = await lazySelector('div.Root__now-playing-bar') |
26 | 28 |
|
27 | | - const nowPlaying = await spotifyControlsReady() |
| 29 | + try { |
| 30 | + spotifySetupAdsObserver(nowPlaying) |
| 31 | + } catch (error) { |
| 32 | + SpotiShush.log('Unable to set up ads monitor:', error.message) |
| 33 | + return |
| 34 | + } |
| 35 | + SpotiShush.log('Success. Monitoring ads now!') |
| 36 | + break |
| 37 | + } |
| 38 | + case 'listen.tidal.com': { |
| 39 | + SpotiShush.log('Waiting for repeat button to be ready...') |
28 | 40 |
|
29 | | - try { |
30 | | - setupAdsObserver(nowPlaying) |
31 | | - } catch (error) { |
32 | | - SpotiShush.log('Unable to set up ads monitor:', error.message) |
33 | | - return |
| 41 | + // TIDAL's repeat button |
| 42 | + const repeatButton = await lazySelector('div#playbackControlBar > button[data-test=repeat]') |
| 43 | + |
| 44 | + tidalSetupAdsObserver(repeatButton) |
| 45 | + |
| 46 | + SpotiShush.log('Success. Monitoring ads now!') |
| 47 | + // On TIDAL, ads are persistent through a page reload, so here we manually |
| 48 | + // trigger our ads observer function to determine if there's an ad in our queue. |
| 49 | + repeatButton.type = repeatButton.getAttribute('type') |
| 50 | + break |
| 51 | + } |
| 52 | + default: |
| 53 | + break |
34 | 54 | } |
35 | | - SpotiShush.log('Success. Monitoring ads now!') |
36 | 55 |
|
37 | | - function spotifyControlsReady (checkInterval) { |
| 56 | + function lazySelector (selector, checkInterval) { |
38 | 57 | return new Promise((resolve) => { |
39 | 58 | const id = setInterval(() => { |
40 | | - const nowPlaying = document.querySelector('div.Root__now-playing-bar') |
| 59 | + const element = document.querySelector(selector) |
41 | 60 |
|
42 | | - if (nowPlaying !== null) { |
| 61 | + if (element !== null) { |
43 | 62 | clearInterval(id) |
44 | | - resolve(nowPlaying) |
| 63 | + resolve(element) |
45 | 64 | } |
46 | 65 | }, checkInterval || 500) |
47 | 66 | }) |
48 | 67 | } |
49 | 68 |
|
| 69 | + function tidalSetupAdsObserver (repeatButton) { |
| 70 | + const mo = new MutationObserver(async (mutations) => { |
| 71 | + SpotiShush.debug('mutations:', mutations) |
| 72 | + |
| 73 | + if (repeatButton.disabled) { |
| 74 | + // TIDAL disables the repeat button when an ad is playing. |
| 75 | + SpotiShush.log('Ad detected!') |
| 76 | + try { |
| 77 | + await browser.runtime.sendMessage({ action: 'mute' }) |
| 78 | + } catch (error) { |
| 79 | + SpotiShush.debug(error.message) |
| 80 | + return |
| 81 | + } |
| 82 | + SpotiShush.log('Tab muted.') |
| 83 | + } else { |
| 84 | + SpotiShush.log('Not an ad!') |
| 85 | + try { |
| 86 | + await browser.runtime.sendMessage({ action: 'unmute' }) |
| 87 | + } catch (error) { |
| 88 | + SpotiShush.debug(error.message) |
| 89 | + return |
| 90 | + } |
| 91 | + SpotiShush.log('Tab unmuted.') |
| 92 | + } |
| 93 | + }) |
| 94 | + mo.observe(repeatButton, { |
| 95 | + attributes: true |
| 96 | + }) |
| 97 | + } |
| 98 | + |
50 | 99 | // Detect ads by observing mutations in the `data-testid` HTML attribute of Spotify's player controls. |
51 | | - function setupAdsObserver (nowPlaying) { |
| 100 | + function spotifySetupAdsObserver (nowPlaying) { |
52 | 101 | const footerObj = nowPlaying.firstElementChild |
53 | 102 |
|
54 | 103 | if (footerObj === null) { |
|
0 commit comments