Firefox 扩展正在阻止 Javascript 运行

Firefox Extension is Preventing Javascript Running

我正在编写一个简单的 Firefox 网络扩展,它将用扩展版本替换某些单词(例如首字母缩略词)。我的代码如下:

var replacements = [];
replacements["example1"] = "My First Example";
replacements["example2"] = "Second Example";

if(!window.location.href.startsWith('https://ignore.thissite.com/')){
    for(key in replacements){
        replaceOnDocument(new RegExp('\b'+key+'\b', 'gi'), '{{{REPLACE_'+key+'}}}');
        document.body.innerHTML = document.body.innerHTML.replace(new RegExp('{{{REPLACE_'+key+'}}}', 'g'), '<abbr title="'+key+'">'+replacements[key]+'</abbr>');
    }
}

function replaceOnDocument(pattern, string){
    Array.from(document.querySelectorAll("body, body *:not(script):not(noscript):not(style):not(code):not(pre)"))
        .forEach(someNode => Array.from(someNode.childNodes)
        .filter(childNode => childNode.nodeType == 3)
        .forEach(textNode => textNode.textContent = textNode.textContent.replace(pattern, string)));
}

这似乎按预期替换了所有实例,但我注意到 Javascript 在脚本具有 运行 的页面上似乎 运行 不正确。我已经盯着代码看了半个小时了,无法弄清楚为什么会这样。更糟糕的是,问题似乎是间歇性的(尽管经常发生)。

关于为什么我的代码会按预期阻止 Javascript 运行ning 的任何想法?

我已经推测替换body.innerHTML的内容可能会出现问题,那么使用以下方法替换文本怎么样? codepen example

const walker = document.createTreeWalker(
  document.body,
  NodeFilter.SHOW_TEXT,
  {
    // filter / exclude tags
    acceptNode: function (node) {
      return node.parentElement.nodeName === "SCRIPT" ? NodeFilter.FILTER_SKIP : NodeFilter.FILTER_ACCEPT;
    }
  }
);

while (walker.nextNode()) {
  // replace every H with @
  walker.currentNode.nodeValue = walker.currentNode.nodeValue.replace("H", "@");
}

这将遍历除脚本文本节点之外的每个文本节点并替换它们的内容。