History.js在内容脚本中不起作用

时间:2013-11-26 17:22:25

标签: javascript history.js

我尝试在google play商店的内容脚本中使用history.js,但在状态更改时不执行该功能 我尝试了适配器绑定方法 在清单中:

"js": [
        "js/jquery/jquery.js",
        "js/jquery.knob.js",
        "js/jquery.history.js", 
        "src/inject/inject.js"
      ]

然后在内容脚本中

History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
    var State = History.getState(); // Note: We are using History.getState() instead of event.state
    alert();
});

没有任何事情发生

此解决方案也不起作用

 (function(history){
    var pushState = history.pushState;
    history.pushState = function(state) {
        if (typeof history.onpushstate == "function") {
            history.onpushstate({state: state});
        }
        // ... whatever else you want to do
        // maybe call onhashchange e.handler
        return pushState.apply(history, arguments);
    }
})(window.history);


history.onpushstate=function(){alert()}

我认为这可能与内容脚本运行的范围有关

1 个答案:

答案 0 :(得分:0)

问题是内容脚本和页面的窗口对象不同,因此这会导致问题。到目前为止,还没有找到在chrome中的内容脚本中使用history.js的方法。 我使用变异观察器,即检查DOM子树变化来解决问题