簡単家計簿2
code:script.js
const id = "userscript-kakeibo";
let enable = false;
const updateKakeibo = () => {
const total = scrapbox.Page.lines
.map(({ text }) => {
const money = Number(text.split(/\s/).pop());
return isNaN(money) ? 0 : money;
})
.reduce((amount, money) => amount + money);
const viewer = document.getElementById(id);
if (viewer) {
viewer.textContent = ${total}円;
} else {
document.getElementById("editor").insertAdjacentHTML(
"beforeend",
`<div
id="${id}"
class="total"
style="text-align:right;font-weight:bold;font-size:1.5em;border-top:lightblue solid 2px;line-height:1.8">
${total}円
</div>`,
);
}
};
const onPageChanged = () => {
if (targetTitles.includes(scrapbox.Page.title)) {
if (enable) return;
updateKakeibo();
scrapbox.addListener("lines:changed", updateKakeibo);
enable = true;
} else {
scrapbox.removeListener("lines:changed", updateKakeibo);
}
};
onPageChanged();
scrapbox.addListener("page:changed", onPageChanged);