/ clis / notion / write.js
write.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  export const writeCommand = cli({
 3      site: 'notion',
 4      name: 'write',
 5      description: 'Append text content to the currently open Notion page',
 6      domain: 'localhost',
 7      strategy: Strategy.UI,
 8      browser: true,
 9      args: [{ name: 'text', required: true, positional: true, help: 'Text to append to the page' }],
10      columns: ['Status'],
11      func: async (page, kwargs) => {
12          const text = kwargs.text;
13          // Focus the page body and move to the end
14          await page.evaluate(`
15        (function(text) {
16          // Find the editable area in Notion
17          const editables = document.querySelectorAll('.notion-page-content [contenteditable="true"], [class*="page-content"] [contenteditable="true"]');
18          let target = editables.length > 0 ? editables[editables.length - 1] : null;
19          
20          if (!target) {
21            // Fallback: just find any contenteditable
22            const all = document.querySelectorAll('[contenteditable="true"]');
23            target = all.length > 0 ? all[all.length - 1] : null;
24          }
25          
26          if (!target) throw new Error('Could not find editable area in Notion page');
27          
28          target.focus();
29          // Move to end
30          const sel = window.getSelection();
31          sel.selectAllChildren(target);
32          sel.collapseToEnd();
33          
34          document.execCommand('insertText', false, text);
35        })(${JSON.stringify(text)})
36      `);
37          await page.wait(0.5);
38          return [{ Status: 'Text appended successfully' }];
39      },
40  });