generated at
空リンクを置換するPopup Menu
ややグレーゾーンかも
どういうときに使うんでしょうか?yosider
ああーなるほどです
あれAPIあったんだyosider
リンク置換するときに通信とにらめっこしてたら見つけたtakker

注意
中身のあるページに対して使うと、リンクは置換されるがタイトルが置換されない

js
import('/api/code/programming-notes/空リンクを置換するPopup_Menu/script.js');
script.js
scrapbox.PopupMenu.addButton({ title: text => getLink(text) ? 'update links' : '.', onClick: text => { const link = getLink(text); if (!link) return; const newLink = window.prompt(`Replace "${link}" to`, link)?.replace?.(/[\[\]\n]/g, ' ') ?? ''; if (newLink === '') return; updateLinks(link, newLink); return; } })

script.js
function getLink(text) { return text.match(/\[([^\[!"#%&'()\*\+,\-\.\/\{\|\}<>_~][^\[\]]*)\]/)?.[1]; }

script.js
async function updateLinks(from, to, {project} = {}) { project = project ?? scrapbox.Project.name; const res = await fetch(`/api/pages/${project}/replace/links`, { method: 'POST', headers: { 'Content-Type': 'application/json;charset=utf-8', 'X-CSRF-TOKEN': window._csrf, }, body: JSON.stringify({from, to}), }); return await res.json(); }