简体   繁体   English

重新加载页面更改时的Chrome DevTools Extension侧边栏

[英]Reload Chrome DevTools Extension Sidebar on Page Change

This may be a dupe of the question at Google Chrome DevTools Extension - Detect Page Change , but it's been sitting for over a year without a real answer, and maybe my question will provide some new insight into the problem. 这可能是对Google Chrome DevTools扩展程序-检测页面更改中的问题的重复,但是已经存在了一年多,没有一个真实的答案,也许我的问题将为您提供对该问题的一些新见解。

I've written a Chrome Extension which inserts a sidebar panel into DevTools. 我编写了一个Chrome扩展程序,它将侧边栏面板插入DevTools。 The side panel lists the dataLayer events, a user can click on the desired event and then use the element picker to select another element on the page, and the plugin displays the path between the elements in dot notation. 侧面板列出了dataLayer事件,用户可以单击所需的事件,然后使用元素选择器选择页面上的另一个元素,并且插件以点表示法显示元素之间的路径。

Here's the link to the Github project: https://github.com/gruebleenagency/gtm-data-layer-sifter 这是Github项目的链接: https : //github.com/gruebleenagency/gtm-data-layer-sifter

It works as I'd like it to normally, but if you navigate to another page, the sidebar is not initialized again, so it displays the info from the previous page. 它可以正常运行,但是如果您导航到另一个页面,则侧栏不会再次初始化,因此它将显示上一页的信息。

You can see that I have this in my background.js file: 您可以看到我的background.js文件中包含以下内容:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {
    reloadExtension();
  }
});

function reloadExtension() {
     // ???
}

I'm wondering if there's any way to make the sidebar reload at this point? 我想知道此时是否有任何方法可以重新加载侧栏? Or am I going about this in the wrong way? 还是我会以错误的方式处理此问题? I've spent a lot of time messing around with this trying to get it to work, but nothing seems to do the trick. 我花了很多时间来解决这个问题,以使其正常工作,但似乎没有办法解决。

Thanks for any and all help. 感谢您提供的所有帮助。

Here's my solution. 这是我的解决方案。 It's very possible that this isn't the way it should be done, but I don't have the time to rewrite the whole plugin at the moment, so it'll have to do for now. 很有可能这不是应该完成的方式,但是我目前没有时间重写整个插件,因此现在必须这样做。 Any suggestions for how I should have done it would be greatly appreciated. 我应该怎么做的任何建议将不胜感激。 And maybe it will be helpful to someone in a different situation. 也许这对处于不同情况的人会有所帮助。

Essentially, listen to the tabs.onUpdated event waiting for the 'complete' status, which indicates we've navigated to a new page. 本质上,请听tabs.onUpdated事件等待“完成”状态,这表明我们已经导航到新页面。 Then send message to Devtools.js to refresh the side panel, by setting its page to 'panel.html' again. 然后通过再次将其页面设置为“ panel.html”,将消息发送至Devtools.js以刷新侧面板。

In background.js: 在background.js中:

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
      if (changeInfo.status == 'complete') {
        reloadExtension(port);
      }
    });

function reloadExtension(port) {
    var message = {action: "reloadExtension"};
    port.postMessage(message);
}

In Devtools.js: 在Devtools.js中:

var sb;
createSidebar();

function createSidebar() {
    chrome.devtools.panels.elements.createSidebarPane("GTM dataLayer Sifter", function(sidebar) {
        sb = sidebar;
        sb.setPage("panel.html");
    });
}

var port = chrome.extension.connect({
    name: "Devtools.js Communication"
});

// Listen to messages from the background page
port.onMessage.addListener(function(message) {
    if(message.action == "reloadExtension"){
        sb.setPage("panel.html");
    }
});

You can do it by listen onNavigated event: 您可以通过监听onNavigated事件来实现:

In Devtools.js 在Devtools.js中

chrome.devtools.network.onNavigated.addListener(() => {
  console.log('Inspected page reloaded');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM