/ clis / facebook / search.js
search.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  cli({
 3      site: 'facebook',
 4      name: 'search',
 5      description: 'Search Facebook for people, pages, or posts',
 6      domain: 'www.facebook.com',
 7      args: [
 8          { name: 'query', required: true, positional: true, help: 'Search query' },
 9          { name: 'limit', type: 'int', default: 10, help: 'Number of results' },
10      ],
11      columns: ['index', 'title', 'text', 'url'],
12      pipeline: [
13          { navigate: 'https://www.facebook.com' },
14          { navigate: { url: 'https://www.facebook.com/search/top?q=${{ args.query | urlencode }}', settleMs: 4000 } },
15          { evaluate: `(async () => {
16    const limit = \${{ args.limit }};
17    // Search results are typically in role="article" or role="listitem"
18    let items = document.querySelectorAll('[role="article"]');
19    if (items.length === 0) {
20      items = document.querySelectorAll('[role="listitem"]');
21    }
22    return Array.from(items)
23      .filter(el => el.textContent.trim().length > 20)
24      .slice(0, limit)
25      .map((el, i) => {
26        const link = el.querySelector('a[href*="facebook.com/"]');
27        const heading = el.querySelector('h2, h3, h4, strong');
28        return {
29          index: i + 1,
30          title: heading ? heading.textContent.trim().substring(0, 80) : '',
31          text: el.textContent.trim().replace(/\\s+/g, ' ').substring(0, 150),
32          url: link ? link.href.split('?')[0] : '',
33        };
34      });
35  })()
36  ` },
37      ],
38  });