Skip to content

Commit

Permalink
🚧 Code for loading search engines
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Nov 11, 2023
1 parent b59ccd1 commit b2834ca
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"files.associations": {
"*.manifest.append": "chrome-manifest"
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dev-content": "webpack serve --mode development",
"dev-modules": "tsc -w -p ./tsconfigs/tsconfig.modules.json",
"dev-actors": "tsc -w -p ./tsconfigs/tsconfig.actors.json",
"rt:slink": "rm --force ./.store/artifact.tar.bz2 && ln -s ../../experiment-runtime/dist/quark-runtime-1.0.0.en-US.linux-x86_64.tar.bz2 ./.store/artifact.tar.bz2",
"rt:slink": "rm --force ./.store/artifact.tar.bz2 && ln -s ../../experiment-runtime/dist/experiment-runtime-1.0.0.en-US.linux-x86_64.tar.bz2 ./.store/artifact.tar.bz2",
"format": "pnpm script:license-check --fix && prettier . --write"
},
"keywords": [],
Expand Down
4 changes: 4 additions & 0 deletions scripts/lib/files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { readFile, writeFile } from 'fs/promises'

import { getArtifactFile } from './constants.js'
Expand Down
4 changes: 2 additions & 2 deletions src/content/browser/lib/Deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Deferred<T, E = unknown> {
* If the value is a Promise then the associated promise assumes the state
* of Promise passed as value.
*/
resolve: null | ((val: T) => void) = null
resolve!: (val: T) => void

/* A method to reject the assocaited Promise with the value passed.
* If the promise is already settled it does nothing.
Expand All @@ -23,7 +23,7 @@ export class Deferred<T, E = unknown> {
* Generally its an Error object. If however a Promise is passed, then the Promise
* itself will be the reason for rejection no matter the state of the Promise.
*/
reject: null | ((err: E) => void) = null
reject!: (err: E) => void

/* A newly created Pomise object.
* Initially in pending state.
Expand Down
4 changes: 4 additions & 0 deletions src/content/browser/lib/binaryEnums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { curry } from 'fnts'

export const isBitSet = curry((bit: number, num: number) => (num & bit) !== 0)
Expand Down
69 changes: 69 additions & 0 deletions src/content/browser/lib/search/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import type { AddonSearchEngine } from 'resource://gre/modules/AddonSearchEngine.sys.mjs'
import { Deferred } from '../Deferred'
import { lazyESModuleGetters } from '../../../shared/TypedImportUtilities'

const lazy = lazyESModuleGetters({
AddonSearchEngine: 'resource://gre/modules/AddonSearchEngine.sys.mjs',
SearchUtils: 'resource://gre/modules/SearchUtils.sys.mjs',
})

const SEARCH_ENGINE_IDS = [
'[email protected]',
'[email protected]',
] as const

class SearchService {
private initPromise: Deferred<void> | undefined
private searchEngines: Deferred<AddonSearchEngine[]> = new Deferred()

constructor() {}

/**
* Assorted fixups for mozilla APIs that are hard coded to use mozilla.org etc
*/
private fixup() {
const generalSearchEngines = lazy.SearchUtils
.GENERAL_SEARCH_ENGINE_IDS as Set<string>
const values = [...generalSearchEngines.values()]
for (const value of values) {
generalSearchEngines.delete(value)
generalSearchEngines.add(
value.replace(/@search\.mozilla\.org$/, '@search.fushra.com'),
)
}
}

private async init() {
if (this.initPromise) return await this.initPromise.promise
this.initPromise = new Deferred()

this.fixup()

const locale = lazy.SearchUtils.DEFAULT_TAG
const engines: AddonSearchEngine[] = []

for (const extensionID of SEARCH_ENGINE_IDS) {
const engine = new (lazy.AddonSearchEngine as any)({
isAppProvided: true,
details: { extensionID, locale },
})
await engine.init({ locale })

engines.push(engine)
}

this.searchEngines.resolve(engines)
this.initPromise.resolve()
}

async getSearchEngines() {
await this.init()
return await this.searchEngines.promise
}
}

export const searchService = new SearchService()
4 changes: 0 additions & 4 deletions src/content/browser/lib/xul/search.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/content/shared/components/Spinner.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<div class="spinner" />

<style>
Expand Down
2 changes: 1 addition & 1 deletion static/search-extensions/ddg/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"name": "DuckDuckGo",
"search_url": "https://duckduckgo.com/",
"search_form": "https://duckduckgo.com/",
"search_url_get_params": "t=ffab&q={searchTerms}",
"search_url_get_params": "t=fushra&q={searchTerms}",
"suggest_url": "https://ac.duckduckgo.com/ac/",
"suggest_url_get_params": "q={searchTerms}&type=list"
}
Expand Down
8 changes: 0 additions & 8 deletions static/search-extensions/google/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
}
},
"hidden": true,
"default_locale": "en",
"icons": {
"16": "favicon.ico"
},
Expand All @@ -21,13 +20,6 @@
"search_url": "https://www.google.com/search",
"search_form": "https://www.google.com/search",
"suggest_url": "https://www.google.com/complete/search?client=fushra&q={searchTerms}",
"params": [
{
"name": "channel",
"condition": "pref",
"pref": "google_channel_row"
}
],
"search_url_get_params": "client=fushra&q={searchTerms}"
}
}
Expand Down

0 comments on commit b2834ca

Please sign in to comment.