From 6100a6ec8297fe915ec2407b259de7ac8d06504e Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Fri, 1 Jul 2022 05:46:28 -0300 Subject: [PATCH] Add support for google and bing image searching --- background.js | 76 +++++++++++++++++++++++++++++++++------------------ manifest.json | 4 +-- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/background.js b/background.js index ed1eb0a..5690d8c 100644 --- a/background.js +++ b/background.js @@ -1,36 +1,60 @@ function switchSearch(tab) { - const bingSearch = "https://www.bing.com/search?q="; - const googleSearch = "https://www.google.com/search?q="; - const url = tab.url; + const bingSearch = "https://www.bing.com/search?q="; + const googleSearch = "https://www.google.com/search?q="; - let newURL; - let removeEngine; - let searchEnd; - let justSearch; - if (url.substring(0, bingSearch.length) === bingSearch) { - removeEngine = url.slice(bingSearch.length); - searchEnd = removeEngine.indexOf('&'); - justSearch = removeEngine.substring(0, searchEnd); - - newURL = googleSearch + justSearch + '&'; - - } - else if (url.substring(0, googleSearch.length) === googleSearch) { + const bingImageSearch = "https://www.bing.com/images/search?q=" + const googleImageSearchSubstring = "&tbm=isch"; + const url = tab.url; + + let newURL; + let removeEngine; + let searchEnd; + let justSearch; + //Handle bing searches + if (url.substring(0, bingSearch.length) === bingSearch) { + removeEngine = url.slice(bingSearch.length); + searchEnd = removeEngine.indexOf('&'); + justSearch = removeEngine.substring(0, searchEnd); + + newURL = googleSearch + justSearch + '&'; + + } + //Handle Google image searches + else if (url.indexOf(googleImageSearchSubstring) > googleSearch.length) { removeEngine = url.slice(googleSearch.length); searchEnd = removeEngine.indexOf('&'); justSearch = removeEngine.substring(0, searchEnd); - newURL = bingSearch + justSearch + '&'; + newURL = bingImageSearch + justSearch + '&'; } - return newURL; + //Handle Google searches + else if (url.substring(0, googleSearch.length) === googleSearch) { + removeEngine = url.slice(googleSearch.length); + searchEnd = removeEngine.indexOf('&'); + justSearch = removeEngine.substring(0, searchEnd); + + newURL = bingSearch + justSearch + '&'; + } + //Handle bing image searches + else if (url.substring(0, bingImageSearch.length) === bingImageSearch) { + removeEngine = url.slice(bingImageSearch.length) + searchEnd = removeEngine.indexOf('&'); + justSearch = removeEngine.substring(0, searchEnd); + + newURL = googleSearch + justSearch + googleImageSearchSubstring + '&'; + } + + + return newURL; } chrome.action.onClicked.addListener((tab) => { - chrome.scripting.executeScript({ - target: {tabId: tab.id}, - func: switchSearch, - args: [tab] - }, async (redirect) => { - await chrome.tabs.update({url: redirect[0].result}); - } ); - }); \ No newline at end of file + chrome.scripting.executeScript({ + target: {tabId: tab.id}, + func: switchSearch, + args: [tab] + }, async (redirect) => { + await chrome.tabs.update({url: redirect[0].result}); + }); + } +) \ No newline at end of file diff --git a/manifest.json b/manifest.json index 670207e..7a85847 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,13 @@ { "name": "SearchSwitcher", - "version": "0.0.1", + "version": "0.0.2", "description": "Search switcher", "manifest_version": 3, "author": "Isaac Shoebottom", "action": { "default_title": "Switch Search" }, - "permissions": ["activeTab", "tabs", "scripting"], + "permissions": ["activeTab", "scripting"], "background": { "service_worker": "background.js" }