/ clis / facebook / notifications.js
notifications.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  cli({
 3      site: 'facebook',
 4      name: 'notifications',
 5      description: 'Get recent Facebook notifications',
 6      domain: 'www.facebook.com',
 7      args: [
 8          { name: 'limit', type: 'int', default: 15, help: 'Number of notifications' },
 9      ],
10      columns: ['index', 'text', 'time'],
11      pipeline: [
12          { navigate: { url: 'https://www.facebook.com/notifications', settleMs: 3000 } },
13          { evaluate: `(() => {
14    const limit = \${{ args.limit }};
15    const items = document.querySelectorAll('[role="listitem"]');
16    return Array.from(items)
17      .filter(el => el.querySelectorAll('a').length > 0)
18      .slice(0, limit)
19      .map((el, i) => {
20        const raw = el.textContent.trim().replace(/\\s+/g, ' ');
21        // Remove leading "未读" and trailing "标记为已读"
22        const cleaned = raw.replace(/^未读/, '').replace(/标记为已读$/, '').replace(/^Unread/, '').replace(/Mark as read$/, '').trim();
23        // Try to extract time (last segment like "11小时", "5天", "1周")
24        const timeMatch = cleaned.match(/(\\d+\\s*(?:分钟|小时|天|周|个月|minutes?|hours?|days?|weeks?|months?))\\s*$/);
25        const time = timeMatch ? timeMatch[1] : '';
26        const text = timeMatch ? cleaned.slice(0, -timeMatch[0].length).trim() : cleaned;
27        return {
28          index: i + 1,
29          text: text.substring(0, 150),
30          time: time || '-',
31        };
32      });
33  })()
34  ` },
35      ],
36  });