Skip to content

Commit

Permalink
Use webRequestBlocking if built for firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
psidex committed Jul 14, 2023
1 parent 81e1a4f commit 6940822
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
6 changes: 6 additions & 0 deletions bob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,17 @@ const tasks = new Listr([
title: 'Merge manifests',
task: (ctx) => {
ctx.manifest = browser === 'chrome' ? manifestChrome : manifestFirefox;

// Object merge isn't deep, so manually merge permission stuff.
const mergedPermissions = [...manifestShared.permissions, ...ctx.manifest.permissions];

// Overwrite shared settings with browser based values.
ctx.manifest = {
...manifestShared,
...ctx.manifest,
};

ctx.manifest.permissions = mergedPermissions;
},
},
{
Expand Down
1 change: 1 addition & 0 deletions manifest.chrome.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"permissions": [],
"background": {
"service_worker": "src/background/main.js"
}
Expand Down
3 changes: 3 additions & 0 deletions manifest.firefox.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"permissions": [
"webRequestBlocking"
],
"background": {
"scripts": [
"src/background/main.js"
Expand Down
35 changes: 25 additions & 10 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import { setBangsLookup } from './lookup';
import { setIgnoredDomains } from './ignoreddomains';
import defaultSettings from '../lib/settings.default.json';

function devLog(message: string): void {
if (dev) {
// eslint-disable-next-line no-console
console.info(message);
}
}

function updateGlobals(settings: Settings): void {
setBangsLookup(settings);
setIgnoredDomains(settings.options.ignoredDomains);
Expand Down Expand Up @@ -48,10 +55,7 @@ async function setupSettings(): Promise<void> {
}

function main(): void {
if (dev) {
// eslint-disable-next-line no-console
console.info(`Dev: ${dev}, Browser: ${currentBrowser}, Version: ${version}, Hash: ${hash}`);
}
devLog(`Dev: ${dev}, Browser: ${currentBrowser}, Version: ${version}, Hash: ${hash}`);

// Because service workers need to set their event listeners immediately, we can't await this.
// FIXME: There may be a better way to do this, but for now we just hope it runs quickly!
Expand All @@ -66,14 +70,25 @@ function main(): void {
}
});

// The requestBody opt is required for handling POST situations.
const extraInfoSpec: browser.WebRequest.OnBeforeRequestOptions[] = ['requestBody'];

// Wrap processRequest because the types don't like an async non-blocking handler.
let webRequestHandler = (r: browser.WebRequest.OnBeforeRequestDetailsType) => {
processRequest(r);
};

if (currentBrowser === 'firefox') {
devLog('Enabling blocking webRequest listener');
// Add blocking spec and unwrap processRequest as it may return a blocking response.
extraInfoSpec.push('blocking');
webRequestHandler = processRequest;
}

browser.webRequest.onBeforeRequest.addListener(
(r) => {
// Type defs don't like an async non-blocking handler.
processRequest(r);
},
webRequestHandler,
{ urls: hostPermissions },
// The requestBody opt is required for handling POST situations.
['requestBody'],
extraInfoSpec,
);
}

Expand Down
20 changes: 16 additions & 4 deletions src/background/requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import browser, { WebRequest } from 'webextension-polyfill';

import { currentBrowser } from '../lib/esbuilddefinitions';
import { getBangsLookup } from './lookup';
import { getIgnoredDomains } from './ignoreddomains';

Expand Down Expand Up @@ -122,7 +123,7 @@ export async function getRedirects(
*/
export async function processRequest(
r: WebRequest.OnBeforeRequestDetailsType,
): Promise<void> {
): Promise<void | WebRequest.BlockingResponse> {
if (r.type !== 'main_frame') {
return Promise.resolve();
}
Expand All @@ -143,8 +144,19 @@ export async function processRequest(
browser.tabs.create({ url: redirections[i] });
}

// Finally send the current tab to the first in the array.
browser.tabs.update(r.tabId, { url: redirections[0] });
if (r.method !== 'GET' || currentBrowser !== 'firefox') {
// If we're handling a POST request, we need to tell the tab where to go, as
// redirecting the POST would not change the tabs location.
// OR we're not using firefox, in which case we have to use this method anyway.
browser.tabs.update(r.tabId, { url: redirections[0] });

return Promise.resolve();
if (currentBrowser === 'firefox') {
return Promise.resolve({ cancel: true });
}

return Promise.resolve();
}

// This is a GET request, we are on firefox, so send a blocking response.
return Promise.resolve({ redirectUrl: redirections[0] });
}

0 comments on commit 6940822

Please sign in to comment.