channel-worker 1.0.19 → 1.0.20
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/lib/command-poller.js +109 -0
- package/package.json +1 -1
package/lib/command-poller.js
CHANGED
|
@@ -52,6 +52,9 @@ class CommandPoller {
|
|
|
52
52
|
case 'set_thumbnail':
|
|
53
53
|
await this.handleSetThumbnail(command);
|
|
54
54
|
break;
|
|
55
|
+
case 'set_tags':
|
|
56
|
+
await this.handleSetTags(command);
|
|
57
|
+
break;
|
|
55
58
|
default:
|
|
56
59
|
// Other commands (scan_facebook_pages, etc.) handled by extension
|
|
57
60
|
console.log(`[commands] Skipping ${command.type} — handled by extension`);
|
|
@@ -334,6 +337,112 @@ class CommandPoller {
|
|
|
334
337
|
}
|
|
335
338
|
}
|
|
336
339
|
|
|
340
|
+
async handleSetTags(command) {
|
|
341
|
+
const { tags, profile_id } = command.payload || {};
|
|
342
|
+
console.log(`[commands] Setting tags for profile: ${profile_id}`, tags);
|
|
343
|
+
try {
|
|
344
|
+
const WebSocket = require('ws');
|
|
345
|
+
|
|
346
|
+
// Find browser
|
|
347
|
+
if (!this.nst) {
|
|
348
|
+
const apiKey = await this.api.getSetting('nst_api_key');
|
|
349
|
+
if (apiKey) this.nst = new NstManager(apiKey);
|
|
350
|
+
}
|
|
351
|
+
const browsersRes = await fetch('http://localhost:8848/api/v2/browsers', {
|
|
352
|
+
headers: { 'x-api-key': this.nst?.apiKey || '' },
|
|
353
|
+
});
|
|
354
|
+
const browsersData = await browsersRes.json();
|
|
355
|
+
const browser = (browsersData?.data || []).find(b => b.name === profile_id) || browsersData?.data?.[0];
|
|
356
|
+
if (!browser) throw new Error('No running browser');
|
|
357
|
+
|
|
358
|
+
const pagesRes = await fetch(`http://localhost:${browser.remoteDebuggingPort}/json/list`);
|
|
359
|
+
const pages = await pagesRes.json();
|
|
360
|
+
const studioPage = pages.find(p => p.url?.includes('studio.youtube.com') && p.type === 'page');
|
|
361
|
+
if (!studioPage) throw new Error('YouTube Studio tab not found');
|
|
362
|
+
|
|
363
|
+
const tagStr = tags.join(', ');
|
|
364
|
+
|
|
365
|
+
const result = await new Promise((resolve, reject) => {
|
|
366
|
+
const ws = new WebSocket(studioPage.webSocketDebuggerUrl);
|
|
367
|
+
let msgId = 1;
|
|
368
|
+
function send(method, params = {}) {
|
|
369
|
+
const id = msgId++;
|
|
370
|
+
return new Promise((res, rej) => {
|
|
371
|
+
const handler = (data) => {
|
|
372
|
+
const msg = JSON.parse(data);
|
|
373
|
+
if (msg.id === id) { ws.removeListener('message', handler); msg.error ? rej(new Error(msg.error.message)) : res(msg.result); }
|
|
374
|
+
};
|
|
375
|
+
ws.on('message', handler);
|
|
376
|
+
ws.send(JSON.stringify({ id, method, params }));
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
ws.on('open', async () => {
|
|
381
|
+
try {
|
|
382
|
+
// Click "Show more" button
|
|
383
|
+
await send('Runtime.evaluate', {
|
|
384
|
+
expression: `
|
|
385
|
+
(function() {
|
|
386
|
+
const texts = ['show more', 'hiện thêm', 'hiển thị thêm'];
|
|
387
|
+
const allEls = document.querySelectorAll('*');
|
|
388
|
+
for (const el of allEls) {
|
|
389
|
+
if (el.children.length > 3) continue;
|
|
390
|
+
const t = el.textContent?.trim()?.toLowerCase() || '';
|
|
391
|
+
if (texts.some(x => t === x)) { el.click(); return 'clicked: ' + el.textContent?.trim(); }
|
|
392
|
+
}
|
|
393
|
+
return 'not_found';
|
|
394
|
+
})()
|
|
395
|
+
`,
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// Wait for section to expand
|
|
399
|
+
await new Promise(r => setTimeout(r, 2000));
|
|
400
|
+
|
|
401
|
+
// Find and fill tags input — placeholder "Add tag"
|
|
402
|
+
const fillResult = await send('Runtime.evaluate', {
|
|
403
|
+
expression: `
|
|
404
|
+
(function() {
|
|
405
|
+
// Find input with placeholder "Add tag" or aria-label containing "tag"
|
|
406
|
+
const inputs = document.querySelectorAll('input');
|
|
407
|
+
for (const inp of inputs) {
|
|
408
|
+
const ph = (inp.placeholder || '').toLowerCase();
|
|
409
|
+
const aria = (inp.getAttribute('aria-label') || '').toLowerCase();
|
|
410
|
+
if (ph.includes('add tag') || ph.includes('thêm thẻ') || aria.includes('tag') || aria.includes('thẻ')) {
|
|
411
|
+
inp.focus();
|
|
412
|
+
inp.value = '${tagStr.replace(/'/g, "\\'")}';
|
|
413
|
+
inp.dispatchEvent(new Event('input', { bubbles: true }));
|
|
414
|
+
inp.dispatchEvent(new Event('change', { bubbles: true }));
|
|
415
|
+
// Press Enter to confirm
|
|
416
|
+
inp.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', keyCode: 13, bubbles: true }));
|
|
417
|
+
inp.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter', code: 'Enter', keyCode: 13, bubbles: true }));
|
|
418
|
+
return 'tags_filled';
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return 'no_tag_input';
|
|
422
|
+
})()
|
|
423
|
+
`,
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
ws.close();
|
|
427
|
+
resolve({ success: true, result: fillResult?.result?.value });
|
|
428
|
+
} catch (e) { ws.close(); resolve({ success: false, error: e.message }); }
|
|
429
|
+
});
|
|
430
|
+
ws.on('error', (e) => reject(e));
|
|
431
|
+
setTimeout(() => { ws.close(); reject(new Error('CDP timeout')); }, 15000);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
console.log(`[commands] Tags CDP result:`, result);
|
|
435
|
+
await this.api.updateCommand(command._id, {
|
|
436
|
+
status: result.success ? 'done' : 'failed',
|
|
437
|
+
result,
|
|
438
|
+
error: result.error || null,
|
|
439
|
+
});
|
|
440
|
+
} catch (err) {
|
|
441
|
+
console.error(`[commands] Set tags failed: ${err.message}`);
|
|
442
|
+
await this.api.updateCommand(command._id, { status: 'failed', error: err.message });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
337
446
|
async handleCloseProfile(command) {
|
|
338
447
|
const { profile_id } = command.payload || {};
|
|
339
448
|
console.log(`[commands] Closing profile: ${profile_id}`);
|