background.js 1.3 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import delay from 'delay';
  2. async function tryUntilItWorks(func, delayBetweenTries = 1, maxTries = 10) {
  3. for (let i = 0; i < maxTries; i++) {
  4. try {
  5. await func();
  6. return;
  7. } catch (err) {
  8. await delay(delayBetweenTries);
  9. continue;
  10. }
  11. }
  12. }
  13. async function onHeadersReceived({ responseHeaders, url, tabId }) {
  14. const isAudio = responseHeaders.some(header =>
  15. header.name.toLowerCase() === 'content-type' && header.value.toLowerCase().startsWith('audio/')
  16. );
  17. if (isAudio) {
  18. // Avoid any short glitch of sound (observed in Firefox) before our injected script executes.
  19. await browser.tabs.update(tabId, { muted: true });
  20. // Too quickly executing the script may fail (bug in Firefox), so we retry if necessary.
  21. await tryUntilItWorks(() => browser.tabs.executeScript(tabId, {
  22. runAt: 'document_end',
  23. file: '/scripts/contentscript.js',
  24. }));
  25. // Unmute again.
  26. await browser.tabs.update(tabId, { muted: false });
  27. await tryUntilItWorks(() => browser.tabs.insertCSS(tabId, {
  28. runAt: 'document_end',
  29. file: '/assets/waveform-playlist.css',
  30. }));
  31. await tryUntilItWorks(() => browser.tabs.insertCSS(tabId, {
  32. runAt: 'document_end',
  33. file: '/assets/main.css',
  34. }));
  35. }
  36. }
  37. browser.webRequest.onHeadersReceived.addListener(
  38. onHeadersReceived,
  39. {
  40. urls: ['<all_urls>'],
  41. types: ['main_frame'],
  42. },
  43. ['responseHeaders'],
  44. );