From 49bad5d35c7437ff9c20e43d07616cc322fafd81 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Wed, 28 Sep 2022 19:13:51 -0300 Subject: [PATCH] Total rewrite of how switching works Add compatibility with most search engine tabs --- .gitignore | 2 +- output/SearchSwitch.zip | Bin 2469 -> 5797 bytes scripts/PackXPI.ps1 | 2 +- v2/background.js | 143 ++++++++++++++++++++++++++++------------ 4 files changed, 103 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index fa91f44..cd39a73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ output -output\* \ No newline at end of file +output\*.* \ No newline at end of file diff --git a/output/SearchSwitch.zip b/output/SearchSwitch.zip index 0ed73f48578f996c2b0864c8e0f3552c2b2c8de4..703116f02285eac8e9cc996bf0a2402c66b2caa2 100644 GIT binary patch literal 5797 zcmb_g&5j#I5cURw*hqVekl@ssLt-mtyvi}b8=(+|XqT|W2^WOI*i*JAnIEUSZ3l(r zA>hPw@H9LF&j6yTyJxz4X8eaTtCj7kuIjI-GM3 z`11n)Pw?$Hn!KLzVwuO`D{*vj^UuF0MJ^;8C;4my5ueQ2kOgxo7vkPvuwJi2l7y2Y z8we$S5S-kwDo?U#20Qc;8Y5cOzfWQ)cBrptOuPCw(PD>k7A=I2_$jQ#4y7E2w89f8qFZvD4HGe$SNYK6osc%iV=QaK zF@zh!;UM#)s0CK>8LW9Cq0XC{er;Zxh9=51RBH!0hlQwl(QvJjY5Euewiinc>< zrGN?u@!AB9OP8? zTfnzf^LqyGOtG!pOysFHYw!nyrvR~Fa$T@#!D*pjW592~a0z44R078ZN3GX7_nyPT zUBz^iYy+qgLu*-GxMOAT(q&ERr4oOa8Eh6Xl0WGxGglr*`797H$lH2Zf&f__@ zCufJ++y_pUoI@@j<+CJ*KFeWUepMU6atE7&WN((he;z>!6N%M7WVdg<8Q#`~PGSLz zh)1Q!2nGYw4>Rf~O96t#$#e=dr)nU0#()gB(bOMl2kGHaJKK6UXsfe2w7{IO2Doo0 z%k+5o1_qxB4@t=b4q34R?LYd5aSTP8Ouz?K=FRn-j5z?31;?q<}h6`0?YU|wm|%_P)=^(;Oa)e z>9>#F^Q*^2uj^K&rB@xd|LVQgsN6uz7ak&9F5*Z+_Z_0R*SsgW z>V-(dyo;;KSyjntG3)Z4Ni7DocUj#%{`C9(7oYuef5neFoecl!?lFtIOVm{c_@$ z*3jM3cC?i9f|K?W5k(V*8w(g0QWjY?i;3>bNQD=NBJAViAckqQOyx_N$W)_O_cx{k z;Ieki*cDELAHf3KK_bWrf%-oLWNHP^qcL{9!1$2Qf*;LrdrMo*rUz+lE5=YNB}yhe zG?7plm#As(zX%8N|%BJnl+TjA%Y IpU|)W09TVRAOHXW delta 778 zcmZ3gyHwa9z?+#xgn@y9gMoFbbtq@qy{bTV1_mD><^_r-B_?O57v-1crRZf9Gy1S^ z{xMmES!1G^7FVIIk~J@v0uW5@VKm`^a3KN{lWjPGe5mYUMm$bE5g>u0)ZF~CRM))p%)C@2;o{W9qT~$Myc8s{tkU9=V6eFLWN+s5$@lrh z)siyv(jj7cIjMQ+B^jDf4In4N4VkRKFTtm-rj8^zIgMY{$3O?61|$b@CrB&AoeH)J z>G}ETIjImU6too(ZUAzCCc-tbi6imPU0;Fg#ySv5fc3Eh-o#*&me%}0NlQwY{(-v`7d)W*meUJ zabZNrp?DcfpxY5)Ii5hDe1cI%85Z;)7uUj~tthpmv?vdj!Ztq>wrAo)P27`>#e5jq OCpU;GuyO)ZIRgMytjB}^ diff --git a/scripts/PackXPI.ps1 b/scripts/PackXPI.ps1 index d35436a..da2a7a5 100644 --- a/scripts/PackXPI.ps1 +++ b/scripts/PackXPI.ps1 @@ -3,4 +3,4 @@ $compress = @{ CompressionLevel = "NoCompression" DestinationPath = "output\SearchSwitch.zip" } -Compress-Archive @compress \ No newline at end of file +Compress-Archive @compress -Force \ No newline at end of file diff --git a/v2/background.js b/v2/background.js index 6c29bd4..55c6296 100644 --- a/v2/background.js +++ b/v2/background.js @@ -1,54 +1,113 @@ +const bingSearch = "https://www.bing.com/search?" +const bingImageSearch = "https://www.bing.com/images/search?" +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 bingSearch = "https://www.bing.com/search?"; - const googleSearch = "https://www.google.com/search?"; + const url = tab.url - const bingImageSearch = "https://www.bing.com/images/search?" - const googleImageSearchSubstring = "&tbm=isch"; - 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) => {