Be careful when doing this. If left unchecked this could very easily start a fire. I did this with an old Nexus 5 and it only took a couple of days before I had a spicy pillow.
Although, this probably works well for a device with a removable battery and that can be powered by USB alone.
If the hardware and software are designed and tested for it, then yes.
Otherwise you may run into various issues caused by phone self-limiting power it takes from USB port. Some kind of negotiation needs to happen before the phone can safely raise the input power limits. If it needs more than currently allowed, it will just poweroff immediately, instead of satisfying the excess need from the battery.
Or a timer switch. Christmas lights 6 hours on 18 off power timer switch. Or a smart switch. Also make sure there is no cushy things like pillow or clothes or stuff around phone.
For this scenario I'd like to buy a device powered by POE, with USB-Eth, some free USB-Ports with PPPC, and some contraption, that goes through a hole in the back of the phone to the battery connector inside.
I had assembled something like that for my old SGS2 alarm clock out of a POE-5V splitter, an USB otg adapter, an Uhubctl-compatible USB-HUB powered by the POE-Splitter, and assorted cabling including a lead to the internal battery terminal with the top bar of an old battery (It contains some electronics). UGLY. Does anybody know a pre-made device like that?
How does this comment come up whenever IP cameras or dash cams are mentioned?
You certainly "can use a old phone as a webcam" and it will be "better quality". That's because it's a device designed to view commercial media, text, etc from your hand while mobile, connected to a network 24/7.
As an analogy, you have a 40 year old Ferrari in pristine condition that really doesn't have much work value, but still a fast classic car. also you daily drive a 5 year old cheap truck. if your daily breaks down, would you replace it with your Ferrari which is certainly "faster" and better quality?
A little example using my old galaxy s11. ( its connected to a pc using usb, airplane mode, no internet )
https://www.youtube.com/watch?v=2CDE7PsLwzk