130 lines
3.2 KiB
JavaScript
130 lines
3.2 KiB
JavaScript
/*
|
|
* PingMe CK 抓取 - Quantumult X Rewrite
|
|
*
|
|
* 订阅地址:
|
|
* https://gitea.chickliu.fun/Hermes/qx/raw/branch/main/conf/pingme.conf
|
|
*
|
|
* 功能:
|
|
* - 监听 api.pingmeapp.net/app/queryBalanceAndBonus、checkIn、videoBonus
|
|
* - 抓取 pingme_multi.js 需要的 url / paramsRaw / headers
|
|
* - 弹窗输出可直接追加到 NiuPanel /app/data/scripts/pingme_ck.json 的 JSON 对象
|
|
*
|
|
* 安全:不上传 CK,不请求第三方;仅在 QX 本地保存最近一次抓取结果。
|
|
*/
|
|
|
|
const SCRIPT_NAME = 'PingMe'
|
|
|
|
function notify(subtitle, message) {
|
|
$notify(SCRIPT_NAME, subtitle || '', message || '')
|
|
}
|
|
|
|
function done() {
|
|
$done({})
|
|
}
|
|
|
|
function setPref(key, value) {
|
|
try { return $prefs.setValueForKey(String(value), key) } catch (e) { return false }
|
|
}
|
|
|
|
function getHeader(headers, name) {
|
|
const lower = String(name).toLowerCase()
|
|
for (const k in (headers || {})) {
|
|
if (String(k).toLowerCase() === lower) return headers[k]
|
|
}
|
|
return ''
|
|
}
|
|
|
|
function parseRawQuery(url) {
|
|
const raw = String(url || '')
|
|
const qIndex = raw.indexOf('?')
|
|
if (qIndex < 0) return {}
|
|
const hashIndex = raw.indexOf('#', qIndex + 1)
|
|
const query = raw.slice(qIndex + 1, hashIndex >= 0 ? hashIndex : undefined)
|
|
const out = {}
|
|
query.split('&').forEach(pair => {
|
|
if (!pair) return
|
|
const idx = pair.indexOf('=')
|
|
if (idx < 0) return
|
|
const k = pair.slice(0, idx)
|
|
const v = pair.slice(idx + 1)
|
|
if (k) out[k] = v
|
|
})
|
|
return out
|
|
}
|
|
|
|
function pickHeaders(headers) {
|
|
const names = [
|
|
'User-Agent',
|
|
'Accept',
|
|
'Accept-Language',
|
|
'Authorization',
|
|
'Cookie',
|
|
'X-Requested-With',
|
|
'Content-Type',
|
|
'Origin',
|
|
'Referer'
|
|
]
|
|
const out = {}
|
|
names.forEach(name => {
|
|
const v = getHeader(headers, name)
|
|
if (v !== undefined && v !== null && String(v) !== '') out[name] = String(v)
|
|
})
|
|
|
|
// 保留 PingMe/客户端自定义头,避免后续接口校验客户端参数。
|
|
Object.keys(headers || {}).forEach(k => {
|
|
const lk = String(k).toLowerCase()
|
|
if (
|
|
lk.startsWith('x-') ||
|
|
lk.indexOf('device') >= 0 ||
|
|
lk.indexOf('token') >= 0 ||
|
|
lk.indexOf('client') >= 0 ||
|
|
lk.indexOf('app') >= 0
|
|
) {
|
|
if (!/^content-length$/i.test(k) && !/^host$/i.test(k)) out[k] = String(headers[k])
|
|
}
|
|
})
|
|
return out
|
|
}
|
|
|
|
function buildRecord(req) {
|
|
const url = String(req.url || '')
|
|
const paramsRaw = parseRawQuery(url)
|
|
const headers = pickHeaders(req.headers || {})
|
|
return { url, paramsRaw, headers }
|
|
}
|
|
|
|
function hasRequiredParams(record) {
|
|
const p = record && record.paramsRaw || {}
|
|
return Object.keys(p).length > 0
|
|
}
|
|
|
|
function formatRecord(record) {
|
|
return JSON.stringify(record)
|
|
}
|
|
|
|
function main() {
|
|
const req = typeof $request !== 'undefined' ? $request : null
|
|
if (!req || !req.url) {
|
|
notify('抓取失败', '没有 request 上下文')
|
|
return done()
|
|
}
|
|
|
|
const record = buildRecord(req)
|
|
if (!hasRequiredParams(record)) {
|
|
notify('抓取失败', '当前请求缺少 URL 查询参数,请打开 PingMe 触发余额/签到/视频接口')
|
|
return done()
|
|
}
|
|
|
|
const text = formatRecord(record)
|
|
setPref('pingme_ck_last', text)
|
|
notify('CK 获取成功', text)
|
|
done()
|
|
}
|
|
|
|
try {
|
|
main()
|
|
} catch (e) {
|
|
notify('脚本异常', String(e && e.message || e).slice(0, 220))
|
|
done()
|
|
}
|