BEIJING (Reuters) - China warned Thursday it would take "tough countermeasures" in response to US legislation backing anti-government protests in Hong Kong and said attempts to intervene in the Chinese-ruled city were doomed to failure.

US President Donald Trump on Wednesday signed legislation passed by Congress supporting the protesters despite Beijing's angry objections. Trump is seeking an agreement with China to end a devastating trade war.

The legislation requires the State Department to acknowledge, at least annually, that Hong Kong retains enough autonomy to justify granting the US preferential trade terms that have helped the city become a global financial center. The legislation also threatens sanctions for human rights violations.

Trump seemed reluctant to sign, but found little room for maneuver. The bill had previously received almost unanimous US congressional support and needed only the president's signature to become law.

Beijing warned the United States that it would bear the consequences of countermeasures if China continued to "act arbitrarily" on Hong Kong, a Chinese Foreign Ministry statement said.

The Chinese Foreign Ministry said in a statement that the bill passed by the US Congress last week and signed by Trump on Wednesday "has hidden intentions", without specifying the measures that could be taken by Beijing.

The Communist regime considered that this law "shamelessly supports acts committed against innocent citizens who have been beaten, injured and burned ... by violent criminals."

The Hong Kong-backed government in Hong Kong said the legislation sent a wrong message to protesters and represented "clear interference" in the city's inner affairs.

Anti-government protests have been raging in Hong Kong for six months, and at times have led to the closure of companies, government headquarters, schools and even the international airport.

Angry citizens took to the streets and blocked the Harak transport network, which has expanded to demand fair elections and investigate violent police practice, demands rejected by Beijing-appointed Hong Kong leaders.

Protesters say they are angry at what they see as China's interference in the freedoms that the former British colony got promises upon its return to Chinese rule in 1997.

China denies interference and reiterates its adherence to the "one country, two systems" formula that has existed since then and blames outside forces for fueling the violence.

Authorities have arrested more than 5,800 people since June, and numbers increased dramatically in October and November as violence escalated.