WebSocket経由でページ作成する
meganii.icon
ScrapboxのWebSocket経由でページを作成するサンプルを見つけられなかったで、pin-diary-6を見ながら試してみた
こいつ・・・動くぞ!となって、takker.iconさんスゴイと改めて感じている
https://gyazo.com/36d6a0eac747c0b163f7c6e5878c441f
code:typescript
import { Socket, disconnect, makeSocket, patch } from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-std/0.24.3/browser/websocket/mod.ts"
import { sleep } from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-std/0.24.3/sleep.ts"
import { useStatusBar } from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-std/0.24.3/mod.ts"
const project = 'hogehoge-project'
const title = 'Test page from websoket'
const { render, dispose } = useStatusBar()
let socket: Socket | undefined
try {
render(
{ type: "spinner" },
{ type: "text", text: Updating... },
)
socket = await makeSocket()
await patch(project, title, (lines) => [
lines0.text,
...lines.splice(1).map(line => line.text),
'updated'
], { socket })
} catch (e: unknown) {
console.error(e)
} finally {
if (socket) {
await disconnect(socket)
}
await sleep(1000)
dispose()
}