From 83c9da607209f5ed45a5a02740114568610ffec7 Mon Sep 17 00:00:00 2001 From: Krzysztof Jan Modras Date: Mon, 2 Nov 2020 14:49:03 +0100 Subject: [PATCH 1/3] Top sites on ghosterysearch --- src/background.js | 9 +++++- src/content/top-sites.js | 68 ++++++++++++++++++++++++++++++++++++++++ src/manifest.json | 3 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/content/top-sites.js diff --git a/src/background.js b/src/background.js index 9b2a601..02f85d5 100644 --- a/src/background.js +++ b/src/background.js @@ -90,10 +90,17 @@ async function start() { }; }, { urls: [`${SERP_BASE_URL}/search*`]}, ["blocking", "requestHeaders"]); - browser.runtime.onMessage.addListener(({ action }) => { + + browser.runtime.onMessage.addListener(async ({ action }) => { if (action === 'getTokenCount') { return Promise.resolve(tokenPool.tokens.length); } + if (action === 'getTopSites') { + return (await browser.topSites.get({ + newtab: true, + includeFavicon: true, + })).filter(site => site.type === 'url'); + } return false; }) } diff --git a/src/content/top-sites.js b/src/content/top-sites.js new file mode 100644 index 0000000..d807520 --- /dev/null +++ b/src/content/top-sites.js @@ -0,0 +1,68 @@ +"use strict"; + +(async function () { + function cleanup() { + const $content = document.querySelector('.content'); + const $oldTopSites = $content.querySelector('.topsites'); + if ($oldTopSites) { + $content.removeChild($oldTopSites); + } + } + + async function loadTopSites() { + const $content = document.querySelector('.content'); + const topSites = await browser.runtime.sendMessage({ + action: 'getTopSites' + }); + console.warn(topSites) + + const $topSitesWrapper = document.createElement('div'); + $topSitesWrapper.classList.add('topsites'); + $topSitesWrapper.style.display = 'flex'; + $topSitesWrapper.style.flexDirection = 'row'; + $topSitesWrapper.style.margin = '40px 0 0 0'; + $topSitesWrapper.style.flexWrap = 'wrap'; + $topSitesWrapper.style.justifyContent = 'center'; + + topSites.forEach(site => { + const $site = document.createElement('a'); + $site.setAttribute('href', site.url); + $site.style.display = 'flex'; + $site.style.flexDirection = 'column'; + $site.style.alignItems = 'center'; + $site.style.margin = '10px 7px'; + $site.style.textDecoration = 'none'; + $site.style.color = 'black'; + + const $favicon = document.createElement('img'); + $favicon.setAttribute('src', site.favicon); + $favicon.style.height = '48px'; + $favicon.style.width = '48px'; + $favicon.style.boxShadow = 'inset 0 0 0 1px rgba(249, 249, 250, 0.2), 0 1px 8px 0 rgba(12, 12, 13, 0.2)'; + $favicon.style.transition = 'box-shadow 150ms'; + $favicon.style.borderRadius = '5px'; + $favicon.style.backgroundColor = 'white'; + $site.appendChild($favicon); + + const $title = document.createElement('span'); + $title.innerText = site.title; + $title.style.marginTop = '5px'; + $site.appendChild($title); + + $topSitesWrapper.appendChild($site); + }); + + $content.appendChild($topSitesWrapper); + } + + if (document.readyState === 'complete' || document.readyState === 'interactive') { + cleanup(); + loadTopSites(); + } else { + document.addEventListener('DOMContentLoaded', function onLoad() { + document.removeEventListener('DOMContentLoaded', onLoad); + cleanup(); + loadTopSites(); + }); + } +}()); diff --git a/src/manifest.json b/src/manifest.json index 186afc8..4029c1a 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -19,7 +19,7 @@ "https://*.ghosterysearch.com/", "http://localhost/*" ], - "js": ["content/login-cta.js"] + "js": ["content/login-cta.js", "content/top-sites.js"] }], "manifest_version": 2, "name": "Ghostery Search", @@ -28,6 +28,7 @@ "cookies", "webRequest", "webRequestBlocking", + "topSites", "https://www.ghostery.com/*", "https://consumerapi.ghostery.com/*", "https://www.ghosterystage.com/*", From 78f024be00cd48a1140d5aa60a4e36ad018b8875 Mon Sep 17 00:00:00 2001 From: Krzysztof Jan Modras Date: Mon, 2 Nov 2020 15:44:46 +0100 Subject: [PATCH 2/3] Additional search engines on ghosterysearch --- src/background.js | 13 +++- src/content/additional-search-engines.js | 76 ++++++++++++++++++++++++ src/content/top-sites.js | 1 - src/manifest.json | 7 +++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/content/additional-search-engines.js diff --git a/src/background.js b/src/background.js index 02f85d5..ba02381 100644 --- a/src/background.js +++ b/src/background.js @@ -91,7 +91,7 @@ async function start() { }, { urls: [`${SERP_BASE_URL}/search*`]}, ["blocking", "requestHeaders"]); - browser.runtime.onMessage.addListener(async ({ action }) => { + browser.runtime.onMessage.addListener(async ({ action, args }, { tab }) => { if (action === 'getTokenCount') { return Promise.resolve(tokenPool.tokens.length); } @@ -101,6 +101,17 @@ async function start() { includeFavicon: true, })).filter(site => site.type === 'url'); } + if (action === 'getSearchEngines') { + return browser.search.get(); + } + if (action === 'search') { + const { query, engine } = args[0]; + return browser.search.search({ + query, + engine, + tabId: tab.id, + }); + } return false; }) } diff --git a/src/content/additional-search-engines.js b/src/content/additional-search-engines.js new file mode 100644 index 0000000..a316d9c --- /dev/null +++ b/src/content/additional-search-engines.js @@ -0,0 +1,76 @@ +"use strict"; + +(async function () { + + function cleanup() { + const $content = document.querySelector('.main-content'); + const $searchEngines = document.querySelector('.searchengines'); + if ($searchEngines) { + $content.removeChild($searchEngines); + } + } + + async function addSearchEngines() { + const query = new URLSearchParams(window.location.search).get('q'); + const $content = document.querySelector('.main-content'); + const searchEngines = await browser.runtime.sendMessage({ + action: 'getSearchEngines', + }); + const $searchEnginesWrapper = document.createElement('div'); + $searchEnginesWrapper.classList.add('searchengines'); + $searchEnginesWrapper.style.display = 'flex'; + $searchEnginesWrapper.style.flexDirection = 'row'; + $searchEnginesWrapper.style.margin = '40px 0 40px 0'; + $searchEnginesWrapper.style.flexWrap = 'wrap'; + + searchEngines.forEach(engine => { + const $engine = document.createElement('a'); + $engine.style.display = 'flex'; + $engine.style.flexDirection = 'column'; + $engine.style.alignItems = 'center'; + $engine.style.margin = '10px 7px'; + $engine.style.textDecoration = 'none'; + $engine.style.color = 'black'; + $engine.style.cursor = 'pointer'; + $engine.addEventListener('click', () => { + browser.runtime.sendMessage({ + action: 'search', + args: [{ + query, + engine: engine.name, + }], + }); + }); + + const $favicon = document.createElement('img'); + $favicon.setAttribute('src', engine.favIconUrl); + $favicon.style.height = '24px'; + $favicon.style.width = '24px'; + $favicon.style.boxShadow = 'inset 0 0 0 1px rgba(249, 249, 250, 0.2), 0 1px 8px 0 rgba(12, 12, 13, 0.2)'; + $favicon.style.transition = 'box-shadow 150ms'; + $favicon.style.borderRadius = '5px'; + $favicon.style.backgroundColor = 'white'; + $engine.appendChild($favicon); + + const $title = document.createElement('span'); + $title.innerText = engine.name; + $title.style.marginTop = '5px'; + $engine.appendChild($title); + + $searchEnginesWrapper.appendChild($engine); + }); + + $content.appendChild($searchEnginesWrapper); + } + + if (document.readyState === 'complete' || document.readyState === 'interactive') { + cleanup(); + addSearchEngines(); + } else { + document.addEventListener('DOMContentLoaded', function onLoad() { + document.removeEventListener('DOMContentLoaded', onLoad); + cleanup(); + addSearchEngines(); + }); + } +}()); diff --git a/src/content/top-sites.js b/src/content/top-sites.js index d807520..aa3ec2c 100644 --- a/src/content/top-sites.js +++ b/src/content/top-sites.js @@ -14,7 +14,6 @@ const topSites = await browser.runtime.sendMessage({ action: 'getTopSites' }); - console.warn(topSites) const $topSitesWrapper = document.createElement('div'); $topSitesWrapper.classList.add('topsites'); diff --git a/src/manifest.json b/src/manifest.json index 4029c1a..d63b471 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -20,10 +20,17 @@ "http://localhost/*" ], "js": ["content/login-cta.js", "content/top-sites.js"] + }, { + "matches": [ + "https://*.ghosterysearch.com/search*", + "http://localhost/search*" + ], + "js": ["content/additional-search-engines.js"] }], "manifest_version": 2, "name": "Ghostery Search", "permissions": [ + "search", "storage", "cookies", "webRequest", From 67808faea5f4a9b471865b3f63b223a2b4bff695 Mon Sep 17 00:00:00 2001 From: Krzysztof Jan Modras Date: Mon, 2 Nov 2020 16:00:54 +0100 Subject: [PATCH 3/3] fixing race conditions --- src/background.js | 53 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/background.js b/src/background.js index ba02381..a82e5a6 100644 --- a/src/background.js +++ b/src/background.js @@ -89,31 +89,36 @@ async function start() { requestHeaders, }; }, { urls: [`${SERP_BASE_URL}/search*`]}, ["blocking", "requestHeaders"]); +} +browser.runtime.onMessage.addListener(async ({ action, args }, { tab }) => { + if (action === 'getTokenCount') { + return Promise.resolve(tokenPool.tokens.length); + } - browser.runtime.onMessage.addListener(async ({ action, args }, { tab }) => { - if (action === 'getTokenCount') { - return Promise.resolve(tokenPool.tokens.length); - } - if (action === 'getTopSites') { - return (await browser.topSites.get({ - newtab: true, - includeFavicon: true, - })).filter(site => site.type === 'url'); - } - if (action === 'getSearchEngines') { - return browser.search.get(); - } - if (action === 'search') { - const { query, engine } = args[0]; - return browser.search.search({ - query, - engine, - tabId: tab.id, - }); - } - return false; - }) -} + if (action === 'getTopSites') { + return (await browser.topSites.get({ + newtab: true, + includeFavicon: true, + })).filter(site => site.type === 'url'); + } + + if (action === 'getSearchEngines') { + return (await browser.search.get()).filter( + engine => engine.name !== browser.runtime.getManifest()["chrome_settings_overrides"]["search_provider"].name + ); + } + + if (action === 'search') { + const { query, engine } = args[0]; + return browser.search.search({ + query, + engine, + tabId: tab.id, + }); + } + + return false; +}); start();