Skip to content

Commit

Permalink
Get rid of blocking behaviour, unify Chrome and Firefox processors, u…
Browse files Browse the repository at this point in the history
…pdate readme
  • Loading branch information
psidex committed Apr 11, 2023
1 parent 1723a32 commit afc2cca
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 102 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ If you want to import bangs from DuckDuckGo, see [this page](./ddg/README.md).

## How the extension works

CBS uses the `WebRequestBlocking` API to intercept requests to the supported search engines, and if a bang is found, redirects the user to the chosen URL with the query inserted.
CBS uses the `webRequest.onBeforeRequest` event listener to listen for requests to the supported search engines, and if a bang is found, sends the user to the chosen URL with the query inserted, using the `tabs.update` API.

## Development

Expand Down
4 changes: 1 addition & 3 deletions manifest.firefox.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"permissions": [
"webRequestBlocking"
],
"permissions": [],
"host_permissions": [],
"background": {
"scripts": [
Expand Down
33 changes: 0 additions & 33 deletions src/background/chrome.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/background/firefox.ts

This file was deleted.

29 changes: 10 additions & 19 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import browser from 'webextension-polyfill';
import {
dev, currentBrowser, version, hash, hostPermissions,
} from '../lib/esbuilddefinitions';
import chromeProcessRequest from './chrome';
import firefoxProcessRequest from './firefox';
import processRequest from './requests';
import { Settings } from '../lib/settings';
import * as legacy from './legacy';
import * as storage from '../lib/storage';
Expand Down Expand Up @@ -67,23 +66,15 @@ function main(): void {
}
});

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

main();
37 changes: 34 additions & 3 deletions src/background/shared.ts → src/background/requests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { WebRequest } from 'webextension-polyfill';
import browser, { WebRequest } from 'webextension-polyfill';

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

const possibleQueryParams = ['q', 'query', 'eingabe'];

export function shouldReject(blacklist: Readonly<string[]>, url: string): boolean {
// Should this URL be rejected provided the given blacklist?
function shouldReject(blacklist: Readonly<string[]>, url: string): boolean {
if (blacklist.includes(new URL(url).hostname)) {
return true;
}
Expand All @@ -25,7 +27,7 @@ function constructRedirect(redirectUrl: string, queryText: string): string {
* @param request (Optional) The request details object from a WebRequestBlocking event.
* @returns A list of redirections to issue.
*/
export async function getRedirects(
async function getRedirects(
reqUrl: string,
request: WebRequest.OnBeforeRequestDetailsType | undefined = undefined,
): Promise<string[]> {
Expand Down Expand Up @@ -84,3 +86,32 @@ export async function getRedirects(

return Promise.resolve(redirects);
}

export default async function processRequest(
r: WebRequest.OnBeforeRequestDetailsType,
): Promise<void> {
if (r.type !== 'main_frame') {
return Promise.resolve();
}

if (shouldReject(await getIgnoredDomains(), r.url)) {
return Promise.resolve();
}

// From the current URL, get the redirections (if any) to apply.
const redirections = await getRedirects(r.url, r);

if (redirections.length === 0) {
return Promise.resolve();
}

// Open all URLs (except the first) in new tabs
for (let i = 1; i < redirections.length; i += 1) {
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] });

return Promise.resolve();
}

0 comments on commit afc2cca

Please sign in to comment.