问题
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