Skip to content

Instantly share code, notes, and snippets.

@ericlaw1979
Created October 17, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericlaw1979/2753d1c773c09ab1468e73f511e8fe8e to your computer and use it in GitHub Desktop.
Save ericlaw1979/2753d1c773c09ab1468e73f511e8fe8e to your computer and use it in GitHub Desktop.
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