fix: decode Bing redirect URLs in LocalBingProvider (#9593)

Added logic to extract and decode the actual target URL from Bing redirect links in LocalBingProvider. Also introduced debug logging in SearchService to log search URLs.
This commit is contained in:
Pleasure1234 2025-08-27 21:34:01 +08:00 committed by GitHub
parent 72f4584b0f
commit c01642ef22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -1,6 +1,9 @@
import { is } from '@electron-toolkit/utils'
import { loggerService } from '@logger'
import { BrowserWindow } from 'electron'
const logger = loggerService.withContext('SearchService')
export class SearchService {
private static instance: SearchService | null = null
private searchWindows: Record<string, BrowserWindow> = {}
@ -55,6 +58,7 @@ export class SearchService {
public async openUrlInSearchWindow(uid: string, url: string): Promise<any> {
let window = this.searchWindows[uid]
logger.debug(`Searching with URL: ${url}`)
if (window) {
await window.loadURL(url)
} else {

View File

@ -17,9 +17,10 @@ export default class LocalBingProvider extends LocalSearchProvider {
items.forEach((item) => {
const node = item.querySelector('a')
if (node) {
const decodedUrl = this.decodeBingUrl(node.href)
results.push({
title: node.textContent || '',
url: node.href
url: decodedUrl
})
}
})
@ -28,4 +29,34 @@ export default class LocalBingProvider extends LocalSearchProvider {
}
return results
}
/**
* Decode Bing redirect URL to get the actual URL
* Bing URLs are in format: https://www.bing.com/ck/a?...&u=a1aHR0cHM6Ly93d3cudG91dGlhby5jb20...
* The 'u' parameter contains Base64 encoded URL with 'a1' prefix
*/
private decodeBingUrl(bingUrl: string): string {
try {
const url = new URL(bingUrl)
const encodedUrl = url.searchParams.get('u')
if (!encodedUrl) {
return bingUrl // Return original if no 'u' parameter
}
// Remove the 'a1' prefix and decode Base64
const base64Part = encodedUrl.substring(2)
const decodedUrl = atob(base64Part)
// Validate the decoded URL
if (decodedUrl.startsWith('http')) {
return decodedUrl
}
return bingUrl // Return original if decoded URL is invalid
} catch (error) {
logger.warn('Failed to decode Bing URL:', error as Error)
return bingUrl // Return original URL if decoding fails
}
}
}