9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
cli({
|
|
site: 'reddit',
|
|
name: 'comment',
|
|
access: 'write',
|
|
description: 'Post a comment on a Reddit post',
|
|
domain: 'reddit.com',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'post-id', type: 'string', required: true, positional: true, help: 'Post ID (e.g. 1abc123) or fullname (t3_xxx)' },
|
|
{ name: 'text', type: 'string', required: true, positional: true, help: 'Comment text' },
|
|
],
|
|
columns: ['status', 'message'],
|
|
func: async (page, kwargs) => {
|
|
if (!page)
|
|
throw new CommandExecutionError('Browser session required');
|
|
await page.goto('https://www.reddit.com');
|
|
const result = await page.evaluate(`(async () => {
|
|
try {
|
|
let postId = ${JSON.stringify(kwargs['post-id'])};
|
|
const urlMatch = postId.match(/comments\\/([a-z0-9]+)/);
|
|
if (urlMatch) postId = urlMatch[1];
|
|
const fullname = postId.startsWith('t3_') || postId.startsWith('t1_')
|
|
? postId : 't3_' + postId;
|
|
|
|
const text = ${JSON.stringify(kwargs.text)};
|
|
|
|
// Get modhash
|
|
const meRes = await fetch('/api/me.json', { credentials: 'include' });
|
|
const me = await meRes.json();
|
|
const modhash = me?.data?.modhash || '';
|
|
|
|
const res = await fetch('/api/comment', {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: 'parent=' + encodeURIComponent(fullname)
|
|
+ '&text=' + encodeURIComponent(text)
|
|
+ '&api_type=json'
|
|
+ (modhash ? '&uh=' + encodeURIComponent(modhash) : ''),
|
|
});
|
|
|
|
if (!res.ok) return { ok: false, message: 'HTTP ' + res.status };
|
|
const data = await res.json();
|
|
const errors = data?.json?.errors;
|
|
if (errors && errors.length > 0) {
|
|
return { ok: false, message: errors.map(e => e.join(': ')).join('; ') };
|
|
}
|
|
return { ok: true, message: 'Comment posted on ' + fullname };
|
|
} catch (e) {
|
|
return { ok: false, message: e.toString() };
|
|
}
|
|
})()`);
|
|
return [{ status: result.ok ? 'success' : 'failed', message: result.message }];
|
|
}
|
|
});
|