A week ago, Israel’s longest-serving prime minister Benjamin Netanyahu was facing the humiliating end of his fifth premiership.

After four stalemated elections, he had once again failed to form a government. His rivals had banded together in an unlikely coalition and were on the verge of forming a government designed to oust him. With courts open again after the coronavirus lockdown, his trial on corruption charges was picking up steam.

But then Hamas, the Palestinian militant group that controls the Gaza Strip, stepped into the fray of a long-simmering dispute over control of al-Aqsa mosque that had escalated after police stormed the compound and wounded more than 600 Palestinians. Hamas fired seven rockets deep into Israel and, as the Israeli military pounded the Gaza Strip in response, Netanyahu’s career has inadvertently been resurrected.

“Netanyahu had no cards left to play, and suddenly, he was saved by the bell,” said Aviv Bushinsky, a former aide to Netanyahu, marvelling at his former boss’s good fortune. “He’s so lucky, every time.”

Seven days after that initial volley, Netanyahu’s rivals have melted away. His political opponents, including spurned allies Naftali Bennett and TV anchor-turned-opposition leader Yair Lapid, were within days of scheduling a vote that would make Bennett the next prime minister.

The coalition talks, which leaned on the support of an Arab-Israeli Islamist party, were abandoned, as communal riots broke out between Arabs and Jews living in mixed Israeli cities. And as Israel’s military carried out the heaviest bombardment of the blockaded Gaza Strip since a war in 2014, Netanyahu has resumed the mantle of “Mr Security”, his nickname in Israeli media.

Suddenly, the 71-year-old political veteran who has seen off numerous rivals during his three-decade career, appears to have a fresh chance.

Israel is heading to a fifth election, and Netanyahu is dusting off a plan that might give him better odds than in the previous four: a direct election to the premiership, followed by months of coalition negotiations to form an actual government.

The scale of Hamas’s volley came as a surprise to the Israeli military, an Israeli military spokesman acknowledged. But for Netanyahu, it presented a last-minute opportunity to cancel out what those to the right of him charged was a weakness: that he was soft on Hamas.

For years, Netanyahu had championed a policy dubbed “quiet for quiet”, hemming in Hamas with short volleys of air strikes, and appeasing it with more hours of electricity, allowing fishermen to venture further into the Mediterranean, and letting Qatar pay $100-a-month salaries to civilian staff such as teachers and doctors.

By Tuesday evening, Netanyahu had greenlit an ambitious new military strategy, one that has seen the Gaza Strip suffer through an intense military bombardment. That 192 Palestinians, including 92 women and children, have been killed has not affected his domestic standing. Israel has reported 10 dead from the Hamas attacks, including two children.

In Israel, the television channels cover the battle nonstop, every explosion in Gaza running with a banner repeating the military’s claims that it was hitting legitimate military targets: tunnels and “terrorists”.

“From a political vantage point, this has put him back on the horse . . . and he’s riding it,” said Yohanan Plesner, the director of the Israel Democracy Institute, a centrist think-tank.

Netanyahu is also riding a rightwing wave of anger against Israel’s Arab minority, a fifth of the population that holds Israeli nationality but has been derided for decades as disloyal to the Zionist cause.

Communal riots in mixed Arab and Jewish towns have shaken Israel’s narrative of peaceful coexistence, with dozens of people, both Arab and Jewish, assaulted, and the police struggling to keep order.

Spurring that anger is an ally of his, Itamar Ben-Gvir, whose party, the Religious Zionists, Netanyahu helped form.

Ben-Gvir is a disciple of the extremist Rabbi Meir Kahane, one of whose followers opened fire in a mosque in Hebron in 1994, killing 29. In his speech upon entering parliament, Ben-Gvir vowed to restore Jewish sovereignty to the Temple Mount, the sacred plot of land in Jerusalem’s walled city where al-Aqsa mosque now stands.

Control of the site, which is administered by a religious trust backed by Jordan, is a hugely emotive issue. Muslim protesters clashed with police last weekend amid anger over access restrictions and an incident where the police, citing security concerns, stopped busloads of Arab Israelis from getting to Jerusalem to pray on a holy night.

“You tell Itamar Ben-Gvir that he should stay at home and shut up,” said a Muslim grandmother waiting outside the old city of Jerusalem for her sons to finish evening prayers on Monday. “If he comes here, he will feel the back of my shoe.”

Since then, Ben-Gvir has popped up in Arab and mixed Jewish cities, as well as occupied East Jerusalem’s Sheikh Jarrah neighbourhood last week, where he championed the eviction of longtime residents to make way for Jewish settlers. Israeli television channels trail him wherever he goes. Any government that Netanyahu forms will rely on support from Ben-Gvir.

“Whenever tensions between Jews and Arabs is rising, it’s generally good for the Israeli right,” said Plesner, whose institute studies long-term political trends. “And overall, that strengthens Netanyahu’s posture.”

For Netanyahu’s political opponents, who were nearly on the verge of toppling the prime minister, his comeback is enraging. Lapid, the man in charge of building the coalition that nearly defeated Netanyahu, said the fact that Israel had been without a stable government for almost two years had aided Netanyahu’s sudden return to the fray.

“If we had a government, no one would allow an extremist like Itamar Ben-Gvir to set Jerusalem alight and then the rest of the country in its wake,” he wrote on Facebook. “No one would ask themselves why conflicts always seem to break out when it’s most convenient for the prime minister.”