FirefoxのツールバーにあるPocketボタンの代わりというかそれをキーボードでやりたい。
APIでのアクセスはログイン状態に左右されないのでボタンクリックしてみたらログイン求められて面倒というのも無くなる。
準備
xhを使っているがPocketのAPIを叩くためのconsumer_keyとaccess_tokenが入手できれば何でも良い。
- Pocket: Developer APIでCreate an ApplicationしてConsumer Keyを用意する
xh https://getpocket.com/v3/oauth/request consumer_key=xxxxxxxxxx redirect_uri=hoge
などしてcodeを入手するhttps://getpocket.com/auth/authorize?request_token=yyyyyyyyyy
にブラウザでアクセスして認可するxh https://getpocket.com/v3/oauth/authorize consumer_key=xxxxxxxxxx code=yyyyyyyyyy
してaccess_tokenを入手する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') } })()