Drawingを押すと外部serviceを立ち上げるUserScript
動機
とりあえず新しいタブで開けるようにする
本当はModal windowでその場で開けるようにしたいが、それは今後の課題にしよう
dependency
jsimport {jack} from '/api/code/programming-notes/Drawingを押すと外部serviceを立ち上げるUserScript/script.js';
jack('https://kakeru.app', {name: 'Open Kakeru'});
script.jsexport function jack(url, {name = 'Drawing'} = {}) {
scrapbox.PageMenu.addItem({
title: name,
onClick: () => window.open(url),
});
}
mobile版だと click
eventの乗っ取りが出来ないので、代わりにDOMを差し替えている
2021-03-29 11:46:02 Reactのrenderでページ遷移時に書き換わってしまうのでうまく動かない
script.js.old(js)import {scrapboxDOM} from '../scrapbox-dom-accessor/script.js';
import {a} from '../easyDOMgenerator/script.js';
let running = false;
export function jack(url, {name = 'Drawing'} = {}) {
if (running) return;
const observer = new MutationObserver(() => replace(url, name));
observer.observe(scrapboxDOM.pageEditMenu.nextElementSibling, {childList: true, subtree: true});
running = true;
}
function replace(url, name) {
const drawing = [...scrapboxDOM.pageEditMenu.nextElementSibling.getElementsByTagName('a')]
.find(a_ => a_.textContent.trim() === 'Drawing');
const newNode = a({tabindex: 0, role: 'menuitem', 'data-jacked': true}, name);
newNode.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
window.open(url);
});
drawing.parentNode.replaceChild(newNode, drawing);
}