Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions .github/workflows/scripts/verify-search-engine-configs.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { JSDOM } from 'jsdom'
import fetch, { Headers } from 'node-fetch'
import nodeFetch from 'node-fetch'

const REQUEST_TIMEOUT_MS = 30_000

const config = {
google: {
Expand Down Expand Up @@ -42,6 +44,7 @@ const config = {
sidebarContainerQuery: ['#content_right'],
appendContainerQuery: ['#container'],
resultsContainerQuery: ['#content_left', '#results'],
requireSameOrigin: true,
},
kagi: {
inputQuery: ["input[name='q']", "textarea[name='q']"],
Expand Down Expand Up @@ -117,11 +120,11 @@ const commonHeaders = {
'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7', // for baidu
}

const desktopHeaders = new Headers({
const desktopHeaders = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/108.0.1462.76',
...commonHeaders,
})
}

const mobileHeaders = {
'User-Agent':
Expand All @@ -140,16 +143,33 @@ const mobileQueryNames = ['inputQuery', 'resultsContainerQuery']

let errors = ''

function fetchSearchPage(siteName, url, options) {
// Baidu resets node-fetch requests, while Naver rejects global fetch requests.
const fetcher = siteName === 'baidu' ? globalThis.fetch : nodeFetch
Comment thread
PeterDaveHello marked this conversation as resolved.
return fetcher(url, options)
}

async function verify(errorTag, urls, headers, queryNames) {
await Promise.all(
Object.entries(urls).map(([siteName, urlArray]) =>
Promise.all(
urlArray.map((url) =>
fetch(url, {
fetchSearchPage(siteName, url, {
method: 'GET',
headers: headers,
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
})
.then((response) => response.text())
.then((response) => {
const requestedOrigin = new URL(url).origin
const responseOrigin = new URL(response.url).origin
if (config[siteName].requireSameOrigin && responseOrigin !== requestedOrigin) {
throw new Error(`Unexpected redirect from ${requestedOrigin} to ${responseOrigin}`)
}
if (response.status !== 200) {
throw new Error(`HTTP ${response.status} ${response.statusText}`)
}
return response.text()
})
.then((text) => {
const dom = new JSDOM(text)
for (const queryName of queryNames) {
Expand All @@ -173,7 +193,9 @@ async function verify(errorTag, urls, headers, queryNames) {
}
})
.catch((error) => {
errors += errorTag + error + '\n'
const errorCode = error.code ?? error.cause?.code
const errorCodeSuffix = errorCode ? ` (${errorCode})` : ''
errors += `${errorTag}${siteName} ${url}: ${error}${errorCodeSuffix}\n`
}),
),
),
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/verify-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
jobs:
verify_configs:
runs-on: ubuntu-22.04
timeout-minutes: 5

steps:
- uses: actions/checkout@v7
Expand Down