like.js
1 import { CommandExecutionError } from '@jackwener/opencli/errors'; 2 import { cli, Strategy } from '@jackwener/opencli/registry'; 3 cli({ 4 site: 'twitter', 5 name: 'like', 6 description: 'Like a specific tweet', 7 domain: 'x.com', 8 strategy: Strategy.UI, // Utilizes internal DOM flows for interaction 9 browser: true, 10 args: [ 11 { name: 'url', type: 'string', required: true, positional: true, help: 'The URL of the tweet to like' }, 12 ], 13 columns: ['status', 'message'], 14 func: async (page, kwargs) => { 15 if (!page) 16 throw new CommandExecutionError('Browser session required for twitter like'); 17 await page.goto(kwargs.url); 18 await page.wait({ selector: '[data-testid="primaryColumn"]' }); // Wait for tweet to load completely 19 const result = await page.evaluate(`(async () => { 20 try { 21 // Poll for the tweet to render 22 let attempts = 0; 23 let likeBtn = null; 24 let unlikeBtn = null; 25 26 while (attempts < 20) { 27 unlikeBtn = document.querySelector('[data-testid="unlike"]'); 28 likeBtn = document.querySelector('[data-testid="like"]'); 29 30 if (unlikeBtn || likeBtn) break; 31 32 await new Promise(r => setTimeout(r, 500)); 33 attempts++; 34 } 35 36 // Check if it's already liked 37 if (unlikeBtn) { 38 return { ok: true, message: 'Tweet is already liked.' }; 39 } 40 41 if (!likeBtn) { 42 return { ok: false, message: 'Could not find the Like button on this tweet after waiting 10 seconds. Are you logged in?' }; 43 } 44 45 // Click Like 46 likeBtn.click(); 47 await new Promise(r => setTimeout(r, 1000)); 48 49 // Verify success by checking if the 'unlike' button appeared 50 const verifyBtn = document.querySelector('[data-testid="unlike"]'); 51 if (verifyBtn) { 52 return { ok: true, message: 'Tweet successfully liked.' }; 53 } else { 54 return { ok: false, message: 'Like action was initiated but UI did not update as expected.' }; 55 } 56 } catch (e) { 57 return { ok: false, message: e.toString() }; 58 } 59 })()`); 60 if (result.ok) { 61 // Wait for the like network request to be processed 62 await page.wait(2); 63 } 64 return [{ 65 status: result.ok ? 'success' : 'failed', 66 message: result.message 67 }]; 68 } 69 });