9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
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
52 lines
2.0 KiB
JavaScript
52 lines
2.0 KiB
JavaScript
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
cli({
|
|
site: 'reddit',
|
|
name: 'subscribe',
|
|
access: 'write',
|
|
description: 'Subscribe or unsubscribe to a subreddit',
|
|
domain: 'reddit.com',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'subreddit', type: 'string', required: true, positional: true, help: 'Subreddit name (e.g. python)' },
|
|
{ name: 'undo', type: 'boolean', default: false, help: 'Unsubscribe instead of subscribe' },
|
|
],
|
|
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 sub = ${JSON.stringify(kwargs.subreddit)};
|
|
if (sub.startsWith('r/')) sub = sub.slice(2);
|
|
|
|
const undo = ${kwargs.undo ? 'true' : 'false'};
|
|
const action = undo ? 'unsub' : 'sub';
|
|
|
|
// 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/subscribe', {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: 'sr_name=' + encodeURIComponent(sub)
|
|
+ '&action=' + action
|
|
+ (modhash ? '&uh=' + encodeURIComponent(modhash) : ''),
|
|
});
|
|
|
|
if (!res.ok) return { ok: false, message: 'HTTP ' + res.status };
|
|
const label = undo ? 'Unsubscribed from' : 'Subscribed to';
|
|
return { ok: true, message: label + ' r/' + sub };
|
|
} catch (e) {
|
|
return { ok: false, message: e.toString() };
|
|
}
|
|
})()`);
|
|
return [{ status: result.ok ? 'success' : 'failed', message: result.message }];
|
|
}
|
|
});
|