I think a JavaScript Action would probably be easier, but a widget as you suggested should also work depending on your requirements. A JavaScript Action will work from a Nanofow so should be more flexible.
https://docs.mendix.com/refguide/javascript-actions/