Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urlbar focus & nightly only features #26

Merged
merged 1 commit into from Nov 9, 2020
Merged
Changes from all commits
Commits
File filter
Filter file types
Jump to
Jump to file
Failed to load files.

Always

Just for now

Urlbar focus & nightly only features

  • Loading branch information
chrmod committed Nov 9, 2020
commit 1fd6f7590b90f122a87e72391906fe4331545be7
@@ -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/",
@@ -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
@@ -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();
});
}
})()
@@ -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;
}
}
}
}
};
@@ -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();
}
}
}
}
};
};
@@ -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": [
@@ -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"
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.