// web 工具共享底层:HTTP 头、URL/HTML 处理、长文落盘
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
import { SANDBOX_ROOT } from '../../sandbox.js'
export const WEB_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.7',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
export function webJson(payload) {
return JSON.stringify(payload)
}
export function normalizeWebUrl(raw) {
const value = String(raw || '').trim()
if (!value) return ''
if (/^https?:\/\//i.test(value)) return value
return `https://${value}`
}
export function decodeHtmlEntities(value = '') {
return String(value)
.replace(/(\d+);/g, (_, code) => String.fromCharCode(Number(code)))
.replace(/([0-9a-f]+);/gi, (_, code) => String.fromCharCode(parseInt(code, 16)))
.replace(/ /g, ' ')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
}
export function htmlToText(html = '') {
return decodeHtmlEntities(html)
.replace(/