Search code examples
javascripthtmlxmlhttprequestfavicontampermonkey

Favicon disappears after preventing execution of a specific inline script tag by Tampemonkey


I use this code in Tampermonkey to prevent certain script tag from executing: https://stackoverflow.com/a/50024143/8849796

(function() {
    'use strict';
    window.stop();
    const xhr = new XMLHttpRequest();
    xhr.open('GET', window.location.href);
    xhr.onload = () => {
        var html = xhr.responseText
        .replace(/<script\b[\s\S]*?<\/script>/g, s => {
            // check if script tag should be replaced/deleted
            if (s.includes('window.location')) {
                return '';
            } else {
                return s;
            }
        });
        document.open();
        document.write(html);
        document.close();
    };
    xhr.send();
})();

The code removes all script tags that contain string 'window.location'.

It works perfectly except that the favicon icon is not displayed in my browser in address bar. I am using Vivaldi browser.

What could be the cause of this behavior?

When I disable the Tampermonkey script the favicon reappears. But I checked that the Tampermonkey script does not change anything regarding the tags used for displaying favicon.


Solution

  • I solved it by setting the favicon href attribute using the following code after the code in my question.

    var favi=document.querySelector('[rel="shortcut icon"]');
    favi.setAttribute("href",favi.getAttribute("href"));