仅在速度较慢的PC上执行功能-DOM加载时间过早?

时间:2019-03-15 12:03:08

标签: javascript dom google-chrome-extension mutation-observers

我正在构建一个Chrome扩展程序,该扩展程序使用观察程序来监视DOM上的更改。如果将某些资源提示添加到DOM中,则会执行processLink()函数:

      document.addEventListener("DOMContentLoaded", function(event) {


                chrome.storage.sync.get(["value", "enabled_value", "mode"], function(result) {



                // there's no HEAD yet at document-start, let's wait until we have it
                new MutationObserver((mutations, observer) => {
                  if (document.head) {
                    observer.disconnect();
                    monitorLinks();
                  }
                }).observe(document.documentElement, {childList: true});

                function monitorLinks() {
                  const onMutation = mutations => {
                    for (const {addedNodes} of mutations) {
                      for (const n of addedNodes) {
                        if (n.nodeName === 'LINK' && (n.rel === 'preconnect' || n.rel === 'prefetch' || n.rel ==='dns-prefetch' || n.rel === 'prerender')) {
                          processLink(n);
                        }
                      }
                    }
                  };
                  // the HEAD may have some children already
                  onMutation([{
                    addedNodes: document.getElementsByTagName('link'),
                  }]);
                  // watch for the newly added children of HEAD
                  new MutationObserver(onMutation).observe(document.head, {childList: true});
                }
    });
 });

processLink()函数仅将消息传递到后台脚本,如下所示:

function processLink(link) {

              prerenderFound = true;
              chrome.runtime.sendMessage({ "newIconPath" : "images/48.png" });
}

此操作的结果应该是扩展栏中的图标更改。这全部在contentScript()上执行的document_start中。在我的计算机上,这是一个内存不足的设备,并且它的CPU也很慢,扩展名运行正常。但是在更好的设备上,图标不会更改。我认为DOM加载太早可能存在问题。什么可能导致此问题?

0 个答案:

没有答案