channel-worker 1.0.20 → 1.0.21
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 +68 -0
- package/package.json +1 -1
package/lib/command-poller.js
CHANGED
|
@@ -55,6 +55,9 @@ class CommandPoller {
|
|
|
55
55
|
case 'set_tags':
|
|
56
56
|
await this.handleSetTags(command);
|
|
57
57
|
break;
|
|
58
|
+
case 'set_file_input':
|
|
59
|
+
await this.handleSetFileInput(command);
|
|
60
|
+
break;
|
|
58
61
|
default:
|
|
59
62
|
// Other commands (scan_facebook_pages, etc.) handled by extension
|
|
60
63
|
console.log(`[commands] Skipping ${command.type} — handled by extension`);
|
|
@@ -443,6 +446,71 @@ class CommandPoller {
|
|
|
443
446
|
}
|
|
444
447
|
}
|
|
445
448
|
|
|
449
|
+
async handleSetFileInput(command) {
|
|
450
|
+
const { profile_id, file_path, selector, fallback_selector, url_match } = command.payload || {};
|
|
451
|
+
console.log(`[commands] Setting file input: ${selector} → ${file_path}`);
|
|
452
|
+
try {
|
|
453
|
+
const WebSocket = require('ws');
|
|
454
|
+
if (!this.nst) {
|
|
455
|
+
const apiKey = await this.api.getSetting('nst_api_key');
|
|
456
|
+
if (apiKey) this.nst = new NstManager(apiKey);
|
|
457
|
+
}
|
|
458
|
+
const browsersRes = await fetch('http://localhost:8848/api/v2/browsers', {
|
|
459
|
+
headers: { 'x-api-key': this.nst?.apiKey || '' },
|
|
460
|
+
});
|
|
461
|
+
const browser = ((await browsersRes.json())?.data || []).find(b => b.name === profile_id) || (await browsersRes.json())?.data?.[0];
|
|
462
|
+
if (!browser) throw new Error('No running browser');
|
|
463
|
+
|
|
464
|
+
const pagesRes = await fetch(`http://localhost:${browser.remoteDebuggingPort}/json/list`);
|
|
465
|
+
const pages = await pagesRes.json();
|
|
466
|
+
const targetPage = pages.find(p => p.type === 'page' && (!url_match || p.url?.includes(url_match)));
|
|
467
|
+
if (!targetPage) throw new Error(`No tab matching ${url_match}`);
|
|
468
|
+
|
|
469
|
+
const result = await new Promise((resolve, reject) => {
|
|
470
|
+
const ws = new WebSocket(targetPage.webSocketDebuggerUrl);
|
|
471
|
+
let msgId = 1;
|
|
472
|
+
function send(method, params = {}) {
|
|
473
|
+
const id = msgId++;
|
|
474
|
+
return new Promise((res, rej) => {
|
|
475
|
+
const handler = (data) => {
|
|
476
|
+
const msg = JSON.parse(data);
|
|
477
|
+
if (msg.id === id) { ws.removeListener('message', handler); msg.error ? rej(new Error(msg.error.message)) : res(msg.result); }
|
|
478
|
+
};
|
|
479
|
+
ws.on('message', handler);
|
|
480
|
+
ws.send(JSON.stringify({ id, method, params }));
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
ws.on('open', async () => {
|
|
485
|
+
try {
|
|
486
|
+
await send('DOM.enable');
|
|
487
|
+
const doc = await send('DOM.getDocument');
|
|
488
|
+
let node = await send('DOM.querySelector', { nodeId: doc.root.nodeId, selector });
|
|
489
|
+
if (!node?.nodeId && fallback_selector) {
|
|
490
|
+
node = await send('DOM.querySelector', { nodeId: doc.root.nodeId, selector: fallback_selector });
|
|
491
|
+
}
|
|
492
|
+
if (!node?.nodeId) { ws.close(); resolve({ success: false, error: 'Input not found' }); return; }
|
|
493
|
+
|
|
494
|
+
await send('DOM.setFileInputFiles', { nodeId: node.nodeId, files: [file_path] });
|
|
495
|
+
await send('Runtime.evaluate', {
|
|
496
|
+
expression: `document.querySelector('${selector.replace(/'/g, "\\'")}')?.dispatchEvent(new Event('change', { bubbles: true })); 'ok'`,
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
ws.close();
|
|
500
|
+
resolve({ success: true });
|
|
501
|
+
} catch (e) { ws.close(); resolve({ success: false, error: e.message }); }
|
|
502
|
+
});
|
|
503
|
+
ws.on('error', (e) => reject(e));
|
|
504
|
+
setTimeout(() => { ws.close(); reject(new Error('Timeout')); }, 15000);
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
await this.api.updateCommand(command._id, { status: result.success ? 'done' : 'failed', result, error: result.error || null });
|
|
508
|
+
} catch (err) {
|
|
509
|
+
console.error(`[commands] Set file input failed: ${err.message}`);
|
|
510
|
+
await this.api.updateCommand(command._id, { status: 'failed', error: err.message });
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
446
514
|
async handleCloseProfile(command) {
|
|
447
515
|
const { profile_id } = command.payload || {};
|
|
448
516
|
console.log(`[commands] Closing profile: ${profile_id}`);
|