(query, minRatio, minSimilarity)
| 494 | } |
| 495 | |
| 496 | static async performSearch(query, minRatio, minSimilarity) { |
| 497 | const isSemanticSearch = document |
| 498 | .getElementById("semanticToggle") |
| 499 | .classList.contains("active"); |
| 500 | if (!isSemanticSearch) { |
| 501 | if (window.subtitleDB && window.subtitleDB.isLoaded) { |
| 502 | try { |
| 503 | const localResults = await window.subtitleDB.search( |
| 504 | query, |
| 505 | minRatio, |
| 506 | minSimilarity, |
| 507 | ); |
| 508 | |
| 509 | if (localResults && Array.isArray(localResults)) { |
| 510 | return { |
| 511 | status: "success", |
| 512 | data: localResults, |
| 513 | count: localResults.length, |
| 514 | }; |
| 515 | } else if ( |
| 516 | localResults && |
| 517 | localResults.status === "success" && |
| 518 | Array.isArray(localResults.data) |
| 519 | ) { |
| 520 | return localResults; |
| 521 | } |
| 522 | } catch (error) { |
| 523 | console.log("本地搜索失败,使用vvapi", error); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | const vvapiUrl = `${CONFIG.apiBaseUrl}/search?query=${encodeURIComponent(query)}&min_ratio=${minRatio}&min_similarity=${minSimilarity}`; |
| 528 | |
| 529 | try { |
| 530 | console.log("使用普通搜索:", vvapiUrl); |
| 531 | const response = await fetch(vvapiUrl); |
| 532 | if (!response.ok) |
| 533 | throw new Error( |
| 534 | `API 请求失败: ${response.status} ${response.statusText}`, |
| 535 | ); |
| 536 | |
| 537 | const text = await response.text(); |
| 538 | const lines = text.trim().split("\n"); |
| 539 | const results = []; |
| 540 | |
| 541 | for (const line of lines) { |
| 542 | try { |
| 543 | if (line.trim()) { |
| 544 | const item = JSON.parse(line); |
| 545 | results.push(item); |
| 546 | } |
| 547 | } catch (e) {} |
| 548 | } |
| 549 | |
| 550 | return { |
| 551 | status: "success", |
| 552 | data: results, |
| 553 | count: results.length, |
no test coverage detected