How to listen to history events like “go back”, “go next” in Chrome extension?

£可爱£侵袭症+ 提交于 2019-12-23 18:01:31

问题


For now, I've only found a way to listen to "reload" event using:

chrome.webNavigation.onCommitted.addListener(function(details) {
  if (details.frameId == 0) {
    if (details.transitionType == "reload") {
      // do something
    }
  }
});

What about "go back" and "go next" events? I'm looking for a Firefox's nsISHistoryListener alternative in Chrome.

Edit: Submitted a feature request to Chromium.


回答1:


You could use the window.beforeunload event to capture when a user intends to navigate to another page (which would be triggered when using the back and forward buttons).

window.onbeforeunload = function () {
    // do something
};

There is no way to get the target destination when using onbeforeunload.

More info here: Is there a cross-browser onload event when clicking the back button?



来源:https://stackoverflow.com/questions/8960602/how-to-listen-to-history-events-like-go-back-go-next-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!