UserScript_utilities
使い回す関数など
code:scrapbox.js
export const styleContainer = document.getElementById('dedicated-container');
export const createPageMenu = (menuTitle, imgOrUnicode, onClick = () => { }) => {
scrapbox.PageMenu.addMenu({
title: menuTitle,
image: !imgOrUnicode.startsWith('\\') ? imgOrUnicode : 'dummy.png',
onClick: onClick,
})
if (imgOrUnicode.startsWith('\\')) {
const PROJECT = scrapbox.Project.name;
const style = document.createElement('style');
style.setAttribute('name', menuTitle);
const selector = #${menuTitle}.tool-btn;
style.textContent = `
${selector}:hover {
text-decoration: none;
}
${selector}::before {
content: '${imgOrUnicode}';
font: 24px 'AppIcons';
font-weight: 400;
position: absolute;
width: 100%;
}
${selector} img {
opacity: 0 !important;
}`
styleContainer.append(style);
scrapbox.on('project:changed', () => {
if (scrapbox.Project.name !== PROJECT) {
style.remove();
} else {
styleContainer.append(style);
}
})
}
return {
addItem(title, onClick = () => { }, imgsrc = '') {
scrapbox.PageMenu(menuTitle).addItem({
title: title,
image: imgsrc,
onClick: onClick,
})
},
addSeparator() {
scrapbox.PageMenu(menuTitle).addSeparator();
},
}
}
code:common.js
export function copyToClipboard(txt) {
navigator.clipboard.writeText(txt)
.then(() => {
console.group('Copied to clipboard');
console.log(txt.length <= 100 ? txt : txt.slice(0, 100) + '…');
console.groupEnd();
})
.catch(err => {
console.log('Failed to copy', err);
});
return false;
}