Total rewrite of how switching works

Add compatibility with most search engine tabs
This commit is contained in:
Isaac Shoebottom 2022-09-28 19:13:51 -03:00
parent 1f5f00f82a
commit 49bad5d35c
4 changed files with 103 additions and 44 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
output
output\*
output\*.*

Binary file not shown.

View File

@ -3,4 +3,4 @@ $compress = @{
CompressionLevel = "NoCompression"
DestinationPath = "output\SearchSwitch.zip"
}
Compress-Archive @compress
Compress-Archive @compress -Force

View File

@ -1,54 +1,113 @@
function switchSearch(tab) {
const bingSearch = "https://www.bing.com/search?";
const googleSearch = "https://www.google.com/search?";
const bingSearch = "https://www.bing.com/search?"
const bingImageSearch = "https://www.bing.com/images/search?"
const googleImageSearchSubstring = "&tbm=isch";
const url = tab.url;
const bingVideoSearch = "https://www.bing.com/videos/search?"
const bingMapSearch = "https://www.bing.com/maps?"
const bingNewsSearch = "https://www.bing.com/news/search?"
const bingShoppingSearch = "https://www.bing.com/shop?"
const googleSearch = "https://www.google.com/search?"
const googleImageSearchSubstring = "&tbm=isch"
const googleVideoSearchSubstring = "&tbm=vid"
const googleMapsSearch = "https://www.google.com/maps?"
const googleMapsSearchRewrite = "https://www.google.com/maps"
const googleNewsSearchSubstring = "&tbm=nws"
const googleShoppingSearchSubstring = "&tbm=shop"
const debug = false
function switchSearch(tab) {
const url = tab.url
let newURL;
let removeEngine;
let searchStart;
let searchEnd;
let justSearch;
//Handle bing searches
//This is the way it should be done, TODO: Refactor rest of methods to be like this
if (url.substring(0, bingSearch.length) === bingSearch) {
removeEngine = url.slice(bingSearch.length);
searchStart = removeEngine.indexOf('&q=') + 3;
justSearch = removeEngine.substring(searchStart);
searchEnd = justSearch.indexOf('&');
justSearch = justSearch.substring(0, searchEnd);
newURL = googleSearch + 'q=' +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 + '&';
return getSwitchedSearch(bingSearch, googleSearch, url)
}
//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 getSwitchedGoogleSearch(bingImageSearch, googleSearch, googleImageSearchSubstring, url)
}
//Handle bing video searches
else if (url.substring(0, bingVideoSearch.length) === bingVideoSearch){
return getSwitchedGoogleSearch(bingVideoSearch, googleSearch, googleVideoSearchSubstring, url)
}
//Handle bing maps searches
else if (url.substring(0, bingMapSearch.length) === bingMapSearch){
return getSwitchedSearch(bingMapSearch, googleMapsSearch, url)
}
//Handle bing news searches
else if (url.substring(0, bingNewsSearch.length) === bingNewsSearch){
return getSwitchedGoogleSearch(bingNewsSearch, googleSearch, googleNewsSearchSubstring, url)
}
//Handle bing shopping searches
else if (url.substring(0, bingShoppingSearch.length) === bingShoppingSearch){
return getSwitchedGoogleSearch(bingShoppingSearch, googleSearch, googleShoppingSearchSubstring, url)
}
return newURL;
//--------------------------------------
//Handle Google image searches
else if (url.indexOf(googleImageSearchSubstring) > googleSearch.length) {
return getSwitchedSearch(googleSearch, bingImageSearch, url)
}
//Handle google video searches
else if (url.indexOf(googleVideoSearchSubstring) > googleSearch.length) {
return getSwitchedSearch(googleSearch, bingVideoSearch, url)
}
//Handle Google Maps searches
//Needs two for url before and after rewrite
else if (url.substring(0, googleMapsSearch.length) === googleMapsSearch) {
return getSwitchedSearch(googleMapsSearch, bingMapSearch, url)
}
else if (url.substring(0, googleMapsSearchRewrite.length) === googleMapsSearchRewrite) {
return getSwitchedSearch(googleMapsSearchRewrite, bingMapSearch, url)
}
//Handle Google News searches
else if (url.indexOf(googleNewsSearchSubstring) > googleSearch.length) {
return getSwitchedSearch(googleSearch, bingNewsSearch, url)
}
//Handle Google shopping searches
else if (url.indexOf(googleShoppingSearchSubstring) > googleSearch.length) {
return getSwitchedSearch(googleSearch, bingShoppingSearch, url)
}
//Handle Google searches
else if (url.substring(0, googleSearch.length) === googleSearch) {
return getSwitchedSearch(googleSearch, bingSearch, url)
}
}
function getSwitchedSearch(currentEngine, newEngine, url) {
let querySelector = '&q='
let separator = '&'
//google maps uses different url scheme
if (currentEngine === googleMapsSearchRewrite) {
querySelector = "/search/"
separator = '/'
}
let removeEngine = url.slice(currentEngine.length)
let searchStart = removeEngine.indexOf(querySelector) + querySelector.length
let justSearch = removeEngine.substring(searchStart)
let searchEnd = justSearch.indexOf(separator)
if (searchEnd > 0) {
justSearch = justSearch.substring(0, searchEnd)
}
if (debug) {
console.debug("------------------ SearchSwitch Diagnostics ------------------")
console.debug("Current Engine: ", currentEngine)
console.debug("Next Engine: ", newEngine)
console.debug("URL: ", url)
console.debug("Search query selector: ", querySelector)
console.debug("Raw Search term with engine removed: ", removeEngine)
console.debug("Index of the start of the real search: ", searchStart)
console.debug("Index of the end of the real search: ", searchEnd)
console.debug("The text that is just the raw search: ", justSearch)
console.debug("--------------------------------------------------------------")
}
return newEngine + 'q=' + justSearch
}
function getSwitchedGoogleSearch(currentEngine, newEngine, substring, url) {
let beforeSubstring = getSwitchedSearch(currentEngine, newEngine, url)
return beforeSubstring + substring
}
chrome.browserAction.onClicked.addListener((tab) => {