当页面位于本地文件系统(file:///不是http://https://)上时,是否有方法检查页面是否已更改?
有一些答案here使用AJAX检查头文件的“Last Modified”头文件,但在使用文件协议时不会返回。
以下是Firefox在本地测试时返回的头:

// headers
Content-Type: text/xml
Content-Length: 20941

更新:
看起来响应已设置为文件,并且在响应上具有lastModified属性。我加了一个答案。

最佳答案

根据您在评论中的回复,我将使用某种形式的缓存页面。
选中this answer将页面的当前内容作为字符串并缓存/a散列。从这里开始,对当前页面执行AJAX请求并比较内容/散列。如果它们不匹配,则可以重新加载页面。

let contents = document.documentElement.outerHTML;
let frequency = 5000; // How often to check for changes (ms)

setInterval(() => {
    let xmlhttp = new XMLHttpRequest();

    xmlhttp.addEventListener("load", (res) => {
        if(res.responseText != contents)
            window.location.reload(true);
    });

    xmlhttp.open("GET", window.location.href);
    xmlhttp.send();
}, frequency);

09-30 17:18
查看更多