From 1fd6f7590b90f122a87e72391906fe4331545be7 Mon Sep 17 00:00:00 2001 From: Krzysztof Jan Modras Date: Mon, 9 Nov 2020 17:46:50 +0100 Subject: [PATCH] Urlbar focus & nightly only features --- package.json | 2 +- src/background.js | 7 ++++ src/content/focus-urlbar.js | 25 ++++++++++++ src/experiment_apis/ghostery_child.js | 49 ++++++++++++++++++++++++ src/experiment_apis/ghostery_parent.js | 19 ++++++++- src/experiment_apis/ghostery_schema.json | 31 ++++++++++++++- src/manifest.json | 9 ++++- 7 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/content/focus-urlbar.js create mode 100644 src/experiment_apis/ghostery_child.js diff --git a/package.json b/package.json index d453716..ee1d5e7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "build": "web-ext build", - "start": "web-ext run --pref extensions.experiments.enabled=true" + "start": "web-ext run --pref extensions.experiments.enabled=true --pref security.sandbox.content.level=2" }, "webExt": { "sourceDir": "./src/", diff --git a/src/background.js b/src/background.js index c485014..b9b8b5f 100644 --- a/src/background.js +++ b/src/background.js @@ -108,12 +108,19 @@ browser.runtime.onMessage.addListener(async ({ action, args }, { tab }) => { } if (action === 'getTopSites') { + if (browser.ghostery.getPref('app.update.channel') !== 'release') { + return; + } return (await browser.topSites.get({ newtab: true, includeFavicon: true, })).filter(site => site.type === 'url'); } + if (action === 'focusUrlbar') { + browser.ghostery.query(args[0]); + } + if (action === 'getSearchEngines') { return (await browser.search.get()).filter( engine => engine.name !== browser.runtime.getManifest()["chrome_settings_overrides"]["search_provider"].name diff --git a/src/content/focus-urlbar.js b/src/content/focus-urlbar.js new file mode 100644 index 0000000..daaabf9 --- /dev/null +++ b/src/content/focus-urlbar.js @@ -0,0 +1,25 @@ +"use strict"; + +(async function () { + function observerSearchInput() { + const input$ = document.querySelector('#search-input'); + input$.addEventListener('input', () => { + browser.runtime.sendMessage({ + action: 'focusUrlbar', + args: [ + input$.value, + ], + }); + input$.value = ''; + }); + } + + if (document.readyState === 'complete' || document.readyState === 'interactive') { + observerSearchInput(); + } else { + document.addEventListener('DOMContentLoaded', function onLoad() { + document.removeEventListener('DOMContentLoaded', onLoad); + observerSearchInput(); + }); + } +})() diff --git a/src/experiment_apis/ghostery_child.js b/src/experiment_apis/ghostery_child.js new file mode 100644 index 0000000..5f111ac --- /dev/null +++ b/src/experiment_apis/ghostery_child.js @@ -0,0 +1,49 @@ +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm"); + +const COMPLEX_VALUE_RE = /^chrome:\/\/.+\/locale\/.+\.properties/; +const prefSvc = Services.prefs; + +this.ghostery = class extends ExtensionCommon.ExtensionAPI { + getAPI(context) { + return { + ghostery: { + getPref(prefName) { + let value = null; + try { + switch (prefSvc.getPrefType(prefName)) { + case prefSvc.PREF_BOOL: + value = prefSvc.getBoolPref(prefName); + break; + case prefSvc.PREF_STRING: { + let charVal = prefSvc.getCharPref(prefName); + // it might be a complex value + if (COMPLEX_VALUE_RE.test(charVal)) { + try { + charVal = prefSvc.getComplexValue( + prefName, + Components.interfaces.nsIPrefLocalizedString, + ).data; + } catch (e) { + break; + } + } + value = charVal; + break; + } + case prefSvc.PREF_INT: + value = prefSvc.getIntPref(prefName); + break; + case prefSvc.PREF_INVALID: + default: + break; + } + } catch (e) { + // nothing + } + return value; + } + } + } + } +}; diff --git a/src/experiment_apis/ghostery_parent.js b/src/experiment_apis/ghostery_parent.js index 2f59873..43b9672 100644 --- a/src/experiment_apis/ghostery_parent.js +++ b/src/experiment_apis/ghostery_parent.js @@ -1,6 +1,18 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { ExtensionUtils } = ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm"); +const { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm"); +const { ExtensionParent } = ChromeUtils.import("resource://gre/modules/ExtensionParent.jsm"); + const { ExtensionError } = ExtensionUtils; +const getWindow = (windowId) => { + const windowTracker = ExtensionParent.apiManager.global.windowTracker; + if (typeof windowId === 'number') { + return windowTracker.getWindow(windowId, null); + } + return windowTracker.getCurrentWindow(); +}; + global.ghostery = class extends ExtensionCommon.ExtensionAPI { getAPI(context) { return { @@ -18,7 +30,12 @@ global.ghostery = class extends ExtensionCommon.ExtensionAPI { Services.search.defaultEngine = searchEngine; }, + query(windowId, query) { + const window = getWindow(windowId); + window.gURLBar.value = query || ''; + window.gURLBar.focus(); + } } } } -}; \ No newline at end of file +}; diff --git a/src/experiment_apis/ghostery_schema.json b/src/experiment_apis/ghostery_schema.json index a7429d8..de56e82 100644 --- a/src/experiment_apis/ghostery_schema.json +++ b/src/experiment_apis/ghostery_schema.json @@ -1,6 +1,6 @@ [{ "namespace": "ghostery", - "description": "API to change the default search engine", + "description": "Ghostery Browser API", "functions": [ { "name": "setDefaultSearchEngine", @@ -13,6 +13,35 @@ "type": "string" } ] + }, + { + "name": "getPref", + "type": "function", + "description": "get the value of browser pref", + "parameters": [ + { + "type": "string", + "name": "prefName", + "description": "Browser pref name" + } + ], + "returns": { + "description": "Constant value" + } + }, + { + "name": "query", + "type": "function", + "description": "Focus the UrlBar", + "parameters": [{ + "type": "number", + "name": "windowId", + "optional": true + }, { + "type": "string", + "name": "query", + "optional": true + }] } ], "events": [ diff --git a/src/manifest.json b/src/manifest.json index 7edb7a8..78b1160 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,5 +1,5 @@ { - "version": "0.1.13", + "version": "0.1.14", "browser_specific_settings": { "gecko": { "id": "search@ghostery.com" @@ -19,7 +19,7 @@ "https://*.ghosterysearch.com/", "http://localhost/*" ], - "js": ["content/login-cta.js", "content/top-sites.js"] + "js": ["content/login-cta.js", "content/top-sites.js", "content/focus-urlbar.js"] }, { "matches": [ "https://*.ghosterysearch.com/search*", @@ -68,6 +68,11 @@ "scopes": ["addon_parent"], "paths": [["ghostery"]], "script": "experiment_apis/ghostery_parent.js" + }, + "child": { + "scopes": ["addon_child"], + "paths": [["ghostery"]], + "script": "experiment_apis/ghostery_child.js" } } }