'window.onhashchange doesn't work in tampermonkey

I'm learning Javascript, and I want to write a simple script in Tampermonkey, which will run when the URL fragment identifier changes. But the following function doesn't work when the identifier change. window.onhashchange doesn't work either. I'm convinced that the func() is not the reason.

window.addEventListener("hashchange", function (){
    alert("run");
    func();
});

But if I add this function for testing,

window.addEventListener("click", function (){
   console.log(window.location.hash);
});

The hash printed in the console changes with the identifier every time I click, as expected.

I have no idea if this is my problem or the website or the Tampermonkey.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source