generated at
抜き書きNew Page
使い方
script.js
import { copy } from "./mod.js"; if (navigator.userAgent.toLowerCase().includes("mobile")) { scrapbox.PageMenu.addMenu({ title: "抜き書き", image: "/assets/img/favicon/apple-touch-icon.png", onClick: () => copy(), }); } else { scrapbox.PopupMenu.addButton({ title: "抜き書き", onClick: () => copy(), }); }

開発用コマンド
$ deno check -r=https://scrapbox.io --remote https://scrapbox.io/api/code/unnamed-project/抜き書きNew_Page/mod.ts
documents
mod.ts
import { caret, open, OpenOptions, getLineId, } from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-std/0.14.1/browser/dom/mod.ts"; import { getIndentCount } from "https://raw.githubusercontent.com/takker99/scrapbox-userscript-std/0.14.1/text.ts"; import type { Scrapbox } from "https://raw.githubusercontent.com/scrapbox-jp/types/0.3.3/userscript.ts"; declare const scrapbox: Scrapbox; export const copy = (init?: Omit<OpenOptions, "body" | "id"> & { project?: string; }) => { if (scrapbox.Layout !== "page") return; const project = init?.project ?? scrapbox.Project.name; const { selectedText, selectionRange: { start, end }, } = caret(); const id = getLineId(Math.min(start.line, end.line)); const from = `${ project !== scrapbox.Project.name ? `/${scrapbox.Project.name}/` : "" }${scrapbox.Page.title}${id ? `#${id}` : ""}`; const lines = selectedText.split("\n"); const title = lines[0] .replaceAll("[", "") .replaceAll("]", "") .replaceAll("`", ""); const minIndentCount = Math.min(...lines.map((text) => getIndentCount(text))); const body = [ `from [${from}]`, ...lines.map((text) => text.slice(minIndentCount)), ].join("\n"); open(project, title, { body, ...(init ?? {}) }); };

mod.js
var l=e=>e==null,s=e=>typeof e=="string",c=e=>typeof e=="number";var g=(e,t)=>{if(!Array.isArray(e))throw new TypeError(`"${t}" must be an array but actual is "${e}"`)};var x=(e,t)=>{if(!(e instanceof HTMLTextAreaElement))throw new TypeError(`"${t}" must be HTMLTextAreaElement but actual is "${e}"`)};var i=()=>{let e=document.getElementById("text-input");if(!!e)return x(e,"textarea#text-input"),e};var m=e=>e.match(/^(\s*)/)?.[1]?.length??0;function p(e){if(l(e))return;if(c(e))return M(e)?.id;if(s(e))return e.startsWith("L")?e.slice(1):e;if(e.classList.contains("line"))return e.id.slice(1);let t=e.closest(".line");if(t)return t.id.slice(1)}function M(e){if(l(e))return;if(c(e))return h()[e];let t=p(e);return t?h().find(n=>n.id===t):void 0}function h(){return g(scrapbox.Page.lines,"scrapbox.Page.lines"),scrapbox.Page.lines}function f(){let e=i();if(!e)throw Error("#text-input is not found.");let t=Object.keys(e).find(n=>n.startsWith("__reactFiber"));if(!t)throw Error('div.cursor must has the property whose name starts with "__reactFiber"');return e[t].return.return.stateNode.props}var E=e=>[...e].map((t,n)=>t===" "?"_":!A.includes(t)||n===e.length-1&&D.includes(t)?encodeURIComponent(t):t).join(""),A='@$&+=:;",',D=':;",';var w=(e,t,n)=>{let r=new URL(`/${e}/${E(t)}`,location.href);if(n?.body&&(r.search=`?body=${encodeURIComponent(n.body)}`),n?.id&&(r.hash=`#${n.id}`),n?.newTab!==!1&&(n?.newTab===!0||e!==scrapbox.Project.name)){window.open(r);return}if(n?.reload!==!1&&(n?.reload===!0||e!==scrapbox.Project.name)){window.open(r,"_self");return}let o=document.createElement("a");o.href=r.toString(),document.body.append(o),o.click(),o.remove()};var st=e=>{if(scrapbox.Layout!=="page")return;let t=e?.project??scrapbox.Project.name,{selectedText:n,selectionRange:{start:r,end:o}}=f(),d=p(Math.min(r.line,o.line)),b=`${t!==scrapbox.Project.name?`/${scrapbox.Project.name}/`:""}${scrapbox.Page.title}${d?`#${d}`:""}`,a=n.split(` `),y=a[0].replaceAll("[","").replaceAll("]","").replaceAll("`",""),T=Math.min(...a.map(u=>m(u))),L=[`from [${b}]`,...a.map(u=>u.slice(T))].join(` `);w(t,y,{body:L,...e??{}})};export{st as copy};