Automaメモ

automa

 

Automaで代入した変数をJavascriptで取得する関数

Automaのブロックで代入した変数をJavascript内で取得する関数

automaRefData('variables', 'keywords');

 

カンマ区切りのキーワードを配列として扱う関数

const keywords = automaRefData('variables', 'keywords');
automaSetVariable(
  'keywords', 
  keywords.split(',').map((keyword) => keyword.trim())
)

 

変数に入れたマークダウンを書式を維持して貼り付ける

const markdownContent = automaRefData('variables', 'post_data');

// エディタ要素を取得
const editor = document.querySelector('div.ProseMirror');

if (!editor) {
    throw new Error('エディタ要素が見つかりません');
}

editor.focus();

// 既存の内容をクリア
document.execCommand('selectAll', false, null);
document.execCommand('delete', false, null);

// 貼り付けイベントを模倣
const clipboardEvent = new ClipboardEvent('paste', {
    bubbles: true,
    cancelable: true,
    clipboardData: new DataTransfer(),
});

clipboardEvent.clipboardData.setData('text/plain', markdownContent);
editor.dispatchEvent(clipboardEvent);

 

Threadsに改行を反映させながら貼り付ける

const tweet = automaRefData('variables', 'tweet');
const editor = document.querySelector('div[contenteditable="true"]');

if (editor) {
  editor.focus();

  // 内容クリア
  document.execCommand('selectAll', false, null);
  document.execCommand('delete', false, null);

  // 行単位で処理
  tweet.split("\n").forEach((line, index, array) => {
    if (line) {
      document.execCommand('insertText', false, line);
    }

    // 改行が必要なときだけ Enter を挿入
    if (index < array.length - 1) {
      editor.dispatchEvent(new KeyboardEvent('keydown', {
        key: 'Enter',
        code: 'Enter',
        keyCode: 13,
        which: 13,
        bubbles: true,
        cancelable: true
      }));
      editor.dispatchEvent(new KeyboardEvent('keyup', {
        key: 'Enter',
        code: 'Enter',
        keyCode: 13,
        which: 13,
        bubbles: true,
        cancelable: true
      }));
    }
  });

  // 最後に状態更新のためのスペースを入力(ポストボタン有効化)
  ['keydown', 'keyup'].forEach(type => {
    editor.dispatchEvent(new KeyboardEvent(type, {
      key: ' ',
      code: 'Space',
      keyCode: 32,
      which: 32,
      bubbles: true,
      cancelable: true
    }));
  });
}

 

Threadsのログアウトボタンを探してクリックさせる

// 「ログアウト」というテキストを持つ <span> を探す
const logoutSpan = Array.from(document.querySelectorAll('span'))
  .find(span => span.textContent.trim() === 'ログアウト');

// 親要素でクリック可能なものを探してクリック
if (logoutSpan) {
  const clickable = logoutSpan.closest('[role="button"]');
  if (clickable) {
    clickable.click();
    console.log('ログアウトボタンをクリックしました。');
  } else {
    console.warn('クリック可能な親要素が見つかりませんでした。');
  }
} else {
  console.warn('ログアウトというテキストの要素が見つかりませんでした。');
}