Instantly share code, notes, and snippets.

Embed
What would you like to do?
Browser Extension code to intercept top-level navigations directly to audio/video files and instead navigate to a playback page that allows the user to play the response inside the browser itself.
// Note, you need to update the "permissions" section of your manifest.json
// to contain ["webRequest", "webRequestBlocking"]
// Watch for top-frame navigations directly to media files. If found, instead navigate to a player page.
browser.webRequest.onHeadersReceived.addListener(
function(details) {
// We don't care about redirects or error responses.
if (details.statusCode !== 200) return;
// Skip navigations that aren't to Audio/Video
let sCT = extractContentType(details.responseHeaders).toLowerCase();
if (!(sCT.startsWith("audio/") || sCT.startsWith("video/") || (sCT.startsWith("application/ogg")))) return;
// Redirect to a player page.
return {
redirectUrl: ("https://bayden.com/test/play.aspx?ct=" + encodeURIComponent(sCT) + "&u=" + encodeURIComponent(details.url))
};
},
{urls:["<all_urls>"], types:["main_frame"] },
["blocking","responseHeaders"]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment