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

Search selected/entered via omnibar on background #1770

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/content_scripts/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/content_scripts/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,14 +582,15 @@ 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("<Space>", "pause/resume on youtube", function() {
* var btn = document.querySelector("button.ytp-ad-overlay-close-button") || document.querySelector("button.ytp-ad-skip-button") || document.querySelector('ytd-watch-flexy button.ytp-play-button');
* btn.click();
* }, {domain: /youtube.com/i});
*/
function tabOpenLink(str, simultaneousness) {
function tabOpenLink(str, simultaneousness,inBackground = false) {
simultaneousness = simultaneousness || 5;

var urls;
Expand All @@ -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
});
Expand Down