In the absence of gravity, the only 2 forces on the dielectric slab are the electrostatic force pulling it into the capacitor and the external force which prevents it from accelerating and gaining KE.
However, rather than find an expression for the electrostatic force and integrating it to find the work done, it is easier to use Conservation of Energy, because you are only interested in the initial and final states, not what happens in between.
The question you found on Stack Exchange does actually answer your question :
The energy stored in the capacitor increases by $E$. The charge stored increases, because of work done by the battery, but this work done equals $2E$. The battery does more work than the increase in energy stored in the capacitor. The lost energy appears as either (i) kinetic energy of the slab which oscillates inside the capacitor, or (ii) heat dissipated by the current flowing back and forth between the plates through the battery, or (iii) electromagnetic energy radiated by the accelerating charges, or (iv) mechanical work done against an external agent which prevents the slab from accelerating, and thereby removes the kinetic energy as soon as it is created.
As the Stack Exchange answer points out, the slab is pulled into the capacitor by electrostatic forces whether the plates are isolated or connected via a battery. So the capacitor/battery does work on the external agent in both cases. The external agent does not do +ve work on the capacitor/battery.