SearchSwitch/v2/background.js

53 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-09-27 18:40:42 -03:00
function switchSearch(tab) {
const bingSearch = "https://www.bing.com/search?q=";
const googleSearch = "https://www.google.com/search?q=";
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 = bingImageSearch + justSearch + '&';
}
//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.browserAction.onClicked.addListener((tab) => {
chrome.tabs.update({url: switchSearch(tab)}).then(r => console.log(r))
}
)