SingleFile 自动保存设置

选项里

1在页面加载完成后或开始卸载时进行自动保存 选中这个之后,会保存两次。经测试,应该是代表下边这俩2 3各一次。取消选中这个,然后选择2,就只保存一次了
2在页面加载完成后进行自动保存。
3在页面开始卸载时进行自动保存

 

 

右上角图表 右键 里边还有个自动保存选项,这两个位置都要设置

 

 

 

onbeforeunload 事件在即将离开当前页面(刷新或关闭)时触发。

 

源码部分分析

https://github.com/gildas-lormeau/SingleFile/blob/0a6587ce566cd9d9c7ecadfd30f4a5b9ec8783f7/src/ui/bg/ui-options.js#L265

    autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
    autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
    autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;

 

 

function refresh() {
    if (autoSaveEnabled && optionsAutoSave && (optionsAutoSave.autoSaveUnload || optionsAutoSave.autoSaveLoadOrUnload || optionsAutoSave.autoSaveDiscard || optionsAutoSave.autoSaveRemove)) {
        if (!unloadListenerAdded) {
            globalThis.addEventListener("unload", onUnload);
            document.addEventListener("visibilitychange", onVisibilityChange);
            unloadListenerAdded = true;
        }
    } else {
        globalThis.removeEventListener("unload", onUnload);
        document.removeEventListener("visibilitychange", onVisibilityChange);
        unloadListenerAdded = false;
    }
}

 

在页面丢弃时进行自动保存

在页面移除时进行自动保存

 

关于这俩选项

    "optionAutoSaveDiscard": {
        "message": "在页面丢弃时进行自动保存",
        "description": "Options page label: 'auto-save on tab discard'"
    },
    "optionAutoSaveRemove": {
        "message": "在页面移除时进行自动保存",
        "description": "Options page label: 'auto-save on tab removal'"
    },

 

https://github.com/gildas-lormeau/SingleFile/blob/b29ab0f5f9a2ad0086c9c55038058d7cea88e313/src/core/content/content-bootstrap.js#L261

 

function onVisibilityChange() {
 if (document.visibilityState == "hidden" && optionsAutoSave.autoSaveDiscard) { // 这个hidden 'hidden' : 此时页面对用户不可见。即文档处于背景标签页或者窗口处于最小化状态,或者操作系统正处于 '锁屏状态' .
 autoSaveUnloadedPage({ autoSaveDiscard: optionsAutoSave.autoSaveDiscard }); // 意思大概就是切换标签页、最小化浏览器等等情况下,会自动保存
 }
}


function onUnload() {
 if (!pageAutoSaved && (optionsAutoSave.autoSaveUnload || optionsAutoSave.autoSaveLoadOrUnload || optionsAutoSave.autoSaveRemove)) { // 可以看到 在页面开始卸载时进行自动保存 跟 在页面移除时进行自动保存 一个效果
 autoSaveUnloadedPage({ autoSaveUnload: optionsAutoSave.autoSaveUnload, autoSaveRemove: optionsAutoSave.autoSaveRemove });
 }
}

posted @ 2024-04-15 12:37  hrdom  阅读(45)  评论(0编辑  收藏  举报