简体   繁体   English

是什么导致我使用脚本的变异观察器出现此错误?

[英]What's causing this error in my mutation observer using script?

The original question asked has been answered . 最初提出的问题已得到答复 In addition, someone outside of Stackoverflow has provided a full solution to what I was trying to achieve with my script, which I have added as an answer to my question. 另外, Stackoverflow之外的某人为我试图通过脚本实现的目标提供了完整的解决方案 ,我将其添加为问题的答案。 The script itself and a summary of the specific problem it solves is detailed in my own answer. 我自己的答案中详细描述了脚本本身以及解决的特定问题的摘要。

Original Question: 原始问题:

I'm not a programmer, unfortunately, so my experience is very limited. 不幸的是,我不是程序员,所以我的经验非常有限。

My browser overwrites a correct img src with an incorrect src a couple of seconds after it starts. 我的浏览器启动后几秒钟,用错误的src覆盖了正确的img src

My script's intended function is a work-around to immediately replace the broken src with the correct src again, at the time my browser overwrites the correct src . 我的脚本的预期功能是一个变通立即更换破损src与正确的src再次,在我的浏览器重写正确的时间src

function getElementsBySrc(srcValue) {
  var nodes = [];
  var e = document.getElementsByTagName('img');

  for (var i = 0; i < e.length; i++) {
    if (e[i].hasAttribute('src') && e[i].getAttribute('src') == srcValue) {
      nodes.push(e[i]);
    }
  }

  return nodes;
}

function initMod(){
    if(!document.querySelector("#browser")){
        setTimeout(initMod, 1000);
        return;
    }

var targetNode = document.getElementsByTagName('img');
var config = { attributes: true, childList: false, subtree: false, characterData: false };

var callback = function(mutationsList, observer) {
    for(var mutation of mutationsList) {
        if (mutation.type == 'attributes') {

            console.log('The ' + mutation.attributeName + ' attribute was modified.');

            var n = getElementsBySrc('https://hyperspace.marquiskurt.net/icons/favicon-32x32.png');
            for (var i = 0; i < n.length; i++) {
                n[i].setAttribute('src', 'chrome://favicon/https://hyperspace.marquiskurt.net/app/');
            }

        }
    }
};

var observer = new MutationObserver(callback);

observer.observe(targetNode, config);

}
initMod();

The script is loaded directly as my browser starts. 在我的浏览器启动时,脚本直接加载。 My Chrome Devtools Console gives the error: 我的Chrome Devtools控制台显示错误:

Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.

as document.getElementsByTagName('img'); 作为document.getElementsByTagName('img'); returns a list of elements, not a node, you can't use targetNode (because it isn't a node) 返回元素列表 ,而不是节点,您不能使用targetNode (因为它不是节点)

Here I'm using document.querySelectorAll instead, same result, but it has a forEach method that I use to create the mutation observers for each IMG 在这里,我改用document.querySelectorAll ,结果相同,但是它有一个forEach方法,用于为每个IMG创建突变观察者

function initMod() {
    if(!document.querySelector("#browser")){
        setTimeout(initMod, 1000);
        return;
    }

    const callback = (mutationsList, observer) => {
        for(let mutation of mutationsList) {
            if (mutation.type == 'attributes') {
                console.log('The ' + mutation.attributeName + ' attribute was modified.');
                const n = getElementsBySrc('https://hyperspace.marquiskurt.net/icons/favicon-32x32.png');
                for (let e of n) {
                    e.setAttribute('src', 'chrome://favicon/https://hyperspace.marquiskurt.net/app/');
                }
            }
        }
    };
    const config = { attributes: true, childList: false, subtree: false, characterData: false };
    document.querySelectorAll('img').forEach(targetNode => {
        const observer = new MutationObserver(callback);
        observer.observe(targetNode, config);
    });
}

Someone very kindly provided a solution that achieved exactly what I was trying to do. 有人非常友好地提供了一种解决方案,该解决方案完全实现了我正在尝试做的事情。

const selector = "#panels img[src='https://hyperspace.marquiskurt.net/icons/favicon-32x32.png']";
function fixImages() {
    const maybeBadImage = document.querySelector(selector);
    if(maybeBadImage){
        maybeBadImage.src = "chrome://favicon/https://hyperspace.marquiskurt.net/app/";
    }
}
const observer = new MutationObserver(fixImages);
function initMod() {
    if(!document.querySelector(selector)){
        setTimeout(initMod, 0);
        return;
    }
    observer.observe(document.querySelector(selector), { attributes: true });
    fixImages();
}
initMod();

With setTimeout set to 0, the broken src is immediately corrected without any visual change that the user can see. setTimeout设置为0时,可以立即更正损坏的src ,而用户看不到任何视觉变化。 This script can be modified for use if anyone else is having the issue where the web browser Vivaldi incorrectly modifies the src for the favicon of a specific user-added web panel. 如果其他任何人遇到网络浏览器Vivaldi错误地修改特定用户添加的Web面板的图标的src的问题,则可以修改此脚本以供使用。

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

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