Last active
November 26, 2022 07:43
-
-
Save nodaguti/7f2f97294f1a98b571e4c7b7f43aa34b to your computer and use it in GitHub Desktop.
Apply pre-defined rules to automatically categorise your expenses on MoneyForward
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
const rules = [ | |
{ | |
content: /kindle/i, | |
note: /^Amazon\.co\.jp$/, | |
kind: '本', | |
}, | |
]; | |
async function find(selector, parent = document) { | |
while(true) { | |
const el = parent.querySelector(selector); | |
if (el) { | |
return el; | |
} | |
await new Promise((r) => window.requestAnimationFrame(r)); | |
} | |
} | |
async function text(selector, parent = document) { | |
const el = parent.querySelector(selector); | |
return el?.textContent.trim() ?? ''; | |
} | |
function click(element) { | |
const ev = new MouseEvent('click', { bubbles: true, cancelable: true, view: window }); | |
element.dispatchEvent(ev); | |
} | |
async function main() { | |
const list = document.querySelectorAll('.transaction_list'); | |
for(const item of list) { | |
const content = await text('.content', item); | |
const note = await text('.note', item); | |
const rule = rules.find((rule) => rule.content.test(content) && rule.note.test(note)); | |
if (!rule) { | |
continue; | |
} | |
const currentKind = await text('.mctg', item); | |
if (currentKind === rule.kind) { | |
continue; | |
} | |
const dropdownToggle = await find('.dropdown-toggle', item); | |
click(dropdownToggle); | |
const dropdown = await find('.main_menu', item); | |
const dropdownItem = Array.from(dropdown.querySelectorAll('.m_c_name')).find((el) => el.textContent === rule.kind); | |
if (!dropdownItem) { | |
console.error(`${rule.kind} is not found in`, dropdown); | |
continue; | |
} | |
// Prevent the dropdown from being removed by MoneyForward's code | |
dropdown.classList.remove('main_menu'); | |
dropdown.classList.add('main_menu_wip'); | |
click(dropdownItem); | |
console.log(`${content} (${note}) -> ${rule.kind}`); | |
} | |
Array.from(document.querySelectorAll('.main_menu_wip')).forEach((el) => el.remove()); | |
} | |
main().then(() => console.log('Finished!')).catch(console.error); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment