Pocket(getpocket.com)に現在のタブを投げるTridactylのコマンドを作る

FirefoxツールバーにあるPocketボタンの代わりというかそれをキーボードでやりたい。
APIでのアクセスはログイン状態に左右されないのでボタンクリックしてみたらログイン求められて面倒というのも無くなる。

準備

xhを使っているがPocketのAPIを叩くためのconsumer_keyとaccess_tokenが入手できれば何でも良い。

  1. Pocket: Developer APIでCreate an ApplicationしてConsumer Keyを用意する
  2. xh https://getpocket.com/v3/oauth/request consumer_key=xxxxxxxxxx redirect_uri=hogeなどしてcodeを入手する
  3. https://getpocket.com/auth/authorize?request_token=yyyyyyyyyyにブラウザでアクセスして認可する
  4. xh https://getpocket.com/v3/oauth/authorize consumer_key=xxxxxxxxxx code=yyyyyyyyyyしてaccess_tokenを入手する
  5. xh https://getpocket.com/v3/add consumer_key=xxxxxxxxxx access_token=zzzzzzzzzz url=https://www.example.com/して200が返って来ればOK

実装

consumer_keyとaccess_tokenを書き換えた以下のJavaScriptをtridactylrcと同じ場所にaddPocket.jsとして保存する。
tridactylrcにbind ,p js -r addPocket.jsを追記して完成。

;(async () => {
  const response = await fetch('https://getpocket.com/v3/add', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      consumer_key: 'xxxxxxxxxx',
      access_token: 'zzzzzzzzzz',
      url: document.location.href,
      title: document.title || document.location.href,
    }),
  })
  if (response.status === 200) {
    fillcmdline_nofocus(`Added: ${document.title || document.location.href}`)
  } else {
    fillcmdline_nofocus('Request failed')
  }
})()