UserScriptからScrapboxのページのpinを付け外し
してみる
原理
Pinの付け外し
Pinしたときの値
Number.MAX_SAFE_INTEGER - Math.floor(Date.now() / 1000)を使う
試してみる
14:22:16 成功!
code:test.js
const {close, send, receive} = await createWS("wss://scrapbox.io/socket.io/?EIO=4&transport=websocket");
// 通信を保持する
for await (const res of receive()) {
await send(40);
break;
}
const data = await getData(scrapbox.Project.name, scrapbox.Page.title)
if (data.pin > 0) {
await send(unpinRequest(data));
} else {
await send(pinRequest(data));
}
await close();
code:test.js
// Pinをつける
function pinRequest(data) {
const {pin, commitId, pageId, userId, projectId} = data;
return `422${JSON.stringify([
"socket.io-request",
{
method: "commit",
data: {
kind: "page",
parentId: commitId,
changes:[{
pin: Number.MAX_SAFE_INTEGER - Math.floor(Date.now() / 1000),
}],
cursor: null,
pageId,
userId,
projectId,
freeze:true,
},
},
])}`;
}
code:test.js
// Pinを外す
function unpinRequest(data) {
const {pin, commitId, pageId, userId, projectId} = data;
return `423${JSON.stringify([
"socket.io-request",
{
method: "commit",
data: {
kind: "page",
parentId: commitId,
changes:[{
pin: 0,
}],
cursor: null,
pageId,
userId,
projectId,
freeze:true,
},
},
])}`;
}
code:test.js
// データの取得
async function getData(project, title) {
const res = await Promise.all([
encodeURIComponent(title)
}`),
fetch(https://scrapbox.io/api/projects/${project}),
fetch(https://scrapbox.io/api/users/me),
]);
const {commitId, pin, id: pageId} = await res0.json(); const {id: projectId} = await res1.json(); const {id: userId} = await res2.json(); return {pin, commitId, pageId, userId, projectId};
}