Update v3 extension to use code rewrite

This commit is contained in:
Isaac Shoebottom 2022-10-07 01:17:45 -03:00
parent 86eb6022c7
commit 454fbd4b15
6 changed files with 111 additions and 38 deletions

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="406" name="JavaScript" />
</Languages>
</inspection_tool>
</profile>
</component>

View File

@ -1,53 +1,116 @@
function switchSearch(tab) { function switchSearch(tab) {
const bingSearch = "https://www.bing.com/search?q="; const bingSearch = "https://www.bing.com/search?"
const googleSearch = "https://www.google.com/search?q="; 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 bingImageSearch = "https://www.bing.com/images/search?q=" const googleSearch = "https://www.google.com/search?"
const googleImageSearchSubstring = "&tbm=isch"; const googleImageSearchSubstring = "&tbm=isch"
const url = tab.url; 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 url = tab.url
let newURL;
let removeEngine;
let searchEnd;
let justSearch;
//Handle bing searches //Handle bing searches
if (url.substring(0, bingSearch.length) === bingSearch) { if (url.substring(0, bingSearch.length) === bingSearch) {
removeEngine = url.slice(bingSearch.length); return getSwitchedSearch(bingSearch, googleSearch, url)
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 //Handle bing image searches
else if (url.substring(0, bingImageSearch.length) === bingImageSearch) { else if (url.substring(0, bingImageSearch.length) === bingImageSearch) {
removeEngine = url.slice(bingImageSearch.length) return getSwitchedGoogleSearch(bingImageSearch, googleSearch, googleImageSearchSubstring, url)
searchEnd = removeEngine.indexOf('&'); }
justSearch = removeEngine.substring(0, searchEnd); //Handle bing video searches
else if (url.substring(0, bingVideoSearch.length) === bingVideoSearch){
newURL = googleSearch + justSearch + googleImageSearchSubstring + '&'; 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)
}
const debug = true
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.action.onClicked.addListener((tab) => { chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({ chrome.scripting.executeScript({
target: {tabId: tab.id}, target: {tabId: tab.id},

BIN
v3/icon128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
v3/icon16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 639 B

BIN
v3/icon48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -1,6 +1,6 @@
{ {
"name": "SearchSwitcher", "name": "SearchSwitcher",
"version": "0.0.2", "version": "0.0.3",
"description": "Search switcher", "description": "Search switcher",
"manifest_version": 3, "manifest_version": 3,
"author": "Isaac Shoebottom", "author": "Isaac Shoebottom",