From 63528355a9b9f492809a777d9ac8d6e529688b02 Mon Sep 17 00:00:00 2001 From: tilpner Date: Mon, 18 May 2020 13:04:08 +0200 Subject: Patch custom qwantjunior addon into omni.ja --- default.nix | 20 ++++++++++++ nix/lib.nix | 12 ++++++- profiles/addons/default.nix | 36 ++++++++++++++++++-- profiles/addons/qwantjunior/content-script.js | 34 +++++++++++++++++++ profiles/addons/qwantjunior/favicon.ico | Bin 0 -> 2285 bytes profiles/addons/qwantjunior/managed_storage.json | 20 ++++++++++++ profiles/addons/qwantjunior/manifest.json | 40 +++++++++++++++++++++++ 7 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 profiles/addons/qwantjunior/content-script.js create mode 100644 profiles/addons/qwantjunior/favicon.ico create mode 100644 profiles/addons/qwantjunior/managed_storage.json create mode 100644 profiles/addons/qwantjunior/manifest.json diff --git a/default.nix b/default.nix index 8fdf3e1..b7f291e 100644 --- a/default.nix +++ b/default.nix @@ -55,6 +55,26 @@ in rec { bundleConfig = { inherit (config) policies; preferences = ffLib.flattenAttrs config.preferences; + + # This is very hacky, but Firefox really resists setting the default search engine + # by any other means. Only builtin search engines are allowed to make themselves default + # without prompting for user consent. + patchOmniJaCommand = + let searchEnginesInfo = { + default = { + searchDefault = "qwantjunior"; + searchOrder = [ "qwantjunior" "ddg" ]; + visibleDefaultEngines = [ "qwantjunior" "ddg" ]; + }; + regionOverrides = {}; + locales = {}; + }; + in '' + pushd chrome/browser/search-extensions + cp -r ${profiles/addons/qwantjunior} ./qwantjunior + cp ${builtins.toFile "list.json" (builtins.toJSON searchEnginesInfo)} list.json + popd + ''; }; bundle = ffLib.bundle bundleConfig; diff --git a/nix/lib.nix b/nix/lib.nix index 1095ab4..93b3dc8 100644 --- a/nix/lib.nix +++ b/nix/lib.nix @@ -49,11 +49,20 @@ rec { final = lib.foldl lib.recursiveUpdate {} sanitised; in final; - bundle = { policies ? {}, preferences ? {} }: + patchOmniJa = src: script: pkgs.runCommand "omni.ja" { } '' + ${pkgs.unzip}/bin/unzip -q ${src} + + ${script} + + ${pkgs.zip}/bin/zip -qr9XD $out . + ''; + + bundle = { policies ? {}, preferences ? {}, patchOmniJaCommand ? "" }: let firefox = pkgs.firefox-unwrapped; policies' = mkPolicies policies; preferences' = mkPrefs preferences; + patchedOmniJa = patchOmniJa "${firefox}/lib/firefox/browser/omni.ja" patchOmniJaCommand; patched = pkgs.runCommand "firefox-bundle" { nativeBuildInputs = [ pkgs.nix ]; disallowedReferences = [ firefox ]; @@ -67,6 +76,7 @@ rec { mkdir $out/lib/firefox/distribution cp ${policies'} $out/lib/firefox/distribution/policies.json cp ${preferences'} $out/lib/firefox/defaults/pref/99-custom.js + cp ${patchedOmniJa} $out/lib/firefox/browser/omni.ja ''; wrapped = (pkgs.wrapFirefox patched { browserName = "firefox"; diff --git a/profiles/addons/default.nix b/profiles/addons/default.nix index 0e1827f..0253026 100644 --- a/profiles/addons/default.nix +++ b/profiles/addons/default.nix @@ -1,17 +1,24 @@ -{ lib, fetchurl }: +{ pkgs, lib, fetchurl, runCommand }: let # Extension IDs are used as keys, see .applications.gecko.id in manifest.json - addon = { id, url, sha256, settings ? null }: { + localAddon = { id, src, settings ? null }: { policies = { ExtensionSettings.${id} = { installation_mode = "force_installed"; - install_url = "file://${fetchurl { inherit url sha256; }}"; + install_url = "file://${src}"; }; } // (lib.optionalAttrs (settings != null) { "3rdparty".Extensions.${id} = settings; }); }; + + addon = { id, url, sha256, settings ? null }: localAddon { + inherit id settings; + src = fetchurl { + inherit url sha256; + }; + }; in { # This can be safe-ish, if extension installation is also disabled disableExtensionSignatureChecking.preferences = { @@ -40,6 +47,29 @@ in { settings.adminSettings = builtins.readFile ./ublock/config.json; }; + qwantjunior = localAddon { + id = "qwantjunior@search.mozilla.org"; + src = runCommand "addon.xpi" { nativeBuildInputs = [ pkgs.zip ]; } '' + SRC=${./qwantsearch} + cd $SRC + zip -r $out . + ''; + + settings.settings = { + searchRegionKey = "DE"; + searchLanguageKey = "de"; + interfaceLanguageKey = "de_de"; + }; + }; + + qwantjuniorSystem.policies = { + "3rdparty".Extensions."qwantjunior@search.mozilla.org".settings = { + searchRegionKey = "DE"; + searchLanguageKey = "de"; + interfaceLanguageKey = "de_de"; + }; + }; + /* borderify.policies = { ExtensionSettings."borderify@example.com" = { diff --git a/profiles/addons/qwantjunior/content-script.js b/profiles/addons/qwantjunior/content-script.js new file mode 100644 index 0000000..01b3976 --- /dev/null +++ b/profiles/addons/qwantjunior/content-script.js @@ -0,0 +1,34 @@ +"use strict"; + +function changeSettings(managed) { + var preferred = managed.settings; + var userStorage = JSON.parse(localStorage.getItem('user')); + var needsReload = false; + + if (userStorage == null) { + localStorage.setItem('user', JSON.stringify({ + userSetting: preferred + })); + + needsReload = true; + } else if (userStorage.version == "2") { + var setting = userStorage.userSetting; + + for (const [key, value] of Object.entries(preferred)) { + if (setting[key] != preferred[key]) { + setting[key] = preferred[key]; + needsReload = true; + } + } + + localStorage.setItem('user', JSON.stringify(userStorage)); + } + + if (needsReload) { + location.reload(); + } +} + +browser.storage.managed + .get("settings") + .then(changeSettings); diff --git a/profiles/addons/qwantjunior/favicon.ico b/profiles/addons/qwantjunior/favicon.ico new file mode 100644 index 0000000..5e44aac Binary files /dev/null and b/profiles/addons/qwantjunior/favicon.ico differ diff --git a/profiles/addons/qwantjunior/managed_storage.json b/profiles/addons/qwantjunior/managed_storage.json new file mode 100644 index 0000000..00afb5a --- /dev/null +++ b/profiles/addons/qwantjunior/managed_storage.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-03/schema#", + "type": "object", + "properties": { + "settings": { + "type": "object", + "properties": { + "searchRegionKey": { + "type": "string" + }, + "searchLanguageKey": { + "type": "string" + }, + "interfaceLanguageKey": { + "type": "string" + } + } + } + } +} diff --git a/profiles/addons/qwantjunior/manifest.json b/profiles/addons/qwantjunior/manifest.json new file mode 100644 index 0000000..f38a086 --- /dev/null +++ b/profiles/addons/qwantjunior/manifest.json @@ -0,0 +1,40 @@ +{ + "name": "QwantJunior", + "description": "Search Qwant Junior", + "manifest_version": 2, + "version": "1.0", + "applications": { + "gecko": { + "id": "qwantjunior@search.mozilla.org" + } + }, + "hidden": true, + "icons": { + "16": "favicon.ico" + }, + "web_accessible_resources": [ + "favicon.ico" + ], + "chrome_settings_overrides": { + "search_provider": { + "is_default": true, + "name": "Qwant Junior", + "search_url": "https://www.qwantjunior.com", + "search_form": "https://www.qwantjunior.com/?q={searchTerms}", + "search_url_get_params": "q={searchTerms}", + "suggest_url": "https://api.qwant.com/egp/suggest/", + "suggest_url_get_params": "q={searchTerms}&client=opensearch" + } + }, + + "permissions": [ + "storage", + "https://www.qwantjunior.com/*" + ], + + "content_scripts": [ + { "matches": [ "https://www.qwantjunior.com/*" ], + "run_at": "document_idle", + "js": [ "content-script.js" ] } + ] +} -- cgit v1.2.3