@@ -17,10 +17,20 @@ const SEARCH_ENGINE_HOSTS = [
1717 'www.yandex.com'
1818] ;
1919
20- function isSupportedSearchEngine ( hostname ) {
21- return SEARCH_ENGINE_HOSTS . some ( domain =>
22- hostname === domain
23- ) ;
20+ function isSupportedSearchEngine ( url ) {
21+ // Check if the hostname matches
22+ if ( ! SEARCH_ENGINE_HOSTS . some ( domain => url . hostname === domain ) ) {
23+ return false ;
24+ }
25+
26+ // Check if this is actually a search path
27+ const searchPaths = [ '/search' , '/web' ] ;
28+ // Root path needs special handling - only redirect if it has a 'q' parameter
29+ if ( url . pathname === '/' || url . pathname === '' ) {
30+ return url . searchParams . has ( 'q' ) ;
31+ }
32+
33+ return searchPaths . some ( path => url . pathname === path || url . pathname === path + '/' ) ;
2434}
2535
2636// Function to check if redirect is enabled
@@ -50,32 +60,76 @@ browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
5060
5161 if ( details . url ) {
5262 console . log ( "Braver Search: URL detected" , details . url ) ;
53- const url = new URL ( details . url ) ;
5463
55- // Check if this is a supported search engine domain
56- if ( ! isSupportedSearchEngine ( url . hostname ) ) {
57- console . log ( "Braver Search: Not a supported search engine" , url . hostname ) ;
58- return ;
59- }
60-
61- const searchQuery = url . searchParams . get ( 'q' ) ;
62- console . log ( "Braver Search: Search query found" , {
63- url : url . toString ( ) ,
64- hostname : url . hostname ,
65- pathname : url . pathname ,
66- searchQuery
67- } ) ;
68-
69- if ( searchQuery ) {
70- const searchUrl = "https://search.brave.com/search?q=" ;
71- const redirectUrl = searchUrl + encodeURIComponent ( searchQuery ) ;
72- console . log ( "Braver Search: Attempting redirect to" , redirectUrl ) ;
64+ try {
65+ const url = new URL ( details . url ) ;
66+
67+ // Skip URLs that are too complex or likely not search queries
68+ if ( details . url . includes ( '%2F%2F' ) || url . pathname . length > 30 ) {
69+ console . log ( "Braver Search: Skipping complex URL" , details . url ) ;
70+ return ;
71+ }
72+
73+ // Check if this is a supported search engine domain and path
74+ if ( ! isSupportedSearchEngine ( url ) ) {
75+ console . log ( "Braver Search: Not a supported search engine or path" , {
76+ hostname : url . hostname ,
77+ pathname : url . pathname
78+ } ) ;
79+ return ;
80+ }
81+
82+ const searchQuery = url . searchParams . get ( 'q' ) ;
83+ console . log ( "Braver Search: Search query found" , {
84+ url : url . toString ( ) ,
85+ hostname : url . hostname ,
86+ pathname : url . pathname ,
87+ searchQuery
88+ } ) ;
7389
74- browser . tabs . update ( details . tabId , { url : redirectUrl } )
75- . then ( ( ) => console . log ( "Braver Search: Redirect successful" ) )
76- . catch ( error => console . error ( "Braver Search: Redirect failed" , error ) ) ;
77- } else {
78- console . log ( "Braver Search: No search query found in URL" ) ;
90+ if ( searchQuery ) {
91+ // More sophisticated check to distinguish URLs from legitimate searches
92+ const isLikelyURL = ( query ) => {
93+ // If it's very long, likely a complex URL
94+ if ( query . length > 100 ) return true ;
95+
96+ // Check for encoded URL components that indicate a complex URL
97+ if ( query . includes ( '%2F%2F' ) ) return true ;
98+
99+ // Check for tracking or redirect URLs
100+ if ( query . includes ( 'awstrack.me' ) ) return true ;
101+ if ( query . includes ( 'tracking=' ) || query . includes ( 'redirect=' ) ) return true ;
102+
103+ // Look for URL-like patterns, but be more lenient with searches
104+ const urlPattern = / ^ h t t p s ? : \/ \/ [ \w \. - ] + \. [ a - z ] { 2 , } ( \/ [ \w \. - ] * ) * $ / i;
105+ if ( urlPattern . test ( query ) ) return true ;
106+
107+ // Check for complex URL structures (multiple paths and query params)
108+ const hasMultiplePaths = ( query . match ( / \/ / g) || [ ] ) . length > 2 ;
109+ const hasMultipleQueryParams = ( query . match ( / [ ? & ] [ ^ ? & ] + = [ ^ ? & ] + / g) || [ ] ) . length > 1 ;
110+ if ( hasMultiplePaths && hasMultipleQueryParams ) return true ;
111+
112+ // Don't block queries that just happen to contain domains or technical terms
113+ return false ;
114+ } ;
115+
116+ if ( isLikelyURL ( searchQuery ) ) {
117+ console . log ( "Braver Search: Query looks like a URL, skipping" , searchQuery ) ;
118+ return ;
119+ }
120+
121+ const searchUrl = "https://search.brave.com/search?q=" ;
122+ const redirectUrl = searchUrl + encodeURIComponent ( searchQuery ) ;
123+ console . log ( "Braver Search: Attempting redirect to" , redirectUrl ) ;
124+
125+ browser . tabs . update ( details . tabId , { url : redirectUrl } )
126+ . then ( ( ) => console . log ( "Braver Search: Redirect successful" ) )
127+ . catch ( error => console . error ( "Braver Search: Redirect failed" , error ) ) ;
128+ } else {
129+ console . log ( "Braver Search: No search query found in URL" ) ;
130+ }
131+ } catch ( error ) {
132+ console . error ( "Braver Search: Error processing URL" , error ) ;
79133 }
80134 } else {
81135 console . log ( "Braver Search: No URL in navigation details" , details ) ;
0 commit comments