dsh-qianchuan-toolkit 1.0.21 → 1.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +109 -0
- package/README.md +6 -4
- package/assets/live-control/scripts/account-lib.mjs +35 -11
- package/assets/live-control/scripts/cdp-lib.mjs +11 -4
- package/assets/live-control/scripts/control.mjs +14 -3
- package/assets/live-control/scripts/expr/cancel-modal-sq.js +11 -0
- package/assets/live-control/scripts/expr/close-dialog-sq.js +12 -0
- package/assets/live-control/scripts/expr/dialog-open-sq.js +7 -0
- package/assets/live-control/scripts/expr/dismiss-overlays-sq.js +37 -0
- package/assets/live-control/scripts/expr/fill-sq.js +68 -0
- package/assets/live-control/scripts/expr/log-rows.js +10 -0
- package/assets/live-control/scripts/expr/log-tab-click.js +9 -0
- package/assets/live-control/scripts/expr/manifest.json +144 -0
- package/assets/live-control/scripts/expr/open-plus-sq.js +40 -0
- package/assets/live-control/scripts/expr/page-state-sq.js +15 -0
- package/assets/live-control/scripts/expr/pause-info-sq.js +57 -0
- package/assets/live-control/scripts/expr/pick-sq.js +51 -0
- package/assets/live-control/scripts/expr/plus-rect-sq.js +24 -0
- package/assets/live-control/scripts/expr/result-sq.js +21 -0
- package/assets/live-control/scripts/expr/row-mode-sq.js +23 -0
- package/assets/live-control/scripts/expr/row-state-sq.js +72 -0
- package/assets/live-control/scripts/expr/submit-dom-sq.js +16 -0
- package/assets/live-control/scripts/expr/submit-rect-sq.js +15 -0
- package/assets/live-control/scripts/expr-util.mjs +18 -1
- package/assets/live-control/scripts/hourly.mjs +70 -18
- package/assets/material-align/SKILL.md +88 -0
- package/assets/material-align/references/01-/345/214/271/351/205/215/350/247/204/345/210/231/344/270/216/345/255/227/346/256/265/345/217/243/345/276/204.md +60 -0
- package/assets/material-align/scripts/align.mjs +284 -0
- package/assets/material-align/scripts/db.mjs +154 -0
- package/assets/material-align/scripts/xlsx.mjs +304 -0
- package/assets/plan-copy/SKILL.md +24 -4
- package/assets/plan-copy/references/01-/345/256/214/346/225/264/346/223/215/344/275/234/346/211/213/345/206/214.md +3 -3
- package/assets/plan-copy/references/04-/346/265/217/350/247/210/345/231/250/344/274/232/350/257/235.md +3 -3
- package/assets/plan-copy/references/05-/345/274/271/347/252/227/346/270/205/345/215/225.md +58 -0
- package/assets/plan-copy/scripts/capture.mjs +67 -1
- package/assets/plan-copy/scripts/cdp.mjs +3 -1
- package/assets/plan-copy/scripts/copy.mjs +130 -20
- package/assets/plan-copy/scripts/newmat.mjs +399 -0
- package/assets/plan-copy/scripts/pick-all.mjs +110 -4
- package/assets/plan-copy/scripts/readmat.mjs +2 -0
- package/assets/plan-copy/scripts/start-qc-copy.cmd +49 -0
- package/lib/index.js +6 -0
- package/package.json +5 -3
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
// newmat.mjs —— 达人新发布素材补投
|
|
2
|
+
// 用法:
|
|
3
|
+
// node newmat.mjs scan <aavid> [--days=5] [--max=999] [--plan=<计划ID>]
|
|
4
|
+
// node newmat.mjs apply <aavid> [--days=5] [--max=999] [--plan=<计划ID>] [--force]
|
|
5
|
+
// 只勾「未勾选 且 发布时间在近 N 天」,超期未勾选不补(用户 2026-11-30 定)。
|
|
6
|
+
// 记录: QC_WORKDIR/newmat/records-<aavid>.csv 与 run-*.log
|
|
7
|
+
process.env.QC_CDP_PORT = process.env.QC_CDP_PORT || '9223'
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { connect, listPages } from '../../live-control/scripts/cdp-lib.mjs'
|
|
11
|
+
|
|
12
|
+
const argv = process.argv.slice(2)
|
|
13
|
+
const mode = argv[0]
|
|
14
|
+
const aavid = argv[1]
|
|
15
|
+
const argOf = (k, d) => { const x = argv.find((a) => a.startsWith('--' + k + '=')); return x ? x.split('=')[1] : d }
|
|
16
|
+
const DAYS = Number(argOf('days', '5'))
|
|
17
|
+
const MAX = Number(argOf('max', '999'))
|
|
18
|
+
const ONE = argOf('plan', '')
|
|
19
|
+
const FORCE = argv.includes('--force')
|
|
20
|
+
const W = process.env.QC_WORKDIR || process.cwd()
|
|
21
|
+
const OUT = path.join(W, 'newmat')
|
|
22
|
+
fs.mkdirSync(OUT, { recursive: true })
|
|
23
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
24
|
+
const T0 = Date.now()
|
|
25
|
+
const LOGF = path.join(OUT, 'run-' + new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19) + '.log')
|
|
26
|
+
function logLine(s) { const line = '[' + new Date().toISOString().slice(11, 19) + '] ' + s; console.log(line); try { fs.appendFileSync(LOGF, line + '\n') } catch (e) {} }
|
|
27
|
+
const CSVF = path.join(OUT, 'records-' + aavid + '.csv')
|
|
28
|
+
if (!fs.existsSync(CSVF)) fs.writeFileSync(CSVF, '\uFEFF' + 'time,planId,planName,aweme,title,publishTime,mode' + String.fromCharCode(13) + String.fromCharCode(10))
|
|
29
|
+
const csv = (o) => fs.appendFileSync(CSVF, [new Date().toISOString(), o.planId, o.planName, o.aweme, o.title, o.publishTime, mode].map((x) => '"' + String(x || '').split('"').join('""') + '"').join(',') + String.fromCharCode(13) + String.fromCharCode(10))
|
|
30
|
+
|
|
31
|
+
const LIST_URL = 'https://qianchuan.jinritemai.com/overall-prom?aavid=' + aavid + '&campaignType=1&uniTab=ad&marGoal=1&uniVideoTab=1&overallPromViewTab=plan'
|
|
32
|
+
const CARD = '.main-vmok-plugin-create-material-list-card-item'
|
|
33
|
+
const NAV = '.main-vmok-plugin-tabs__nav-list'
|
|
34
|
+
|
|
35
|
+
const pages = await listPages()
|
|
36
|
+
const cands = pages.filter((p) => p.type === 'page' && (p.url || '').indexOf('jinritemai') >= 0)
|
|
37
|
+
const tab = cands.find((p) => (p.url || '').indexOf(aavid) >= 0 && (p.url || '').indexOf('overall-prom') >= 0) || cands.find((p) => (p.url || '').indexOf('overall-prom') >= 0) || cands[0]
|
|
38
|
+
if (!tab) { logLine('ERR 没有千川标签页'); process.exit(2) }
|
|
39
|
+
const page = await connect('jinritemai', { id: tab.id })
|
|
40
|
+
logLine('TAB ' + String(tab.url).slice(0, 90))
|
|
41
|
+
|
|
42
|
+
async function waitJs(expr, ms) { const t0 = Date.now(); while (Date.now() - t0 < ms) { try { if (await page.eval(expr, 20000)) return true } catch (e) {} await sleep(400) } return false }
|
|
43
|
+
async function clickExact(txt) { return await page.eval("(() => { const t = " + JSON.stringify(txt) + "; const ls = [...document.querySelectorAll('span,div,button,a,li')].filter(e => e.children.length === 0 && (e.innerText||'').trim() === t && e.getBoundingClientRect().width > 0); if (!ls.length) return 'none'; ls[ls.length-1].click(); return 'ok' })()") }
|
|
44
|
+
async function mouse(x, y) {
|
|
45
|
+
await page.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y })
|
|
46
|
+
await page.send('Input.dispatchMouseEvent', { type: 'mousePressed', x, y, button: 'left', clickCount: 1 })
|
|
47
|
+
await page.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x, y, button: 'left', clickCount: 1 })
|
|
48
|
+
}
|
|
49
|
+
const key = async (k, vk) => { await page.send('Input.dispatchKeyEvent', { type: 'keyDown', key: k, code: k, windowsVirtualKeyCode: vk, nativeVirtualKeyCode: vk }); await page.send('Input.dispatchKeyEvent', { type: 'keyUp', key: k, code: k, windowsVirtualKeyCode: vk, nativeVirtualKeyCode: vk }) }
|
|
50
|
+
|
|
51
|
+
async function openList() {
|
|
52
|
+
await page.send('Page.navigate', { url: LIST_URL }, 25000)
|
|
53
|
+
await sleep(1200)
|
|
54
|
+
await waitJs("document.body.innerText.indexOf('计划视图') >= 0 || document.body.innerText.indexOf('商品自选') >= 0 || document.body.innerText.indexOf('全店托管') >= 0", 20000)
|
|
55
|
+
await clickExact('计划视图'); await sleep(1200)
|
|
56
|
+
await clickExact('商品自选')
|
|
57
|
+
const ok = await waitJs("!![...document.querySelectorAll('input')].find(x => /回车搜索|计划名称|计划ID|计划 ID/.test(x.placeholder || ''))", 20000)
|
|
58
|
+
logLine('LIST-READY ' + ok)
|
|
59
|
+
await sleep(600)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const STATUSBOX = "(() => { const T = (s) => (s || '').replace(/[\\s]+/g, ' ').trim(); const ls = [...document.querySelectorAll('*')].filter((x) => { if (x.closest('tbody')) return false; const t = T(x.innerText); return t === '请筛选计划状态' || t === '投放中' }); if (!ls.length) return null; const leaf = ls[ls.length - 1]; const txt = T(leaf.innerText); let n = leaf; for (let i = 0; i < 5 && n; i++) { const r = n.getBoundingClientRect(); if (r.width > 120 && r.height > 18) return { x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2), text: txt }; n = n.parentElement } const r2 = leaf.getBoundingClientRect(); return { x: Math.round(r2.x + r2.width / 2), y: Math.round(r2.y + r2.height / 2), text: txt } })()"
|
|
63
|
+
async function filterRunning() {
|
|
64
|
+
for (let attempt = 1; attempt <= 2; attempt++) {
|
|
65
|
+
const box = await page.eval(STATUSBOX, 20000)
|
|
66
|
+
if (!box) throw new Error('找不到计划状态筛选框')
|
|
67
|
+
logLine('FILTER-CHIP attempt=' + attempt + ' chip=' + box.text)
|
|
68
|
+
if (box.text === '投放中') return
|
|
69
|
+
await mouse(box.x, box.y)
|
|
70
|
+
await sleep(1100)
|
|
71
|
+
await key('ArrowDown', 40); await sleep(300)
|
|
72
|
+
await key('Enter', 13)
|
|
73
|
+
let box2 = null
|
|
74
|
+
for (let i = 0; i < 10; i++) { await sleep(300); box2 = await page.eval(STATUSBOX, 20000); if (box2 && box2.text === '投放中') break }
|
|
75
|
+
logLine('FILTER-AFTER attempt=' + attempt + ' chip=' + (box2 ? box2.text : 'nochip'))
|
|
76
|
+
if (box2 && box2.text === '投放中') return
|
|
77
|
+
await key('Escape', 27); await sleep(700)
|
|
78
|
+
}
|
|
79
|
+
throw new Error('状态筛选没切到投放中(2 次尝试失败)')
|
|
80
|
+
}
|
|
81
|
+
const ROWS = "(() => { const T = (s) => (s || '').replace(/[\\s\\u00a0]+/g, ' ').trim(); const out = []; for (const tr of document.querySelectorAll('tbody tr')) { const t = T(tr.innerText); if (t.indexOf('ID') < 0) continue; const m = t.match(/ID[::]\\s*(\\d{10,})/); if (!m) continue; const raw = t.split(' ID')[0]; const name = raw.replace(/^[[【]\s*(?:扶持中|已暂停|投放中|已删除|审核中|删除中|待审核)\s*[\]】]\s*/, ''); out.push({ id: m[1], name: name.slice(0, 60), run: t.indexOf('投放中') >= 0 }) } return JSON.stringify(out) })()"
|
|
82
|
+
async function pageRows() { try { return JSON.parse(await page.eval(ROWS, 20000)) } catch (e) { return [] } }
|
|
83
|
+
async function enumerate() {
|
|
84
|
+
const all = []; const seen = new Set()
|
|
85
|
+
for (let w = 0; w < 25; w++) { const rr = await pageRows(); if (rr.length) break; await sleep(800) }
|
|
86
|
+
const first = await pageRows()
|
|
87
|
+
logLine('ROWS first=' + first.length + (first.length ? ' sample=' + first[0].id : ''))
|
|
88
|
+
for (let p = 0; p < 12; p++) {
|
|
89
|
+
for (const r of await pageRows()) if (!seen.has(r.id)) { seen.add(r.id); all.push(r) }
|
|
90
|
+
if (all.length >= MAX) break
|
|
91
|
+
const np = await page.eval("(() => { const b = document.querySelector('li.aurora-qc-pagination-next'); if (!b) return 'none'; if (/disabled/.test(String(b.className || ''))) return 'disabled'; b.scrollIntoView({ block: 'center' }); const r = b.getBoundingClientRect(); return JSON.stringify({ x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) }) })()", 20000)
|
|
92
|
+
if (np === 'none' || np === 'disabled') break
|
|
93
|
+
const pp = JSON.parse(np)
|
|
94
|
+
await sleep(300)
|
|
95
|
+
const p2 = await page.eval("(() => { const b = document.querySelector('li.aurora-qc-pagination-next'); if (!b) return 'none'; const r = b.getBoundingClientRect(); return JSON.stringify({ x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) }) })()")
|
|
96
|
+
const at = p2 === 'none' ? pp : JSON.parse(p2)
|
|
97
|
+
const sigBefore = JSON.stringify(await pageRows())
|
|
98
|
+
await mouse(at.x, at.y)
|
|
99
|
+
for (let k = 0; k < 24; k++) { await sleep(250); const rr = await pageRows(); if (rr.length && JSON.stringify(rr) !== sigBefore) break }
|
|
100
|
+
}
|
|
101
|
+
return all.slice(0, MAX)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const SEARCH_INPUT = "(() => { const i = [...document.querySelectorAll('input')].find(x => /回车搜索|计划名称|计划ID/.test(x.placeholder || '')); return i ? 'ok' : 'noinput' })()"
|
|
105
|
+
const searchFill = (v) => "(() => { const i = [...document.querySelectorAll('input')].find(x => /回车搜索|计划名称|计划ID/.test(x.placeholder || '')); if (!i) return 'noinput'; const set = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; i.focus(); set.call(i, ''); i.dispatchEvent(new Event('input', { bubbles: true })); set.call(i, " + JSON.stringify(v) + "); i.dispatchEvent(new Event('input', { bubbles: true })); return 'ok' })()"
|
|
106
|
+
const uniqRow = (p) => "(() => { const P = " + JSON.stringify(p) + "; const rs = [...document.querySelectorAll('tbody tr')].filter(r => { const t = r.innerText || ''; return /ID[::]\\s*\\d{10,}/.test(t) && t.indexOf('每日预算') >= 0 && t.indexOf('素材ID') < 0 }); return rs.length === 1 && (rs[0].innerText || '').indexOf(P) >= 0 })()"
|
|
107
|
+
async function ensureList() {
|
|
108
|
+
if (await page.eval(SEARCH_INPUT, 15000) === 'ok') { logLine('LIST-OK reuse'); return true }
|
|
109
|
+
try { await openList(); return true } catch (e) { logLine('LIST-FAIL ' + String(e.message).slice(0, 80)); return false }
|
|
110
|
+
}
|
|
111
|
+
async function ensurePlanView() {
|
|
112
|
+
const ROWSD = "(() => { const rs = [...document.querySelectorAll('tbody tr')].filter(r => (r.innerText||'').indexOf('每日预算') >= 0); return rs.length > 0 })()"
|
|
113
|
+
for (let t = 0; t < 2; t++) {
|
|
114
|
+
if (await page.eval(ROWSD, 15000)) return true
|
|
115
|
+
try { await clickExact('计划视图') } catch (e) {}
|
|
116
|
+
await sleep(600)
|
|
117
|
+
try { await clickExact('商品自选') } catch (e) {}
|
|
118
|
+
for (let i = 0; i < 8; i++) { await sleep(400); if (await page.eval(ROWSD, 15000)) return true }
|
|
119
|
+
}
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
async function reloadList() {
|
|
123
|
+
logLine('RELOAD list (reload)')
|
|
124
|
+
try { await page.send('Page.reload', { ignoreCache: false }) } catch (e) {}
|
|
125
|
+
await sleep(2500)
|
|
126
|
+
for (let i = 0; i < 10; i++) { if (await page.eval(SEARCH_INPUT, 15000) === 'ok') break; await sleep(500) }
|
|
127
|
+
if (await ensurePlanView()) { logLine('RELOAD planview-ok'); return }
|
|
128
|
+
logLine('RELOAD fallback-openList')
|
|
129
|
+
await openList()
|
|
130
|
+
}
|
|
131
|
+
const INPUT_VAL = "(() => { const i = [...document.querySelectorAll('input')].find(x => /输入计划名称|回车搜索/.test(x.placeholder || '')); return i ? i.value : 'noinput' })()"
|
|
132
|
+
const INPUT_FOCUS = "(() => { const i = [...document.querySelectorAll('input')].find(x => /输入计划名称|回车搜索/.test(x.placeholder || '')); if (!i) return 'noinput'; i.focus(); return document.activeElement === i ? 'focused' : 'lost' })()"
|
|
133
|
+
async function typeSearch(planId) {
|
|
134
|
+
for (let k = 0; k < 3; k++) {
|
|
135
|
+
const rect = await page.eval("(() => { const i = [...document.querySelectorAll('input')].find(x => /输入计划名称|回车搜索/.test(x.placeholder || '')); if (!i) return 'noinput'; const r = i.getBoundingClientRect(); return JSON.stringify({ x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2) }) })()", 15000)
|
|
136
|
+
if (rect === "noinput") return "noinput"
|
|
137
|
+
const p = JSON.parse(rect)
|
|
138
|
+
const fo = await page.eval(INPUT_FOCUS, 15000)
|
|
139
|
+
if (fo === "noinput") return "noinput"
|
|
140
|
+
await mouse(p.x, p.y)
|
|
141
|
+
await sleep(180)
|
|
142
|
+
await page.eval(searchFill(""), 15000)
|
|
143
|
+
const v0 = await page.eval(INPUT_VAL, 15000)
|
|
144
|
+
if (String(v0) !== "") {
|
|
145
|
+
await page.eval(INPUT_FOCUS, 15000)
|
|
146
|
+
await page.send("Input.dispatchKeyEvent", { type: "rawKeyDown", key: "a", code: "KeyA", windowsVirtualKeyCode: 65, modifiers: 2, commands: ["selectAll"] })
|
|
147
|
+
await page.send("Input.dispatchKeyEvent", { type: "keyUp", key: "a", code: "KeyA", windowsVirtualKeyCode: 65, modifiers: 2 })
|
|
148
|
+
await key("Backspace", 8)
|
|
149
|
+
await sleep(180)
|
|
150
|
+
}
|
|
151
|
+
await page.send("Input.insertText", { text: String(planId) })
|
|
152
|
+
await sleep(320)
|
|
153
|
+
const v = await page.eval(INPUT_VAL, 15000)
|
|
154
|
+
if (String(v) === String(planId)) { await key("Enter", 13); return "ok" }
|
|
155
|
+
logLine("TYPE-RETRY k=" + k + " val=" + v + " want=" + planId)
|
|
156
|
+
}
|
|
157
|
+
return "badtype"
|
|
158
|
+
}
|
|
159
|
+
async function searchPlanId(planId) {
|
|
160
|
+
for (let att = 0; att < 2; att++) {
|
|
161
|
+
if (att > 0) await reloadList()
|
|
162
|
+
const r = await typeSearch(planId)
|
|
163
|
+
if (r === 'ok' && (await waitJs(uniqRow(planId), 3000))) return
|
|
164
|
+
logLine('SEARCH-MISS ' + planId + ' attempt=' + att + ' type=' + r)
|
|
165
|
+
}
|
|
166
|
+
throw new Error('搜索该 ID 没有出唯一 1 行: ' + planId)
|
|
167
|
+
}
|
|
168
|
+
async function openDrawer(planId) {
|
|
169
|
+
const chip = await page.eval("(() => { const P = " + JSON.stringify(planId) + "; const tr = [...document.querySelectorAll('tbody tr')].find(r => (r.innerText||'').indexOf(P) >= 0); if (!tr) return 'norow'; const leaves = [...tr.querySelectorAll('*')].filter(e => e.children.length === 0 && (e.innerText||'').trim().length > 0); const first = (tr.innerText||'').split(String.fromCharCode(10)).map(s => s.trim()).filter(Boolean)[0]; const el = leaves.find(e => e.innerText.trim() === first) || leaves.find(e => e.innerText.trim() === '素材'); if (!el) return 'nochip'; el.click(); return 'ok' })()")
|
|
170
|
+
if (chip !== 'ok') throw new Error('打不开素材入口: ' + chip)
|
|
171
|
+
if (!(await waitJs('document.body.innerText.indexOf("保障历史") >= 0', 15000))) throw new Error('详情抽屉未打开')
|
|
172
|
+
await sleep(1200)
|
|
173
|
+
const mt = await page.eval("(() => { const roots = [...document.querySelectorAll('*')].filter(e => { const t = e.innerText || ''; return t.indexOf('保障历史') >= 0 && t.indexOf('素材') >= 0 && t.length > 100 }); if (!roots.length) return 'noroot'; roots.sort((a,b) => a.innerText.length - b.innerText.length); const r = roots[0]; const lv = [...r.querySelectorAll('*')].filter(e => e.children.length === 0 && (e.innerText||'').trim() === '素材'); if (!lv.length) return 'notab'; lv[0].click(); return 'ok' })()")
|
|
174
|
+
if (mt !== 'ok') throw new Error('素材 tab: ' + mt)
|
|
175
|
+
if (!(await waitJs('document.body.innerText.indexOf("素材来源") >= 0', 15000))) throw new Error('素材 tab 未就绪')
|
|
176
|
+
await clickExact('自选素材')
|
|
177
|
+
await waitJs("!![...document.querySelectorAll('*')].find(e => e.children.length === 0 && (e.innerText||'').trim() === '视频')", 8000)
|
|
178
|
+
await clickExact('视频')
|
|
179
|
+
const addLeaf = "!![...document.querySelectorAll('span,div,button')].find(e => e.children.length === 0 && (e.innerText||'').trim() === '添加视频' && e.getBoundingClientRect().width > 0)"
|
|
180
|
+
if (!(await waitJs(addLeaf, 15000))) throw new Error('没有「添加视频」按钮')
|
|
181
|
+
await sleep(300)
|
|
182
|
+
const b = await clickExact('添加视频')
|
|
183
|
+
if (b !== 'ok') throw new Error('点不到「添加视频」')
|
|
184
|
+
if (!(await waitJs('/已选择\\s*\\d+\\/200/.test(document.body.innerText)', 20000))) throw new Error('「添加视频」弹窗未打开')
|
|
185
|
+
for (let i = 0; i < 25; i++) { const ts = await accountTabs(); if (ts.length) break; await sleep(200) }
|
|
186
|
+
}
|
|
187
|
+
const TABS_JS = "(() => { const out = []; for (const l of document.querySelectorAll('" + NAV + "')) { for (const c of l.children) { const t = (c.innerText||'').trim(); if (t) out.push(t) } } return JSON.stringify([...new Set(out)]) })()"
|
|
188
|
+
async function accountTabs() { const t = JSON.parse(await page.eval(TABS_JS)); return t.filter((x) => ['我的视频', '上传视频', '其他视频'].indexOf(x) < 0) }
|
|
189
|
+
const CARDS_SIG = "(() => { const CARD = '" + CARD + "'; const ids = []; for (const c of document.querySelectorAll(CARD)) { const w = c.querySelector('[itemid]') || c; ids.push(w.getAttribute('itemid') || '') } return ids.slice(0, 8).join(',') })()"
|
|
190
|
+
const CARDS_JS = "(() => { const CARD = '" + CARD + "'; const out = []; const seen = new Set(); for (const c of document.querySelectorAll(CARD)) { const w = c.querySelector('[itemid]') || c; const id = w.getAttribute('itemid'); if (!id || seen.has(id)) continue; const r = c.getBoundingClientRect(); if (r.width < 10 || r.height < 10) continue; seen.add(id); const cb = c.querySelector('input[type=checkbox]'); const cls = /selected|checked/i.test(String(c.className || '')) ? 1 : 0; out.push({ id, ct: Number(w.getAttribute('createtime') || 0), title: String(w.getAttribute('title') || ''), sel: cb ? (cb.checked ? 1 : 0) : cls }) } return JSON.stringify(out) })()"
|
|
191
|
+
async function readCards() { try { return JSON.parse(await page.eval(CARDS_JS, 25000)) } catch (e) { return [] } }
|
|
192
|
+
const sigOf = (arr) => arr.slice(0, 6).map((c) => c.id).join(',') + '|' + arr.length
|
|
193
|
+
async function readCardsStable(prev) { let sig = '', same = 0, last = []; for (let i = 0; i < 40; i++) { const c = await readCards(); const s = sigOf(c); if (c.length > 0) { if (s === sig) same++; else { same = 0; sig = s } last = c; if (same >= 1 && (s !== prev || i >= 10)) return last } await sleep(180) } for (let i = 0; i < 3; i++) { await sleep(600); const c = await readCards(); if (c.length) return c } return last }
|
|
194
|
+
const SCROLLINFO = "(() => { const CARD = '" + CARD + "'; const c0 = document.querySelector(CARD); if (!c0) return '0'; let a = c0.parentElement, best = null; for (let i = 0; i < 10 && a; i++, a = a.parentElement) { const s = getComputedStyle(a); if ((s.overflowY === 'auto' || s.overflowY === 'scroll') && a.scrollHeight > a.clientHeight + 20) { best = a; break } } if (!best) return '0'; best.setAttribute('data-qc-scroll', 'on'); return JSON.stringify({ h: best.scrollHeight, c: best.clientHeight }) })()"
|
|
195
|
+
const SCROLLTO = (v) => "(() => { const e = document.querySelector('[data-qc-scroll=on]'); if (!e) return 'no'; e.scrollTop = " + Number(v) + "; return 'ok' })()"
|
|
196
|
+
async function readAllCards(prev) {
|
|
197
|
+
const cards = await readCardsStable(prev)
|
|
198
|
+
const seen = new Set(cards.map((c) => c.id))
|
|
199
|
+
try {
|
|
200
|
+
const si = await page.eval(SCROLLINFO, 15000)
|
|
201
|
+
if (si !== '0') {
|
|
202
|
+
const info = JSON.parse(si)
|
|
203
|
+
if (info.h > info.c + 20) {
|
|
204
|
+
const steps = Math.min(12, Math.ceil(info.h / Math.max(200, info.c)) + 1)
|
|
205
|
+
let extra = 0
|
|
206
|
+
for (let i = 1; i <= steps; i++) {
|
|
207
|
+
await page.eval(SCROLLTO(Math.round((info.h - info.c) * i / steps)), 15000)
|
|
208
|
+
await sleep(240)
|
|
209
|
+
for (const c of await readCards()) if (!seen.has(c.id)) { seen.add(c.id); cards.push(c); extra++ }
|
|
210
|
+
}
|
|
211
|
+
await page.eval(SCROLLTO(0), 15000)
|
|
212
|
+
await sleep(180)
|
|
213
|
+
if (extra) logLine('DEEP extra=' + extra + ' total=' + cards.length)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} catch (e) { logLine('DEEP-ERR ' + String(e.message).slice(0, 80)) }
|
|
217
|
+
return cards
|
|
218
|
+
}
|
|
219
|
+
const fmt = (ct) => new Date(ct * 1000 + 8 * 3600 * 1000).toISOString().slice(0, 16).replace('T', ' ')
|
|
220
|
+
const nowSecF = () => Math.floor(Date.now() / 1000)
|
|
221
|
+
const WIN = DAYS * 86400
|
|
222
|
+
|
|
223
|
+
const cardPosFn = (id) => "(() => { const CARD = '" + CARD + "'; const id = " + JSON.stringify(id) + "; const cands = [...document.querySelectorAll(CARD)].filter(x => (x.querySelector('[itemid]') || x).getAttribute('itemid') === id); if (!cands.length) return { err: 'nocard' }; for (const c of cands) { const r = c.getBoundingClientRect(); if (r.width < 10 || r.height < 10) continue; const x = Math.round(r.x + r.width / 2), y = Math.round(r.y + r.height / 2); if (x < 0 || y < 0 || x > innerWidth || y > innerHeight) continue; const e = document.elementFromPoint(x, y); if (!e || !c.contains(e)) continue; return { x, y } } return { err: 'noclick' } })()"
|
|
224
|
+
const isSelFn = (id) => "(() => { const CARD = '" + CARD + "'; const id = " + JSON.stringify(id) + "; const c = [...document.querySelectorAll(CARD)].find(x => (x.querySelector('[itemid]') || x).getAttribute('itemid') === id); if (!c) return -1; if (/selected|checked/i.test(String(c.className || ''))) return 1; const cb = c.querySelector('input[type=checkbox]'); if (cb) return cb.checked ? 1 : 0; return -1 })()"
|
|
225
|
+
const SCROLL = "(() => { const roots = [...document.querySelectorAll('[class*=create-material]')]; const els = new Set(); for (const r of roots) { let a = r; for (let i = 0; i < 8 && a; i++, a = a.parentElement) els.add(a) } let n = 0; for (const el of els) { if (el === document.body || el === document.documentElement) continue; const rc = el.getBoundingClientRect(); if (!rc || rc.width < 10) continue; const s = getComputedStyle(el); if ((s.overflowX === 'auto' || s.overflowX === 'scroll') && el.scrollWidth > el.clientWidth + 4) { el.scrollLeft = Math.min(el.scrollLeft + Math.round(el.clientWidth * 0.8), el.scrollWidth - el.clientWidth); n++ } if ((s.overflowY === 'auto' || s.overflowY === 'scroll') && el.scrollHeight > el.clientHeight + 4 && el.clientHeight > 200) { el.scrollTop = Math.min(el.scrollTop + Math.round(el.clientHeight * 0.6), el.scrollHeight - el.clientHeight); n++ } } return n })()"
|
|
226
|
+
|
|
227
|
+
async function clickCards(ids) {
|
|
228
|
+
let clicked = 0, already = 0, failed = []
|
|
229
|
+
let pending = ids.slice()
|
|
230
|
+
for (let pass = 0; pass < 10 && pending.length; pass++) {
|
|
231
|
+
const next = []
|
|
232
|
+
for (const id of pending) {
|
|
233
|
+
if ((await page.eval(isSelFn(id))) === 1) { already++; continue }
|
|
234
|
+
const p = await page.eval(cardPosFn(id))
|
|
235
|
+
if (!p || p.err || typeof p.x !== 'number') { next.push(id); continue }
|
|
236
|
+
await sleep(120)
|
|
237
|
+
await mouse(p.x, p.y)
|
|
238
|
+
clicked++
|
|
239
|
+
let st = -1
|
|
240
|
+
for (let i = 0; i < 8; i++) { st = await page.eval(isSelFn(id)); if (st === 1) break; await sleep(150) }
|
|
241
|
+
if (st !== 1) failed.push(id)
|
|
242
|
+
}
|
|
243
|
+
if (!next.length) { pending = []; break }
|
|
244
|
+
for (let k = 0; k < 2; k++) { await page.eval(SCROLL, 20000); await sleep(450) }
|
|
245
|
+
pending = next
|
|
246
|
+
}
|
|
247
|
+
return { clicked, already, failed, skipped: pending.length }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const COUNTER = "(() => { const m = (document.body.innerText||'').match(/已选择\\s*(\\d+)\\/200/); return m ? Number(m[1]) : -1 })()"
|
|
251
|
+
async function clickCandidates(cands) {
|
|
252
|
+
let clicked = 0; const failed = []
|
|
253
|
+
let cnt = await page.eval(COUNTER, 15000)
|
|
254
|
+
if (!(cnt >= 0)) cnt = 0
|
|
255
|
+
for (const c of cands) {
|
|
256
|
+
let done = false
|
|
257
|
+
for (let a = 0; a < 3 && !done; a++) {
|
|
258
|
+
if ((await page.eval(isSelFn(c.id))) === 1) { done = true; break }
|
|
259
|
+
const p = await page.eval(cardPosFn(c.id))
|
|
260
|
+
if (!p || p.err || typeof p.x !== "number") { await page.eval(SCROLL, 20000); await sleep(500); continue }
|
|
261
|
+
await mouse(p.x, p.y)
|
|
262
|
+
clicked++
|
|
263
|
+
for (let i = 0; i < 12; i++) {
|
|
264
|
+
await sleep(150)
|
|
265
|
+
const now = await page.eval(COUNTER, 15000)
|
|
266
|
+
if (now > cnt) { cnt = now; done = true; break }
|
|
267
|
+
if ((await page.eval(isSelFn(c.id))) === 1) { if (now > cnt) cnt = now; done = true; break }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (!done) failed.push(c.id)
|
|
271
|
+
}
|
|
272
|
+
return { clicked: clicked, failed: failed, counter: cnt }
|
|
273
|
+
}
|
|
274
|
+
async function closeDrawer() {
|
|
275
|
+
for (let i = 0; i < 3; i++) {
|
|
276
|
+
const ok = await page.eval("(() => { const els = [...document.querySelectorAll('[aria-label=关闭], [class*=close-icon], [class*=drawer] [class*=close]')].filter(e => e.getBoundingClientRect().width > 0); if (!els.length) return 'none'; els[0].click(); return 'ok' })()")
|
|
277
|
+
if (ok === 'ok') { await sleep(900); break }
|
|
278
|
+
await key('Escape', 27); await sleep(700)
|
|
279
|
+
}
|
|
280
|
+
await sleep(600)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const SUBMIT_CANDS = "(() => { const out = []; for (const b of document.querySelectorAll('button')) { const t = (b.innerText || '').replace(/\s+/g, ''); if (t !== '确定') continue; const r = b.getBoundingClientRect(); if (r.width < 5 || r.height < 5) continue; if (b.offsetParent === null) continue; const dis = !!b.disabled || b.getAttribute('aria-disabled') === 'true' || /disabled/i.test(String(b.className || '')); let inPopup = false; let n = b; for (let i = 0; i < 8 && n; i++, n = n.parentElement) { if ((n.innerText || '').indexOf('已选择') >= 0) { inPopup = true; break } } out.push({ x: Math.round(r.x + r.width / 2), y: Math.round(r.y + r.height / 2), dis, inPopup }) } return JSON.stringify(out) })()"
|
|
284
|
+
const SUBMIT_STATE = "(() => { const ls = [...document.querySelectorAll('button,span,div')].filter(e => e.children.length === 0 && (e.innerText||'').trim() === '确定' && e.getBoundingClientRect().width > 0); if (!ls.length) return 'none'; const b = ls[ls.length - 1]; const btn = b.closest('button') || b; const dis = !!btn.disabled || btn.getAttribute('aria-disabled') === 'true' || /disabled/i.test(String(btn.className || '')); return dis ? 'disabled' : 'enabled' })()"
|
|
285
|
+
async function tabClick(n) {
|
|
286
|
+
return await page.eval("(() => { for (const l of document.querySelectorAll('" + NAV + "')) { for (const c of l.children) { if ((c.innerText||'').replace(/\\s+/g,' ').trim().includes(" + JSON.stringify(n) + ")) { c.click(); return 'ok' } } } return 'NOTAB' })()")
|
|
287
|
+
}
|
|
288
|
+
const PLAN_MS = Number(process.env.QC_PLAN_MS || 180000)
|
|
289
|
+
async function scanPlan(plan) {
|
|
290
|
+
const t0 = Date.now()
|
|
291
|
+
const nowS = nowSecF()
|
|
292
|
+
const over = (where) => { if (Date.now() - t0 > PLAN_MS) throw new Error('单条计划超时 ' + Math.round((Date.now() - t0) / 1000) + 's @' + where) }
|
|
293
|
+
await searchPlanId(plan.id)
|
|
294
|
+
await openDrawer(plan.id)
|
|
295
|
+
const names = await accountTabs()
|
|
296
|
+
const rec = { planId: plan.id, planName: plan.name, accounts: [], cand: [], stale: 0, clicked: 0, failed: 0 }
|
|
297
|
+
let selTab = null
|
|
298
|
+
let prevSig = ''
|
|
299
|
+
for (const n of names) {
|
|
300
|
+
const tk = await tabClick(n)
|
|
301
|
+
if (tk !== 'ok') { logLine('TAB-FAIL ' + n + ' ' + tk); rec.tabFail = (rec.tabFail || 0) + 1; continue }
|
|
302
|
+
const cards = await readAllCards(prevSig)
|
|
303
|
+
prevSig = sigOf(cards)
|
|
304
|
+
over('cards ' + n)
|
|
305
|
+
logLine('ACCT-READ ' + n + ' cards=' + cards.length + ' recent10=' + cards.filter((c) => c.ct > 0 && nowS - c.ct <= 10 * 86400).length)
|
|
306
|
+
const cand = cards.filter((c) => c.ct > 0 && nowS - c.ct <= WIN && c.sel !== 1)
|
|
307
|
+
const stale = cards.filter((c) => c.ct > 0 && nowS - c.ct > WIN && c.sel !== 1).length
|
|
308
|
+
rec.accounts.push({ name: n, cards: cards.length, sel: cards.filter((c) => c.sel === 1).length, cand: cand.length })
|
|
309
|
+
rec.stale += stale
|
|
310
|
+
if (cand.length) {
|
|
311
|
+
for (const c of cand) rec.cand.push({ aweme: n, id: c.id, title: c.title, ct: c.ct })
|
|
312
|
+
if (mode === 'apply') {
|
|
313
|
+
const r = await clickCandidates(cand)
|
|
314
|
+
rec.clicked += r.clicked
|
|
315
|
+
rec.failed += r.failed.length
|
|
316
|
+
rec.selAfter = r.counter
|
|
317
|
+
for (const c of cand) if (r.failed.indexOf(c.id) < 0) csv({ planId: plan.id, planName: plan.name, aweme: n, title: c.title, publishTime: fmt(c.ct) })
|
|
318
|
+
logLine('ACCT-SEL ' + n + ' clicked=' + r.clicked + ' counter=' + r.counter + ' failed=' + r.failed.length)
|
|
319
|
+
if (r.counter > 0 && !selTab) selTab = n
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (mode === 'apply' && rec.cand.length && !(rec.selAfter > 0)) {
|
|
324
|
+
logLine('NO-PENDING-SELECTION counter=0 skip submit')
|
|
325
|
+
await clickExact('取消'); await sleep(1200); await closeDrawer()
|
|
326
|
+
rec.ms = Date.now() - t0
|
|
327
|
+
return rec
|
|
328
|
+
}
|
|
329
|
+
if (mode === 'apply' && rec.cand.length) {
|
|
330
|
+
logLine('SELECTED-TOTAL ' + (rec.selAfter || 0) + ' selTab=' + selTab)
|
|
331
|
+
if (selTab) { await tabClick(selTab); await sleep(1500) }
|
|
332
|
+
let pick = null
|
|
333
|
+
for (let i = 0; i < 12; i++) {
|
|
334
|
+
let cands = []
|
|
335
|
+
try { cands = JSON.parse(await page.eval(SUBMIT_CANDS, 20000)) } catch (e) { cands = [] }
|
|
336
|
+
if (i === 0 || !pick) logLine('SUBMIT-CANDS ' + JSON.stringify(cands))
|
|
337
|
+
pick = cands.find((c) => c.inPopup && !c.dis) || cands.find((c) => !c.dis) || null
|
|
338
|
+
if (pick) break
|
|
339
|
+
await sleep(350)
|
|
340
|
+
}
|
|
341
|
+
if (!pick) throw new Error('确定按钮不可点(没有可用候选)')
|
|
342
|
+
await mouse(pick.x, pick.y)
|
|
343
|
+
logLine('SUBMIT ' + plan.id + ' at=' + pick.x + ',' + pick.y)
|
|
344
|
+
let closed = 0
|
|
345
|
+
for (let i = 0; i < 20; i++) { await sleep(200); closed = await page.eval("document.body.innerText.indexOf('已选择') >= 0 ? 0 : 1"); if (closed) break }
|
|
346
|
+
logLine('POPUP-CLOSED ' + closed)
|
|
347
|
+
if (!closed) throw new Error('提交后素材弹窗没关闭')
|
|
348
|
+
} else {
|
|
349
|
+
await clickExact('取消')
|
|
350
|
+
await sleep(1200)
|
|
351
|
+
}
|
|
352
|
+
rec.ms = Date.now() - t0
|
|
353
|
+
await closeDrawer()
|
|
354
|
+
return rec
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
async function main() {
|
|
358
|
+
if (mode !== 'scan' && mode !== 'apply') { logLine('用法: node newmat.mjs scan|apply <aavid> [--days=5] [--max=N] [--plan=ID]'); process.exit(2) }
|
|
359
|
+
if (!aavid) { logLine('缺 aavid'); process.exit(2) }
|
|
360
|
+
logLine('START mode=' + mode + ' aavid=' + aavid + ' days=' + DAYS + ' max=' + MAX + (ONE ? ' plan=' + ONE : ''))
|
|
361
|
+
const stateF = path.join(OUT, 'state-' + aavid + '.json')
|
|
362
|
+
const done = (fs.existsSync(stateF) && !FORCE) ? JSON.parse(fs.readFileSync(stateF, 'utf8')) : { done: [] }
|
|
363
|
+
if (!done.done) done.done = []
|
|
364
|
+
let plans = []
|
|
365
|
+
if (ONE) plans = [{ id: ONE, name: '(--plan 指定)' }]
|
|
366
|
+
else if (done.queue && done.queue.length && !FORCE) {
|
|
367
|
+
plans = done.queue.map((x) => (typeof x === 'string' ? { id: x, name: '(--queue)' } : x))
|
|
368
|
+
logLine('RESUME queue=' + plans.length + ' done=' + done.done.length)
|
|
369
|
+
} else {
|
|
370
|
+
await openList()
|
|
371
|
+
try { await filterRunning(); logLine('FILTER-ONCE ok') } catch (e) { logLine('FILTER-ONCE fail ' + String(e.message).slice(0, 60)) }
|
|
372
|
+
plans = await enumerate()
|
|
373
|
+
plans = plans.filter((p) => p.run !== false)
|
|
374
|
+
logLine('PLANS-RUNNING ' + plans.length)
|
|
375
|
+
done.queue = plans
|
|
376
|
+
try { fs.writeFileSync(stateF, JSON.stringify(done, null, 1)) } catch (e) {}
|
|
377
|
+
}
|
|
378
|
+
logLine('PLANS ' + plans.length)
|
|
379
|
+
if (plans.length && !(await ensureList())) throw new Error('列表页不可用')
|
|
380
|
+
let ok = 0; const errs = []
|
|
381
|
+
for (const p of plans) {
|
|
382
|
+
if (done.done.indexOf(p.id) >= 0) { logLine('SKIP ' + p.id); continue }
|
|
383
|
+
try {
|
|
384
|
+
const r = await scanPlan(p)
|
|
385
|
+
ok++
|
|
386
|
+
if (mode === 'apply') { done.done.push(p.id); fs.writeFileSync(stateF, JSON.stringify(done, null, 1)) }
|
|
387
|
+
logLine('PLAN ' + p.id + ' accounts=' + r.accounts.length + ' cand=' + r.cand.length + ' clicked=' + r.clicked + ' failed=' + r.failed + ' stale=' + r.stale + ' ' + (r.ms / 1000).toFixed(1) + 's ' + JSON.stringify(r.accounts.map((a) => a.name + ':' + a.cand)))
|
|
388
|
+
} catch (e) {
|
|
389
|
+
const msg = String(e.message).slice(0, 160)
|
|
390
|
+
errs.push({ id: p.id, err: msg })
|
|
391
|
+
logLine('PLAN-ERR ' + p.id + ' ' + msg)
|
|
392
|
+
try { await clickExact('取消'); await closeDrawer() } catch (e2) {}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
logLine('DONE ok=' + ok + ' err=' + errs.length + ' records=' + CSVF)
|
|
396
|
+
if (errs.length) logLine('ERRS ' + JSON.stringify(errs))
|
|
397
|
+
process.exit(0)
|
|
398
|
+
}
|
|
399
|
+
await main()
|
|
@@ -14,7 +14,9 @@ import { pathToFileURL } from 'node:url'
|
|
|
14
14
|
// 5) 全部号点完,一次性点「确定」提交;不再二次进弹窗、不再翻抽屉核对
|
|
15
15
|
import fs from 'node:fs'
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
// 复制技能固定连 9223 的独立浏览器实例(与 9222 的巡航实例物理隔离,互不抢标签)。
|
|
18
|
+
process.env.QC_CDP_PORT = process.env.QC_CDP_PORT || '9223'
|
|
19
|
+
const PORT = Number(process.env.QC_CDP_PORT || 9222), BASE = 'http://127.0.0.1:' + PORT
|
|
18
20
|
const OUT_DIR = process.env.QC_OUT || path.join(process.env.USERPROFILE || process.env.HOME || '.', '.qianchuan-cruise', 'out')
|
|
19
21
|
const CARD = '.main-vmok-plugin-create-material-list-card-item'
|
|
20
22
|
const NAV = '.main-vmok-plugin-tabs__nav-list'
|
|
@@ -52,6 +54,7 @@ const clickTab = (name) => `(()=>{const ls=[...document.querySelectorAll('${NAV}
|
|
|
52
54
|
// 只取真能被点到的卡片:弹窗 DOM 里有隐藏同构副本,坐标取自副本会点击落空或点穿页面
|
|
53
55
|
// 返回全部去重卡片 id(网格多行、可横向滚动;滚动与命中判定交给 cardPos)
|
|
54
56
|
const listCards = `(()=>{const seen=new Set();const out=[];for(const c of document.querySelectorAll('${CARD}')){const w=c.querySelector('[itemid]')||c;const id=w.getAttribute('itemid');if(!id||seen.has(id))continue;const r=c.getBoundingClientRect();if(r.width<10||r.height<10)continue;seen.add(id);out.push({id,y:Math.round(r.y),x:Math.round(r.x)})}out.sort((a,b)=>a.y-b.y||a.x-b.x);return out.map(o=>o.id)})()`
|
|
57
|
+
const allCards = '(()=>{const out=[];for(const c of document.querySelectorAll("' + CARD + '")){const w=c.querySelector("[itemid]")||c;const id=w.getAttribute("itemid");if(!id)continue;const r=c.getBoundingClientRect();if(r.width<10||r.height<10)continue;const im=c.querySelector("img");const src=im?String(im.src||""):"";const km=src.match(/tos-cn-[^\\/]+\\/([^~?]+)/);out.push([id,String(w.getAttribute("title")||""),km?km[1]:""])}return JSON.stringify(out)})()'
|
|
55
58
|
const chainInfo = (id) => `(()=>{const c=[...document.querySelectorAll('${CARD}')].map(x=>x).filter(x=>(x.querySelector('[itemid]')||x).getAttribute('itemid')===${JSON.stringify(id)})[0];if(!c)return null;const out=[];let a=c;for(let i=0;i<8&&a&&a!==document.body;i++,a=a.parentElement){const s=getComputedStyle(a);const r=a.getBoundingClientRect();out.push({t:a.tagName,c:String(a.className).slice(0,60),ox:s.overflowX,oy:s.overflowY,sl:a.scrollLeft,sw:a.scrollWidth,cw:a.clientWidth,x:Math.round(r.x),w:Math.round(r.width),tr:s.transform.slice(0,30)})}return out})()`
|
|
56
59
|
// 面板被渲染到视口右侧外(左边界≥innerWidth)时,强制 fixed 定位到屏幕内,避免点不到卡片
|
|
57
60
|
const fixOffscreen = `(()=>{const el=document.querySelector('.main-vmok-plugin-create-material-list');if(!el)return 'nolist';let a=el.offsetParent,tf=null;while(a&&a!==document.documentElement){const s=getComputedStyle(a);if(s.transform&&s.transform!=='none'){tf=a;break}a=a.offsetParent}if(!tf){let p=el.parentElement;while(p&&p!==document.documentElement){const s=getComputedStyle(p);if(s.transform&&s.transform!=='none'){tf=p;break}p=p.parentElement}}if(tf){tf.style.transform='none'}let r=el.getBoundingClientRect();if(!(r.x>=0&&r.x+40<innerWidth)){el.style.position='fixed';el.style.left='320px';el.style.top='60px';el.style.zIndex='99999';el.style.background='#fff';el.style.maxHeight=(innerHeight-90)+'px';el.style.overflow='auto'}const r2=el.getBoundingClientRect();return 'tf='+(tf?String(tf.className).slice(0,42):'none')+' x='+Math.round(r.x)+' -> '+Math.round(r2.x)})()`
|
|
@@ -109,13 +112,27 @@ async function selectCards(c, ids) {
|
|
|
109
112
|
const scrollStep = '(()=>{const roots=[...document.querySelectorAll("[class*=create-material]")];const els=new Set();for(const r of roots){let a=r;for(let i=0;i<8&&a;i++,a=a.parentElement)els.add(a)}let n=0;for(const el of els){if(el===document.body||el===document.documentElement)continue;const rc=el.getBoundingClientRect();if(!rc||rc.width<10)continue;const s=getComputedStyle(el);if((s.overflowX==="auto"||s.overflowX==="scroll")&&el.scrollWidth>el.clientWidth+4){el.scrollLeft=Math.min(el.scrollLeft+Math.round(el.clientWidth*0.8),el.scrollWidth-el.clientWidth);n++}if((s.overflowY==="auto"||s.overflowY==="scroll")&&el.scrollHeight>el.clientHeight+4&&el.clientHeight>200){el.scrollTop=Math.min(el.scrollTop+Math.round(el.clientHeight*0.6),el.scrollHeight-el.clientHeight);n++}}return n})()'
|
|
110
113
|
|
|
111
114
|
// 供 copy.mjs 复用:在已打开的「添加视频」弹窗里按名单逐个抖音号全选,返回结构化日志
|
|
112
|
-
export async function pickAllOnTarget(target, names) {
|
|
115
|
+
export async function pickAllOnTarget(target, names, onlyIds, onlyRows) {
|
|
116
|
+
const ONLY = Array.isArray(onlyIds) && onlyIds.length ? new Set(onlyIds.map(String)) : null
|
|
113
117
|
const c = await connect(target)
|
|
114
118
|
const modalOpen = await ev(c, `(()=>/已选择\\s*\\d+\\/200/.test(document.body.innerText))()`)
|
|
115
119
|
if (!modalOpen) return { error: '「添加视频」弹窗没开,先点「添加视频」' }
|
|
116
120
|
await ev(c, resetScroll)
|
|
117
121
|
|
|
118
122
|
const log = []
|
|
123
|
+
const matchedIds = new Set()
|
|
124
|
+
const matchedTitles = []
|
|
125
|
+
const OROWS = Array.isArray(onlyRows) ? onlyRows : []
|
|
126
|
+
const IMGS = new Set(OROWS.map((r) => (r && typeof r === 'object' ? String(r.img || '') : '')).filter(Boolean))
|
|
127
|
+
const WHITE = OROWS.map((r) => (r && typeof r === 'object' ? String(r.row || '') : String(r))).map((r) => r.replace(/[\s\u00a0]+/g, '')).filter(Boolean)
|
|
128
|
+
const USE_IMG = IMGS.size > 0
|
|
129
|
+
const DRY = process.env.QC_ONLY_DRYRUN === '1'
|
|
130
|
+
const DUMP = process.env.QC_DUMP_CARDS === '1'
|
|
131
|
+
const cardsAll = []
|
|
132
|
+
console.log('MATCH-MODE ' + (USE_IMG ? 'img' : 'title') + ' imgs=' + IMGS.size + ' rows=' + WHITE.length)
|
|
133
|
+
console.log('WHITE-SAMPLE ' + JSON.stringify(WHITE.slice(0, 2).map((t) => t.slice(0, 50))) + ' IMG-SAMPLE ' + JSON.stringify([...IMGS].slice(0, 2)))
|
|
134
|
+
let onlyClicked = 0
|
|
135
|
+
let onlyDiag = null
|
|
119
136
|
const fixRes = 'skip'
|
|
120
137
|
for (const name of names) {
|
|
121
138
|
let r = await ev(c, clickTab(name))
|
|
@@ -131,17 +148,106 @@ export async function pickAllOnTarget(target, names) {
|
|
|
131
148
|
if (t === 'OK') { refreshed = true; await sleep(1200); ids = await pollCards(c, 12000) }
|
|
132
149
|
}
|
|
133
150
|
const before = await ev(c, getSel)
|
|
151
|
+
if (DUMP) {
|
|
152
|
+
try {
|
|
153
|
+
const arr = JSON.parse(await ev(c, '(() => { const out = []; for (const c of document.querySelectorAll("' + CARD + '")) { const w = c.querySelector("[itemid]") || c; const id = w.getAttribute("itemid"); if (!id) continue; out.push([id, String(w.getAttribute("title") || ""), String(w.getAttribute("createtime") || ""), String(w.getAttribute("authoruid") || ""), String(w.getAttribute("awemeuid") || "")]) } return JSON.stringify(out) })()'))
|
|
154
|
+
for (const a of arr) cardsAll.push({ tab: name, id: a[0], title: a[1], createtime: a[2], authoruid: a[3], awemeuid: a[4] })
|
|
155
|
+
} catch (e) { console.log('DUMP-ERR ' + String(e.message).slice(0, 80)) }
|
|
156
|
+
}
|
|
134
157
|
let sel = { clicked: 0, retried: 0, skipped: 0, already: 0, unknown: [] }
|
|
135
|
-
if (
|
|
158
|
+
if (ONLY) {
|
|
159
|
+
const rowsTab = OROWS.filter((r0) => r0 && typeof r0 === 'object' && (String(r0.aweme || '') === name || String(r0.awemeId || '') === name))
|
|
160
|
+
const rowsUse = rowsTab.length ? rowsTab : OROWS
|
|
161
|
+
if (!rowsTab.length) console.log('FALLBACK-ALL ' + name)
|
|
162
|
+
const whiteTab = rowsUse.map((r0) => (r0 && typeof r0 === 'object' ? String(r0.row || '') : String(r0))).map((s) => s.replace(/[\s\u00a0]+/g, '')).filter(Boolean)
|
|
163
|
+
const r = await selectOnly(c, ONLY, whiteTab, 25000, IMGS, USE_IMG, DRY)
|
|
164
|
+
sel = { clicked: r.clicked, retried: 0, skipped: 0, already: 0, unknown: [], picked: r.picked }
|
|
165
|
+
onlyDiag = r.diag || null
|
|
166
|
+
for (const id of r.picked) matchedIds.add(String(id))
|
|
167
|
+
for (const t of r.pickedTitles) matchedTitles.push(t)
|
|
168
|
+
onlyClicked += r.clicked
|
|
169
|
+
await ev(c, resetScroll)
|
|
170
|
+
} else if (ids.length) sel = await selectCards(c, ids)
|
|
136
171
|
const after = await ev(c, getSel)
|
|
137
172
|
if (refreshed) { await ev(c, filterToggle); await sleep(600) }
|
|
138
173
|
log.push({ name, cards: ids.length, before, after, delta: after - before, clicked: sel.clicked, retried: sel.retried, already: sel.already, unknown: sel.unknown, skipped: sel.skipped, why: sel.why, pos: await ev(c, listPos), note: ids.length ? undefined : '该号主页无可投视频(取消过滤后仍为空)' })
|
|
139
174
|
}
|
|
175
|
+
if (DUMP) {
|
|
176
|
+
const fsmod = await import('node:fs')
|
|
177
|
+
fsmod.writeFileSync(process.env.QC_DUMP_FILE || 'cards-dump.json', JSON.stringify(cardsAll))
|
|
178
|
+
console.log('DUMP-WROTE ' + cardsAll.length)
|
|
179
|
+
}
|
|
180
|
+
let onlyInfo = null
|
|
181
|
+
if (ONLY) {
|
|
182
|
+
const matchedRows = WHITE.filter((r) => matchedTitles.some((t) => r.indexOf(t) >= 0))
|
|
183
|
+
const missRow = WHITE.filter((r) => matchedRows.indexOf(r) < 0)
|
|
184
|
+
const missId = WHITE.length ? [] : [...ONLY].filter((id) => !matchedIds.has(id))
|
|
185
|
+
onlyInfo = { want: WHITE.length || ONLY.size, matched: WHITE.length ? matchedRows.length : matchedIds.size, missing: missRow.slice(0, 10).map((r) => r.slice(0, 90)).concat(missId.slice(0, 10)), missingCount: missRow.length + missId.length, clicked: onlyClicked }
|
|
186
|
+
console.log('CARD-DIAG ' + JSON.stringify(onlyDiag))
|
|
187
|
+
try { console.log('SRC-OPTS ' + JSON.stringify(await ev(c, '(() => { const es = [...document.querySelectorAll("[class*=select],[class*=Select]")].map((e) => String(e.innerText || "").replace(/[\s]+/g, " ").trim()).filter((t) => t); return es.slice(0, 15) })()'))) } catch (e) { console.log('SRC-OPTS err ' + String(e.message).slice(0, 80)) }
|
|
188
|
+
if (onlyInfo.missingCount) console.log('ONLY-MISSING ' + onlyInfo.missingCount + ' ' + JSON.stringify(onlyInfo.missing))
|
|
189
|
+
}
|
|
190
|
+
if (DRY) {
|
|
191
|
+
const cx = await ev(c, '(() => { const bs = [...document.querySelectorAll("button")].filter((b) => b.innerText.trim() === "取消" && b.getBoundingClientRect().width > 0); if (!bs.length) return "NOCANCEL"; const r = bs[bs.length - 1].getBoundingClientRect(); return [Math.round(r.x + r.width / 2), Math.round(r.y + r.height / 2)] })()')
|
|
192
|
+
if (Array.isArray(cx)) { await click(c, cx[0], cx[1]); await sleep(800) }
|
|
193
|
+
return { ok: true, dryRun: true, submitted: false, only: onlyInfo, log }
|
|
194
|
+
}
|
|
140
195
|
const okb = await ev(c, `(()=>{const bs=[...document.querySelectorAll('button')].filter(b=>b.innerText.trim()==='确定'&&b.getBoundingClientRect().width>0);if(!bs.length)return 'NOOK';const r=bs[bs.length-1].getBoundingClientRect();return [Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]})()`)
|
|
141
196
|
let submitted = false
|
|
142
197
|
if (Array.isArray(okb)) { await click(c, okb[0], okb[1]); await sleep(5000); submitted = true }
|
|
143
198
|
const total = await ev(c, `(()=>{const m=document.body.innerText.match(/共\\d+个视频/);return m?m[0]:null})()`)
|
|
144
|
-
return { ok: true, fix: fixRes, submitted, totalVideos: total, log }
|
|
199
|
+
return { ok: true, fix: fixRes, submitted, totalVideos: total, only: onlyInfo, log }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function titleOf(id) {
|
|
203
|
+
return '(() => { const ws = [...document.querySelectorAll("' + CARD + '")].map((x) => x.querySelector("[itemid]")).filter((x) => x && x.getAttribute("itemid") === ' + JSON.stringify(String(id)) + '); if (!ws.length) return ""; return String(ws[0].getAttribute("title") || "").replace(/[\s\u00a0]+/g, "") })()'
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function selectOnly(c, ONLY, WHITE, maxMs, IMGS, USE_IMG, DRY) {
|
|
207
|
+
const seen = new Set(); const picked = new Set(); const pickedTitles = []; const sample = []; let clicked = 0; let idle = 0
|
|
208
|
+
const __report = () => { try { console.log('ONLY-SAMPLE ' + JSON.stringify(sample)) } catch (e) {} }
|
|
209
|
+
const t0 = Date.now()
|
|
210
|
+
while (Date.now() - t0 < maxMs && idle < 2) {
|
|
211
|
+
let ids = []
|
|
212
|
+
try { ids = await ev(c, listCards) } catch (e) { ids = [] }
|
|
213
|
+
let added = 0
|
|
214
|
+
for (const id of ids) if (!seen.has(id)) { seen.add(id); added++ }
|
|
215
|
+
const hits = []
|
|
216
|
+
if (WHITE.length) {
|
|
217
|
+
let pairs = []
|
|
218
|
+
try { pairs = JSON.parse(await ev(c, allCards)) } catch (e) { pairs = [] }
|
|
219
|
+
for (const pr of pairs) {
|
|
220
|
+
const id = pr[0]; const raw = String(pr[1] || '')
|
|
221
|
+
if (picked.has(id)) continue
|
|
222
|
+
const t = raw.replace(/[\s\u00a0]+/g, '')
|
|
223
|
+
if (sample.length < 6 && t) sample.push(t.slice(0, 60))
|
|
224
|
+
if (!t && !USE_IMG) continue
|
|
225
|
+
const hit = USE_IMG ? IMGS.has(String(pr[2] || '')) : WHITE.some((r) => r.indexOf(t) >= 0)
|
|
226
|
+
if (hit) hits.push([id, t])
|
|
227
|
+
}
|
|
228
|
+
} else if (ONLY) {
|
|
229
|
+
for (const id of ids) if (ONLY.has(String(id)) && !picked.has(id)) hits.push([id, ''])
|
|
230
|
+
}
|
|
231
|
+
for (const hp of hits) {
|
|
232
|
+
const id = hp[0]; const t = hp[1]
|
|
233
|
+
if (picked.has(id)) continue
|
|
234
|
+
if (!DRY) {
|
|
235
|
+
const p = await ev(c, cardPos(id))
|
|
236
|
+
if (!p || p.err || typeof p.x !== 'number') continue
|
|
237
|
+
if ((await ev(c, isSel(id))) !== 1) { await click(c, p.x, p.y); clicked++ }
|
|
238
|
+
} else clicked++
|
|
239
|
+
picked.add(id)
|
|
240
|
+
if (t) pickedTitles.push(t)
|
|
241
|
+
await sleep(120)
|
|
242
|
+
}
|
|
243
|
+
idle = added ? 0 : idle + 1
|
|
244
|
+
const moved = await ev(c, scrollStep)
|
|
245
|
+
await sleep(350)
|
|
246
|
+
if (!moved && !added) break
|
|
247
|
+
}
|
|
248
|
+
const diag = await ev(c, '(() => { const cards=[...document.querySelectorAll("' + CARD + '")].slice(0,3); return cards.map((c) => { const w=c.querySelector("[itemid]")||c; const attrs={}; for (const a of w.attributes) attrs[a.name]=String(a.value).slice(0,90); const img=c.querySelector("img"); return { itemid:w.getAttribute("itemid"), attrs, img:img?String(img.src).slice(0,180):null, text:(c.innerText||"").replace(/\s+/g," ").slice(0,120), html:String(c.outerHTML).slice(0,400) } }) })()')
|
|
249
|
+
console.log('ONLY-SAMPLE ' + JSON.stringify(sample))
|
|
250
|
+
return { clicked, seen: [...seen], picked: [...picked], pickedTitles, diag }
|
|
145
251
|
}
|
|
146
252
|
|
|
147
253
|
async function main() {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { connect, sleep, listPages } from '../../live-control/scripts/cdp-lib.mjs'
|
|
2
|
+
// 复制技能固定连 9223 的独立浏览器实例(与 9222 的巡航实例物理隔离,互不抢标签)。
|
|
3
|
+
process.env.QC_CDP_PORT = process.env.QC_CDP_PORT || '9223';
|
|
2
4
|
const A = process.argv[2], ID = process.argv[3]
|
|
3
5
|
const pages = await listPages()
|
|
4
6
|
const hit = pages.find(p => p.type==='page' && (p.url||'').indexOf('overall-prom')>=0 && (p.url||'').indexOf(A)>=0)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
rem ===== QianChuan COPY debug browser launcher (port 9223, separate instance) =====
|
|
3
|
+
rem This instance is dedicated to the plan-copy skill so it never fights the cruise tab on 9222.
|
|
4
|
+
rem port: 9223 profile: %USERPROFILE%\qc-chrome-copy Chrome first, Edge fallback
|
|
5
|
+
rem First run: log in to QianChuan in this window once; the login is kept in this profile.
|
|
6
|
+
setlocal
|
|
7
|
+
set "PORT=9223"
|
|
8
|
+
set "PROFILE=%USERPROFILE%\qc-chrome-copy"
|
|
9
|
+
|
|
10
|
+
curl -s -m 2 http://127.0.0.1:%PORT%/json/version >nul 2>&1
|
|
11
|
+
if not errorlevel 1 (
|
|
12
|
+
echo [QC-COPY] already listening on port %PORT%
|
|
13
|
+
exit /b 0
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
set "BROWSER=C:\Program Files\Google\Chrome\Application\chrome.exe"
|
|
17
|
+
if not exist "%BROWSER%" set "BROWSER=C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
|
|
18
|
+
if not exist "%BROWSER%" set "BROWSER=%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"
|
|
19
|
+
if not exist "%BROWSER%" set "BROWSER=%ProgramFiles%\Microsoft\Edge\Application\msedge.exe"
|
|
20
|
+
if not exist "%BROWSER%" (
|
|
21
|
+
echo [QC-COPY] Chrome/Edge not found. Edit the BROWSER line in this file.
|
|
22
|
+
pause
|
|
23
|
+
exit /b 1
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
echo [QC-COPY] browser: %BROWSER%
|
|
27
|
+
echo [QC-COPY] profile: %PROFILE%
|
|
28
|
+
start "" "%BROWSER%" ^
|
|
29
|
+
--remote-debugging-port=%PORT% ^
|
|
30
|
+
--remote-allow-origins=* ^
|
|
31
|
+
--user-data-dir="%PROFILE%" ^
|
|
32
|
+
--no-first-run ^
|
|
33
|
+
--no-default-browser-check ^
|
|
34
|
+
--start-maximized ^
|
|
35
|
+
"https://qianchuan.jinritemai.com/login"
|
|
36
|
+
|
|
37
|
+
echo [QC-COPY] waiting for port %PORT% ...
|
|
38
|
+
for /l %%i in (1,1,40) do (
|
|
39
|
+
timeout /t 1 /nobreak >nul
|
|
40
|
+
curl -s -m 1 http://127.0.0.1:%PORT%/json/version >nul 2>&1
|
|
41
|
+
if not errorlevel 1 (
|
|
42
|
+
echo [QC-COPY] OK port %PORT% ready in %%i s
|
|
43
|
+
exit /b 0
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
echo [QC-COPY] FAILED: port not listening.
|
|
47
|
+
echo [QC-COPY] Hint: this profile runs in its own window; keep it open while copying.
|
|
48
|
+
pause
|
|
49
|
+
exit /b 1
|