Browse Source

Make work more reliably in Firefox

tags/v0.1.0
Gerben 5 years ago
parent
commit
bee854dd00
3 changed files with 37 additions and 8 deletions
  1. +31
    -8
      app/scripts/background.js
  2. +5
    -0
      package-lock.json
  3. +1
    -0
      package.json

+ 31
- 8
app/scripts/background.js View File

@@ -1,21 +1,44 @@
import delay from 'delay';

async function tryUntilItWorks(func, delayBetweenTries = 1, maxTries = 10) {
for (let i = 0; i < maxTries; i++) {
try {
await func();
return;
} catch (err) {
await delay(delayBetweenTries);
continue;
}
}
}

async function onHeadersReceived({ responseHeaders, url, tabId }) {
const isAudio = responseHeaders.some(header =>
header.name === 'Content-Type' && header.value.startsWith('audio/')
);

if (isAudio) {
browser.tabs.executeScript(tabId, {
runAt: 'document_end',
file: '/scripts/contentscript.js',
});
browser.tabs.insertCSS(tabId, {
// Avoid any short glitch of sound (observed in Firefox) before our injected script executes.
await browser.tabs.update(tabId, { muted: true });

// Too quickly executing the script may fail (bug in Firefox), so we retry if necessary.
await tryUntilItWorks(() => browser.tabs.executeScript(tabId, {
runAt: 'document_end',
file: '/scripts/contentscript.js',
}));

// Unmute again.
await browser.tabs.update(tabId, { muted: false });

await tryUntilItWorks(() => browser.tabs.insertCSS(tabId, {
runAt: 'document_end',
file: '/assets/main.css',
});
browser.tabs.insertCSS(tabId, {
}));

await tryUntilItWorks(() => browser.tabs.insertCSS(tabId, {
runAt: 'document_end',
file: '/assets/waveform-playlist.css',
});
}));
}
}



+ 5
- 0
package-lock.json View File

@@ -1957,6 +1957,11 @@
}
}
},
"delay": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/delay/-/delay-4.3.0.tgz",
"integrity": "sha512-Lwaf3zVFDMBop1yDuFZ19F9WyGcZcGacsbdlZtWjQmM50tOcMntm1njF/Nb/Vjij3KaSvCF+sEYGKrrjObu2NA=="
},
"des.js": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/des.js/-/des.js-1.0.0.tgz",


+ 1
- 0
package.json View File

@@ -15,6 +15,7 @@
"webextension-toolbox": "latest"
},
"dependencies": {
"delay": "^4.3.0",
"waveform-playlist": "^3.0.4"
}
}

Loading…
Cancel
Save