mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 06:49:02 +08:00
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:
parent
72f4584b0f
commit
c01642ef22
@ -1,6 +1,9 @@
|
|||||||
import { is } from '@electron-toolkit/utils'
|
import { is } from '@electron-toolkit/utils'
|
||||||
|
import { loggerService } from '@logger'
|
||||||
import { BrowserWindow } from 'electron'
|
import { BrowserWindow } from 'electron'
|
||||||
|
|
||||||
|
const logger = loggerService.withContext('SearchService')
|
||||||
|
|
||||||
export class SearchService {
|
export class SearchService {
|
||||||
private static instance: SearchService | null = null
|
private static instance: SearchService | null = null
|
||||||
private searchWindows: Record<string, BrowserWindow> = {}
|
private searchWindows: Record<string, BrowserWindow> = {}
|
||||||
@ -55,6 +58,7 @@ export class SearchService {
|
|||||||
|
|
||||||
public async openUrlInSearchWindow(uid: string, url: string): Promise<any> {
|
public async openUrlInSearchWindow(uid: string, url: string): Promise<any> {
|
||||||
let window = this.searchWindows[uid]
|
let window = this.searchWindows[uid]
|
||||||
|
logger.debug(`Searching with URL: ${url}`)
|
||||||
if (window) {
|
if (window) {
|
||||||
await window.loadURL(url)
|
await window.loadURL(url)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -17,9 +17,10 @@ export default class LocalBingProvider extends LocalSearchProvider {
|
|||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
const node = item.querySelector('a')
|
const node = item.querySelector('a')
|
||||||
if (node) {
|
if (node) {
|
||||||
|
const decodedUrl = this.decodeBingUrl(node.href)
|
||||||
results.push({
|
results.push({
|
||||||
title: node.textContent || '',
|
title: node.textContent || '',
|
||||||
url: node.href
|
url: decodedUrl
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -28,4 +29,34 @@ export default class LocalBingProvider extends LocalSearchProvider {
|
|||||||
}
|
}
|
||||||
return results
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user