|
4 | 4 | [](https://github.com/takker99/scrapbox-userscript-std/actions?query=workflow%3Aci)
|
5 | 5 |
|
6 | 6 | UNOFFICIAL standard module for Scrapbox UserScript
|
| 7 | + |
| 8 | +## Getting Started |
| 9 | + |
| 10 | +This library serves as an unofficial standard library for developing Scrapbox |
| 11 | +userscripts. It provides a comprehensive set of utilities for interacting with |
| 12 | +Scrapbox's features, including REST API operations, browser interactions, and |
| 13 | +common utilities. |
| 14 | + |
| 15 | +### Installation |
| 16 | + |
| 17 | +1. Bundler Configuration This library is distributed through JSR (JavaScript |
| 18 | + Registry) and requires a bundler configuration. Follow these steps: |
| 19 | + |
| 20 | +a. Configure your bundler to use JSR: |
| 21 | + |
| 22 | +- For esbuild: Add JSR to your import map |
| 23 | +- For other bundlers: Refer to your bundler's JSR integration documentation |
| 24 | + |
| 25 | +b. Import the library: |
| 26 | + |
| 27 | +```typescript |
| 28 | +// Import commonly used functions |
| 29 | +import { getPage } from "jsr:@cosense/std/rest"; |
| 30 | +import { parseAbsoluteLink } from "jsr:@cosense/std"; |
| 31 | + |
| 32 | +// Import specific modules (recommended) |
| 33 | +import { getLinks } from "jsr:@cosense/std/rest"; |
| 34 | +import { press } from "jsr:@cosense/std/browser/dom"; |
| 35 | +import { getLines } from "jsr:@cosense/std/browser/dom"; |
| 36 | +``` |
| 37 | + |
| 38 | +2. Module Organization The library is organized into the following main modules: |
| 39 | + |
| 40 | +- `rest/`: API operations for Scrapbox REST endpoints |
| 41 | + - Page operations |
| 42 | + - Project management |
| 43 | + - User authentication |
| 44 | +- `browser/`: Browser-side operations |
| 45 | + - DOM manipulation |
| 46 | + - WebSocket communication |
| 47 | + - Event handling |
| 48 | +- Core utilities: |
| 49 | + - `title`: Title parsing and formatting |
| 50 | + - `parseAbsoluteLink`: External link analysis |
| 51 | + - Additional helper functions |
| 52 | + |
| 53 | +## Examples |
| 54 | + |
| 55 | +### Basic Usage |
| 56 | + |
| 57 | +1. Retrieving Page Information |
| 58 | + |
| 59 | +```typescript |
| 60 | +// Get page content and metadata |
| 61 | +import { getPage } from "jsr:@cosense/std/rest"; |
| 62 | + |
| 63 | +const result = await getPage("projectName", "pageName"); |
| 64 | +if (result.ok) { |
| 65 | + const page = result.val; |
| 66 | + console.log("Page title:", page.title); |
| 67 | + console.log("Page content:", page.lines.map((line) => line.text)); |
| 68 | + console.log("Page descriptions:", page.descriptions.join("\n")); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +2. DOM Operations |
| 73 | + |
| 74 | +```typescript |
| 75 | +// Interact with the current page's content |
| 76 | +import { getLines, press } from "jsr:@cosense/std/browser/dom"; |
| 77 | + |
| 78 | +// Get all lines from the current page |
| 79 | +const lines = getLines(); |
| 80 | +console.log(lines.map((line) => line.text)); |
| 81 | + |
| 82 | +// Simulate keyboard input |
| 83 | +await press("Enter"); // Add a new line |
| 84 | +await press("Tab"); // Indent the line |
| 85 | +``` |
| 86 | + |
| 87 | +3. External Link Analysis |
| 88 | + |
| 89 | +```typescript |
| 90 | +// Parse external links (YouTube, Spotify, etc.) |
| 91 | +import { parseAbsoluteLink } from "jsr:@cosense/std"; |
| 92 | +import type { LinkNode } from "@progfay/scrapbox-parser"; |
| 93 | + |
| 94 | +// Create a link node with absolute path type |
| 95 | +const link = { |
| 96 | + type: "link" as const, |
| 97 | + pathType: "absolute" as const, |
| 98 | + href: "https://www.youtube.com/watch?v=xxxxx", |
| 99 | + content: "", |
| 100 | + raw: "[https://www.youtube.com/watch?v=xxxxx]", |
| 101 | +} satisfies LinkNode & { pathType: "absolute" }; |
| 102 | + |
| 103 | +// Parse and handle different link types |
| 104 | +const parsed = parseAbsoluteLink(link); |
| 105 | +if (parsed?.type === "youtube") { |
| 106 | + // Handle YouTube links |
| 107 | + console.log("YouTube video ID:", parsed.href.split("v=")[1]); |
| 108 | + const params = new URLSearchParams(parsed.href.split("?")[1]); |
| 109 | + const start = params.get("t"); |
| 110 | + if (start) { |
| 111 | + console.log("Video timestamp:", start); |
| 112 | + } |
| 113 | +} else if (parsed?.type === "spotify") { |
| 114 | + // Handle Spotify links |
| 115 | + const match = parsed.href.match(/spotify\.com\/track\/([^?]+)/); |
| 116 | + if (match) { |
| 117 | + console.log("Spotify track ID:", match[1]); |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Important Notes |
| 123 | + |
| 124 | +- This library requires a bundler for use in userscripts |
| 125 | +- Full TypeScript support with type definitions included |
| 126 | +- Comprehensive error handling with type-safe responses |
| 127 | +- For more examples and use cases, see the |
| 128 | + [Examples](https://github.com/takker99/scrapbox-userscript-std/tree/main/examples) |
| 129 | + directory |
| 130 | + |
| 131 | +### Additional Resources |
| 132 | + |
| 133 | +- [JSR Package Page](https://jsr.io/@cosense/std) |
| 134 | +- [API Documentation](https://jsr.io/@cosense/std/doc) |
| 135 | +- [GitHub Repository](https://github.com/takker99/scrapbox-userscript-std) |
0 commit comments