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

有些话、适合烂在心里 提交于 2020-12-15 05:45:14

问题


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.


回答1:


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"));


来源:https://stackoverflow.com/questions/65103986/favicon-disappears-after-preventing-execution-of-a-specific-inline-script-tag-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!