/ clis / cursor / extract-code.js
extract-code.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  export const extractCodeCommand = cli({
 3      site: 'cursor',
 4      name: 'extract-code',
 5      description: 'Extract multi-line code blocks from the current Cursor conversation',
 6      domain: 'localhost',
 7      strategy: Strategy.UI,
 8      browser: true,
 9      args: [],
10      columns: ['Code'],
11      func: async (page) => {
12          const blocks = await page.evaluate(`
13        (function() {
14          // Find standard pre/code blocks
15          let elements = Array.from(document.querySelectorAll('pre code, .markdown-root pre'));
16          
17          // Fallback to Monaco editor content inside the UI
18          if (elements.length === 0) {
19            elements = Array.from(document.querySelectorAll('.monaco-editor'));
20          }
21          
22          // Generic fallback to any code tag that spans multiple lines
23          if (elements.length === 0) {
24            elements = Array.from(document.querySelectorAll('code')).filter(c => c.innerText.includes('\\n'));
25          }
26          
27          return elements.map(el => el.innerText || el.textContent || '').filter(text => text.trim().length > 0);
28        })()
29      `);
30          if (!blocks || blocks.length === 0) {
31              return [{ Code: 'No code blocks found in Cursor.' }];
32          }
33          return blocks.map((code) => ({ Code: code }));
34      },
35  });