puhitaku
https://lh3.googleusercontent.com/a-/AOh14Ggrdw7fLFd2pNH6WDq_c050XwwdRWe2u2dT0mOXD-8=s96-c#.png
辞書博士(やめがきひろし)
code:style.css
/* 見出しボタンのアピアランスを揃える */
button#見出し:hover {
text-decoration: none;
}
button#見出し::before {
position: absolute;
content: '\f03a';
font: 900 20px 'Font Awesome 5 Free';
}
button#見出し img {
opacity: 0;
}
code:script.js
(() => {
// Ctrl+U でコメント + ユーザー名を挿入
const textArea = document.getElementById('text-input');
const inputEvent = {
event: document.createEvent('Event'),
dispatch: function(string) {
textArea.value = string;
textArea.dispatchEvent(this.event);
}
};
inputEvent.event.initEvent('input', true, true);
const onKeyDown = function(e){
if (e.key == "u" && e.ctrlKey) {
inputEvent.dispatch("comment puhitaku: "); }
}
document.addEventListener('keydown', onKeyDown)
// ページの見出しを作るUserScript
const indexes = function () {
scrapbox.PageMenu('見出し').removeAllItems()
for (let line of scrapbox.Page.lines) {
if (!line.section.start || line.title) continue
const image = line.nodes && getIconUrl(line.nodes)
const noIcon = !!image
const title = line.nodes ? renderPlainText(line.nodes, {noIcon}) : line.text
const onClick = () => location.hash = line.id
scrapbox.PageMenu('見出し').addItem({title, image, onClick})
}
}
function renderPlainText (node, options) {
if (node instanceof Array) return node.map(node => renderPlainText(node, options)).join('')
if (typeof node === 'string') return node
switch (node.type) {
case 'icon':
case 'strong-icon':
return options.noIcon ? ' ' : node.unit.page
case 'hashTag':
return ''
}
return renderPlainText(node.children, options)
}
function getIconUrl (node) {
if (/icon/.test(node.type)) {
return /api/pages/${node.unit.project || scrapbox.Project.name}/${node.unit.page}/icon
}
if (node instanceof Array) {
return node.map(getIconUrl).find(img => img)
}
return null
}
scrapbox.PageMenu.addMenu({
title: '見出し',
onClick: indexes
})
})()