概要
ブックマークレットとはブックマークなどからJavaScriptを呼び出すことで簡易的なプログラムを実行できる機能のこと。今回はこれを使って今日投稿したMisskeyのノートの一覧を取得するブックマークレットを実装した。
環境
名前 | バージョン等 |
---|---|
Misskey | 2024.3.1-io.3 |
コード
2024-04-01 取得したノートを時間順に取得するためpromptにreverse()
追加。
const SERVER_DOMAIN = "ここにサーバーのドメインを入力";
const USER_API_KEY = "ここに「アカウントの情報を見る」を有効にしたAPIキーを入力";
const USER_ID = await fetch(`https://${SERVER_DOMAIN}/api/i`, {
method: 'POST',
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({ i: USER_API_KEY })
}).then(async (res) => (await res.json())["id"]);
const API_URL = `https://${SERVER_DOMAIN}/api/users/notes`;
let bodyObject = {
// APIドキュメントを読むことで細かい調整ができます
// https://misskey.io/api-doc#tag/users/operation/users/notes
userId: USER_ID,
withReplies: false,
withRenotes: true,
withChannelNotes: false,
limit: 100,
sinceDate: new Date(new Date().setHours(0,0,1,0)).getTime(),
untilDate: new Date(new Date().setHours(23,59,59,999)).getTime(),
allowPartial: false,
withFiles: false,
};
const OPTIONS = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(bodyObject)
};
const notes = await (await fetch(API_URL, OPTIONS)).json();
prompt("",notes.reverse().map(note => `https://${SERVER_DOMAIN}/notes/${note.id}`).join(" "));
ブックマークレット用
Info
自分がいるMisskeyサーバーを開いた状態で実行する必要があります。
Warning
APIキーは他人に見られないよう注意してください。
javascript: (async()=>{const SERVER_DOMAIN="ここにサーバーのドメインを入力";const USER_API_KEY="ここに「アカウントの情報を見る」を有効にしたAPIキーを入力";const USER_ID=await fetch(`https://${SERVER_DOMAIN}/api/i`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({i:USER_API_KEY})}).then(async(res)=>(await res.json())["id"]);const API_URL=`https://${SERVER_DOMAIN}/api/users/notes`;let bodyObject={userId:USER_ID,withReplies:!1,withRenotes:!0,withChannelNotes:!1,limit:100,sinceDate:new Date(new Date().setHours(0,0,1,0)).getTime(),untilDate:new Date(new Date().setHours(23,59,59,999)).getTime(),allowPartial:!1,withFiles:!1,};const OPTIONS={method:'POST',headers:{'Content-Type':'application/json',},body:JSON.stringify(bodyObject)};const notes=await(await fetch(API_URL,OPTIONS)).json();prompt("",notes.reverse().map(note=>`https://${SERVER_DOMAIN}/notes/${note.id}`).join(" "))})();