popup
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/popup/script.ts
script.tsimport "../unlink_Popup_menu/script.ts";
script.tsimport type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts";
declare const scrapbox: Scrapbox;
// 選択範囲に下線を引く
scrapbox.PopupMenu.addButton({
title: '\uf0cd',
onClick: text => `[_ ${text}]`
});
script.tsscrapbox.PopupMenu.addButton({
title: "\uf698",
onClick: text => `[$ ${text}]`
});
script.tsimport "../長いタイトルを刻むページを簡単に作れるUserScript/main.ts";
script.tsimport { makeNewPage } from "../custom-new-page/mod.ts";
import {
newPageHook,
splittedLinkHook,
taskLineHook,
} from "../for-custom-new-page/mod.ts";
const hooks = [taskLineHook, newPageHook, splittedLinkHook];
scrapbox.PopupMenu.addButton({
title: (text) => {
const hook = makeNewPage({ project: scrapbox.Project.name, mode: "noopen", hooks });
if (!hook || hook.hookName.includes("default")) return "";
if (hook.hookName.includes("task")) return "\uf0c4\uf46d";
if (hook.hookName.includes("split")) return "\uf0c4\uf595";
return "\uf0c4";
},
onClick: () => {
const project = window.prompt("Create a new page at", scrapbox.Project.name);
if (!project) return;
makeNewPage({ project, mode: "noopen", hooks })?.();
return undefined;
},
});
末尾が拡張子で終わっていない画像URLをscrapboxに埋め込む
2023-02-06 05:36:43
URLを含むときのみ実行する
URLだけを画像記法に直す
script.ts{
/** from scrapbox-parser */
scrapbox.PopupMenu.addButton({
title: (text) => /(https?:\/\/[^\s]+)/g.test(text) ? "\uf03e" : "",
onClick: (text) => {
const newText = text.replace(/(https?:\/\/[^\s]+)/g, "[$1#.jpg]");
if (text === newText) return;
return newText;
},
});
}
script.ts//import '/api/code/scrapboxlab/いい感じにリンクするomakase-links/script.js';
2020-10-01 13:00:26 全然使わないので外した
script.ts// 選択された文字列をScrapboxプロジェクト内で検索する
// Scapbox検索ボックスを使ったときと同じ結果ページを開く
scrapbox.PopupMenu.addButton({
title: "S",
onClick: (text) => {
window.open(`https://scrapbox.io/${scrapbox.Project.name}/search/page?q=${text}`);
return undefined;
},
});
script.tsscrapbox.PopupMenu.addButton({
title: '\uf1a0',
onClick: text => {
// 記法を解除する
const keyword = text.split(/\n/)
.map(line => line.replace(/\[(.*)\]/g,'$1')).join('\n');
window.open(`https://www.google.com/search?q=${encodeURIComponent(keyword)}`);
return undefined;
}
});
2023-02-06 05:56:07 アイコンを変えた
script.tsscrapbox.PopupMenu.addButton({
title: "\uf1ab",
onClick: text => {
const isJapanese = (str: string) => [...str].some(char => char.charCodeAt(0) > 255);
// 記法を解除する
const keyword = text.split(/\n/)
.map(line => line.replace(/\[(.*)\]/g,'$1')).join('\n').trim().replace(/\s+/g,'+');
if (isJapanese(keyword)) {
window.open(`https://www.ldoceonline.com/search/japanese-english/direct/?q=${encodeURIComponent(keyword)}`);
} else {
window.open(`https://www.oxfordlearnersdictionaries.com/search/english/?q=${encodeURIComponent(keyword)}`);
}
return undefined;
}
});
選択範囲を検索するように変えた
script.tsscrapbox.PopupMenu.addButton({
title: '\uf002',
onClick: text => {
// 記法を解除する
const keyword = text.split(/\n/)
.map(line => line.replace(/\[(.*)\]/g,'$1')).join('\n');
window.open(`https://www.google.com/search?q=${encodeURIComponent(keyword)}+site:scrapbox.io`);
return undefined;
}
});
script.ts// インライン引用
scrapbox.PopupMenu.addButton({
title: '\uf10e',
onClick: text => `[" ${text}]`
});
indentを保持するように書き換えたい
2021-05-26 15:15:50 使わないので消した
script.ts.disabled(ts)// 選択範囲を引用にする
scrapbox.PopupMenu.addButton({
title: '\uf101',
onClick: text => text.split(/\n/).map(line => ` > ${line}`).join('\n')
});
script.ts // 拡張記法
// 選択した文字列にマーカー
scrapbox.PopupMenu.addButton({
title: '🖍',
onClick: text => `[[${text}]]`
});
script.tsimport '../選択範囲をMarkdown記法に変換してclip_boardにcopyするPopupMenu/script.js';
script.tsscrapbox.PopupMenu.addButton({
title: text => /\$([^$]+)\$/.test(text) ? 'L' : '',
onClick: text => {
const result = text.split(/\n/)
.map(line => line.replace(/\$([^$]+)\$/g,`[$ $1]`)).join('\n');
// テロメアが無駄に更新されるのを防ぐ
if(text == result) return;
return result;
}
});
script.tsimport {
convert,
convertGyazoURL,
convertScrapboxURL,
expandShortURL,
formatTweet,
formatURL,
formatWikipedia,
Middleware,
redirectGoogleSearch,
redirectWikiwand,
shortenAmazonURL,
} from "../takker99%2Fscrapbox-url-customizer/mod.ts";
import { formatNiconico } from "../nicothumb2img/middleware.ts";
import { insertText } from "../scrapbox-userscript-std/dom.ts";
// 毎回functionsを作るのは無駄なので、globalに保持しておく
const middlewares: Middleware[] = [
redirectGoogleSearch,
expandShortURL,
redirectGoogleSearch,
redirectWikiwand,
shortenAmazonURL,
convertScrapboxURL(),
convertGyazoURL,
formatTweet(),
formatWikipedia,
駅探のURLからいらないparametersを消す
script.ts (url) => {
if (url.hostname !== "ekitan.com") return url;
if (!/^\/transit\/(?:route|first|last)\//.test(url.pathname)) return url;
const dt = url.searchParams.get("dt");
const tm = url.searchParams.get("tm");
const sr = url.searchParams.get("sr");
// URL.searchParamsはsetterがないのでresetできない。代わりにURL.search経由でresetする
url.search = "";
if (dt) url.searchParams.append("dt", dt);
if (url.pathname.startsWith("/transit/route/")) {
if (tm) url.searchParams.append("tm", tm);
if (sr) url.searchParams.append("sr", sr);
}
return url;
},
script.ts (url) => {
if (url.host !== location.host) return url;
if (!url.pathname.startsWith("/api/code/")) return url;
const path = encodeURIComponent(`${url.origin}${url.pathname}`);
return `${url}`.endsWith(".svg")
? `[https://svg-hosting.vercel.app/api/svg?url=${path} ${url}]`
: `${url}`.endsWith(".mlt")
? `[https://aa2svg.vercel.app/svg/${path}?width=1000 ${url}]` : url;
},
script.ts (url) => {
if (url.hostname === "raw.githubusercontent.com") {
return `[https://code2svg.vercel.app/svg/${url.origin}${url.pathname}#.svg ${url}]`;
}
if (url.hostname !== "github.com") return url;
const [user, repo, filepath] =
url.pathname.match(/^\/([^\\]+)\/([^\\]+)\/blob\/(.+)$/)?.slice?.(1) ??
[];
if (!user || !repo || !filepath) return url;
const [, start, end] = url.hash.match(/L(\d+)-L(\d+)/) ??
url.hash.match(/L(\d+)/) ?? [];
return `[https://code2svg.vercel.app/svg/${
start && end ? `L${start}-${end}/` : start ? `L${start}/` : ""
}https://raw.githubusercontent.com/${user}/${repo}/${filepath}#.svg ${url}]`;
},
script.ts // githubはそのまま返す
(url) => url.hostname === "github.com" ? `${url}` : url,
formatNiconico,
formatURL(),
];
scrapbox.PopupMenu.addButton({
title: (text) => /https?:\/\/\S+/.test(text) ? "URL" : "",
onClick: (text) => {
const promise = convert(text, ...middlewares);
if (typeof promise === "string") {
// 文字列に違いがあるときのみ更新
return text === promise ? undefined : promise;
}
// 選択範囲に変換後の文字列を上書きする
// 変換中に選択範囲が変わると、ずれた位置に挿入されるので注意
promise.then((converted) => {
if (text === converted) return;
return insertText(converted);
});
return undefined;
},
});
script.tsimport '../複数行に打ち消し線を引くPopup Menu@0.3.0/script.js';
script.tsimport "../replace_text_UserScript再実装版/script.js";
script.tsimport { setup } from "../複数のリンクをまとめて置換するUserScript/example.ts";
setup(["takker", "takker-memex", "takker-private", "takker-books"]);
script.tsimport "../一旦改行を消して句点で改行し直すPopupMenu/script.js";
script.tsimport "../選択したGyazoのOCRテキストをコピーするUserScript/popupMenu.js";
script.tsimport "../選択したGyazo画像のtrimmingページを開くPopup_Menu/script.js";
script.tsimport "../Scrapbox記法をTeX記法に変換するPopupMenu/script.ts";
script.tsimport { startTalk } from "../scrapbox-keichobot/mod.ts";
import { textInput } from "../scrapbox-userscript-std/dom.ts";
if (!/mobile/i.test(navigator.userAgent)) {
const talk = (text: string, mode?: "normal" | "empathy_writing" | "KPT" | "value_hypothesis") => {
if (text.trim() === "") return undefined;
startTalk({ mode }).then((result) => {
if (!result) return;
const { ask, visible, onExit } = result;
const handleKeydown = (e: KeyboardEvent) => {
if (
!visible() || e.key !== "Enter" || !e.ctrlKey || e.altKey ||
e.shiftKey
) return;
e.preventDefault();
e.stopPropagation();
ask();
};
textInput()!.addEventListener("keydown", handleKeydown);
onExit(() => textInput()!.removeEventListener("keydown", handleKeydown));
});
};
scrapbox.PopupMenu.addButton({
title: () => "🤖",
onClick: (text) => talk(text),
});
/*
scrapbox.PopupMenu.addButton({
title: () => "🤖✍️",
onClick: (text) => talk(text, "empathy_writing"),
});
scrapbox.PopupMenu.addButton({
title: () => "🤖💡",
onClick: (text) => talk(text, "value_hypothesis"),
});*/
}
併用するstyle
style.css@import '../popupmenuのbuttonを非表示にする/style.css';
style.css@import '../PopupMenuを徹底的にアイコンにする/style.css';
なぜか httpRegExp
が deno check
でmodule can't be parsed errorを吐くので書き換えた
01:03:03 Typescriptに書き換え & middlewares追加
いくつかrefactoring & アイコン変更