Cosmological constraints on exotic injection of electromagnetic energy