【问题标题】:Check browser back in Chrome extension in Trello在 Trello 的 Chrome 扩展程序中重新检查浏览器
【发布时间】:2018-03-06 15:57:12
【问题描述】:

我正在开发一个 Chrome 扩展程序,它在我的卡片列表中处理 Trello 中的卡片。

即使 Trello 是一个动态 JS 应用,我的扩展程序也能够在用户点击链接时检测到 URL 的变化。

但是如何检测用户何时点击浏览器中的后退/前进按钮?

我尝试了很多(简单的)方法来检测我在互联网上找到的“后退”按钮,但都没有成功。这是我目前的检测功能。

function detectHistoryChange(handler) {
    document.head.appendChild(document.createElement('script')).text = '(' +
        function () {
            // injected DOM script is not a content script anymore,
            // it can modify objects and functions of the page
            var _pushState = history.pushState;
            history.pushState = function (state, title, url) {
                _pushState.call(this, state, title, url);
                window.dispatchEvent(new CustomEvent('state-changed', { detail: state }));
            };
            // repeat the above for replaceState too
        } + ')(); $(this).remove();'; // remove the DOM script element

    // And here content script listens to our DOM script custom events
    window.addEventListener('state-changed', function (e) {
        console.log('History state changed', e.detail, location.hash);
        handler();
    });
}

谢谢!

【问题讨论】:

  • 查看 chrome.webNavigation.onHistoryStateUpdated 或 chrome.tabs.onUpdated 的文档
  • 谢谢,但我可以 chrome.tabs.onUpdated 对我不起作用,即使有许可:“标签”。但我找到了答案。似乎 Trello 在导航上触发了一个事件single-spa:routing-event。所以我在这个事件上添加了一个监听器,它就像一个魅力! :)
  • 是的,找到页面事件是解决方案之一。至于 chrome.tabs 和 chrome.webNavigation,两者都应该在事件/背景页面中使用。
  • 好吧,不知道。谢谢。

标签: javascript google-chrome-extension dom-events trello


【解决方案1】:

似乎 Trello 在导航时触发了一个事件 single-spa:routing-event。所以我在这个事件上添加了一个监听器,它就像一个魅力!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2013-08-26
    相关资源
    最近更新 更多