From 7717a1917473d3d3fe0f291f488f56ccad44b77a Mon Sep 17 00:00:00 2001 From: mationic Date: Fri, 14 Oct 2016 18:38:35 +0200 Subject: [PATCH 1/2] autocompletion for startpage.com & linguee.de (ger<->eng) --- content_scripts/complete.js | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/content_scripts/complete.js b/content_scripts/complete.js index 15ab283a..6442d0e2 100644 --- a/content_scripts/complete.js +++ b/content_scripts/complete.js @@ -518,4 +518,43 @@ Complete.engines = { }); } }, + + startpage: { + baseUrl: 'https://www.startpage.com', + requestUrl: 'https://www.startpage.com/do/asearch?query=', + apiUrl: 'https://www.startpage.com/do/suggest?query=%s', + queryApi: function(query, callback) { + httpRequest({ + url: this.apiUrl.embedString(encodeURIComponent(query)), + json: false + }, function(response) { + callback(response.split(/\r?\n/)); + }); + } + }, + + linguee: { + baseUrl: 'https://www.linguee.de/', + requestUrl: 'https://www.linguee.de/deutsch-englisch/search?source=auto&query=', + apiUrl: 'https://www.linguee.de/deutsch-englisch/search?qe=%s&source=auto"', + queryApi: function(query, callback) { + httpRequest({ + url: this.apiUrl.embedString(encodeURIComponent(query)), + json: false + }, function(response) { + var _obj = (new DOMParser()).parseFromString(response, "text/html"); + callback([].slice.call(_obj.querySelectorAll(".autocompletion_item")).map(function(e) { + var item = e.querySelector(".main_item"), + translations = ""; + e.querySelectorAll(".translation_item").forEach( function(child) { + var n, filter = function(node) { return ( node.length > 2 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT ); }, + walk = document.createTreeWalker(child, NodeFilter.SHOW_TEXT, filter, false); + while (n = walk.nextNode()) + translations += n.textContent.trim() + ", "; + }); + return [ item.textContent + " -> " + translations.slice(0,-2), "https://www.linguee.de" + item.getAttribute('href') ]; + })); + }); + } + }, }; From 3f7a554af6e09525a66e7ee5677d23474e42309b Mon Sep 17 00:00:00 2001 From: mationic Date: Sat, 15 Oct 2016 19:48:38 +0200 Subject: [PATCH 2/2] fixed translation results --- content_scripts/complete.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/content_scripts/complete.js b/content_scripts/complete.js index 6442d0e2..f83ab9ac 100644 --- a/content_scripts/complete.js +++ b/content_scripts/complete.js @@ -547,10 +547,9 @@ Complete.engines = { var item = e.querySelector(".main_item"), translations = ""; e.querySelectorAll(".translation_item").forEach( function(child) { - var n, filter = function(node) { return ( node.length > 2 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT ); }, - walk = document.createTreeWalker(child, NodeFilter.SHOW_TEXT, filter, false); - while (n = walk.nextNode()) - translations += n.textContent.trim() + ", "; + child.childNodes.forEach( function(data) { + if (data.nodeType == Node.TEXT_NODE && data.length > 1) translations += data.textContent.trim() + ", "; + }); }); return [ item.textContent + " -> " + translations.slice(0,-2), "https://www.linguee.de" + item.getAttribute('href') ]; }));