【问题标题】:Favicon disappears after preventing execution of a specific inline script tag by TampemonkeyFavicon 在阻止 Tampermonkey 执行特定的内联脚本标记后消失
【发布时间】:2021-03-14 04:05:36
【问题描述】:

我在 Tampermonkey 中使用此代码来防止某些脚本标签执行: 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();
})();

代码删除所有包含字符串'window.location'的脚本标签。

它工作得很好,只是我的浏览器的地址栏中没有显示图标图标。我正在使用 Vivaldi 浏览器。

此行为的原因可能是什么?

当我禁用 Tampermonkey 脚本时,图标会重新出现。但是我检查了 Tampermonkey 脚本并没有改变关于用于显示 favicon 的标签的任何内容。

【问题讨论】:

  • 也许是 window.stop();正在阻止页面在加载之前加载网站图标。
  • 对修改代码以使其不影响网站图标的显示有何建议?

标签: javascript html xmlhttprequest favicon tampermonkey


【解决方案1】:

我通过在我的问题代码后面使用以下代码设置 favicon href 属性解决了这个问题。

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

【讨论】:

    猜你喜欢
    • 2018-10-05
    • 2019-06-27
    • 2018-10-22
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2021-05-22
    • 2015-11-01
    相关资源
    最近更新 更多