generated at
UserScriptからScrapboxのページのpinを付け外し
してみる

原理
Pinの付け外し
WebSocketを通じて行われているので、それをemulateすればいい
Pinしたときの値
Number.MAX_SAFE_INTEGER - Math.floor(Date.now() / 1000) を使う

試してみる
14:22:16 成功!
test.js
import {createWS} from "https://scrapbox.io/api/code/takker/%E7%B0%A1%E5%8D%98websocket_promise_wrapper/mod.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();

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, }, }, ])}`; }

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, }, }, ])}`; }

test.js
// データの取得 async function getData(project, title) { const res = await Promise.all([ fetch(`https://scrapbox.io/api/pages/${project}/${ encodeURIComponent(title) }`), fetch(`https://scrapbox.io/api/projects/${project}`), fetch(`https://scrapbox.io/api/users/me`), ]); const {commitId, pin, id: pageId} = await res[0].json(); const {id: projectId} = await res[1].json(); const {id: userId} = await res[2].json(); return {pin, commitId, pageId, userId, projectId}; }

#2021-09-17 13:56:41