Possible to check by a firmware/software whether Electron is powered only by battery?

I'm particularly fond of this answer. As it turns out, it's harder than you'd think to figure this out, for reasons explained in this thread: