Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Created August 6, 2021 05:47
Show Gist options
  • Save JamesTheAwesomeDude/d941765e5d6bf12caa4ad9620e380fc3 to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/d941765e5d6bf12caa4ad9620e380fc3 to your computer and use it in GitHub Desktop.
DuckDuckGo .Onion redirect
const DDG_ONION = browser.runtime.getManifest().permissions.find(p =>
p.match(/duckduckgo.*\.onion/));
const DDG_CLEARNET = browser.runtime.getManifest().permissions.find(p =>
p.match(/duckduckgo(?!.*\.onion)/))
const DDG_ONION_HOST = DDG_ONION.match(/^\*:\/\/(.+)\/\*$/)[1];
function injectOnionHeader(details) {
let u = new URL(details.url);
let h = details.responseHeaders;
u.host = DDG_ONION_HOST;
h.push({name: 'Onion-Location', value: u.toString()});
return {responseHeaders: h};
}
browser.webRequest.onHeadersReceived.addListener(
injectOnionHeader,
{urls: [DDG_CLEARNET]},
["blocking", "responseHeaders"]
);
function redirectToOnion(details) {
let u = new URL(details.url);
u.host = DDG_ONION_HOST;
return {redirectUrl: u.toString()};
}
function enableOnionRedirect(details) {
if( details.statusCode != 200 ) return;
browser.webRequest.onBeforeRequest.addListener(
redirectToOnion,
{urls: [DDG_CLEARNET]},
["blocking"]
);
console.info('browser.webRequest.onBeforeRequest.removeListener(redirectToOnion);');
browser.webRequest.onCompleted.removeListener(enableOnionRedirect);
}
browser.webRequest.onCompleted.addListener(
enableOnionRedirect,
{urls: [DDG_ONION]}
);
{
"manifest_version": 2,
"name": "DuckDuckGo .Onion redirect",
"version": "0",
"permissions": [
"webRequest","webRequestBlocking",
"*://duckduckgo.com/*",
"*://duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion/*"
],
"background": {
"scripts": ["main.js"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment