From 008ff52635eea08d98d26b403b82ebd040eec320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emin=20Umut=20Ger=C3=A7ek?= Date: Thu, 30 Jun 2022 23:31:36 +0300 Subject: [PATCH] Add search background basics --- src/content_scripts/common/api.js | 14 ++++++++++++-- src/content_scripts/common/utils.js | 6 ++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/content_scripts/common/api.js b/src/content_scripts/common/api.js index 88dcd2d02..01d327533 100644 --- a/src/content_scripts/common/api.js +++ b/src/content_scripts/common/api.js @@ -352,7 +352,9 @@ function createAPI(clipboard, insert, normal, hints, visual, front, browser) { * }); */ function addSearchAlias(alias, prompt, search_url, search_leader_key, suggestion_url, callback_to_parse_suggestion, only_this_site_key, options) { + var background_tab_key = ';'; // TODO _addSearchAlias(alias, prompt, search_url, suggestion_url, callback_to_parse_suggestion, options); + function ssw() { searchSelectedWith(search_url); } @@ -361,12 +363,19 @@ function createAPI(clipboard, insert, normal, hints, visual, front, browser) { front.openOmnibar({type: "SearchEngine", extra: alias}); }); vmapkey((search_leader_key || 's') + alias, '', ssw); + function ssw2() { searchSelectedWith(search_url, true); } mapkey((search_leader_key || 's') + (only_this_site_key || 'o') + alias, '', ssw2); vmapkey((search_leader_key || 's') + (only_this_site_key || 'o') + alias, '', ssw2); + function ssw3(){ + searchSelectedWith(search_url, false, false, "", true) + } + mapkey((search_leader_key || 's') + (background_tab_key || 'b') + alias, '', ssw3); + vmapkey((search_leader_key || 's') + (background_tab_key || 'b') + alias, '', ssw3); + var capitalAlias = alias.toUpperCase(); if (capitalAlias !== alias) { function ssw4() { @@ -416,11 +425,12 @@ function createAPI(clipboard, insert, normal, hints, visual, front, browser) { * @param {boolean} [onlyThisSite=false] whether to search only within current site, need support from the provided search engine. * @param {boolean} [interactive=false] whether to search in interactive mode, in case that you need some small modification on the selected content. * @param {string} [alias=""] only used with interactive mode, in such case the url from `se` is ignored, SurfingKeys will construct search URL from the alias registered by `addSearchAlias`. + * @param {boolean} [inBackground=false] open selected text in a not focused (background) tab * * @example * searchSelectedWith('https://translate.google.com/?hl=en#auto/en/'); */ - function searchSelectedWith(se, onlyThisSite, interactive, alias) { + function searchSelectedWith(se, onlyThisSite, interactive, alias, inBackground=false) { clipboard.read(function(response) { var query = window.getSelection().toString() || response.data; if (onlyThisSite) { @@ -429,7 +439,7 @@ function createAPI(clipboard, insert, normal, hints, visual, front, browser) { if (interactive) { front.openOmnibar({type: "SearchEngine", extra: alias, pref: query}); } else { - tabOpenLink(constructSearchURL(se, encodeURIComponent(query))); + tabOpenLink(constructSearchURL(se, encodeURIComponent(query)), 5,inBackground); // TODO } }); } diff --git a/src/content_scripts/common/utils.js b/src/content_scripts/common/utils.js index 331c0f1d5..5596fef6c 100644 --- a/src/content_scripts/common/utils.js +++ b/src/content_scripts/common/utils.js @@ -582,6 +582,7 @@ function constructSearchURL(se, word) { * * @param {string} str links to be opened, the links should be split by `\n` if there are more than one. * @param {number} [simultaneousness=5] how many tabs will be opened simultaneously, the rest will be queued and opened later whenever a tab is closed. + * @param {boolean} [inBackground=false] open link in background * * @example * mapkey("", "pause/resume on youtube", function() { @@ -589,7 +590,7 @@ function constructSearchURL(se, word) { * btn.click(); * }, {domain: /youtube.com/i}); */ -function tabOpenLink(str, simultaneousness) { +function tabOpenLink(str, simultaneousness,inBackground = false) { simultaneousness = simultaneousness || 5; var urls; @@ -612,7 +613,8 @@ function tabOpenLink(str, simultaneousness) { urls.slice(0, simultaneousness).forEach(function(url) { RUNTIME("openLink", { tab: { - tabbed: true + tabbed: true, + active: !inBackground }, url: url });