Advertisement suddenly appearing on top of almost every page

After many tests, I've realized that the problem is happening on cellular network only because of caching. After clearing a cache (Clear History and Website Data) and refreshing, the problem went away. And it reappeared only after connecting back to the Wi-Fi.

This made it obvious that the problem is due to a compromised router, Edimax AR-7265WNB. Resetting the router back to the factory settings and re-configuring fixed the problem.

I did not find newer version of router firmware than the one I have (FwVer:3.10.16.0_TC3085 HwVer:T14.F7_3.0). Though I've found that the firewall on the router was off. Actually the router reset itself few weeks back. When reconfiguring it, I probably forgot to enable the firewall (actually I would expect the firewall to be on by default).

The problem seems world-wide now (other reports here and here, some other were deleted), contrary to my claim in the question. That would suggest remote exploiting of some router vulnerability (supported by firewall issue), rather than local hacking into Wi-Fi. The others report different types of router (D-Link DSL-2600U, TP-Link), so the issue is not specific to the Edimax.

The other reports mention that a DNS or proxy settings was modified. I did not checked this before resetting my router. But it is possible that my router was modified this way, as the firewall was off. Also it explains injecting code in any page without a need for any router-specific exploit. So the attacker possibly scans internet for any unsecured routers and simply configures them to point to attacker's proxy.