;(
    function (){

        async function sleep(ms) {
            return new Promise((resolve) => setTimeout(resolve, ms));
        }

         Promise.all([
            waitForThing(window, 'SteedosUI'),
        ]).then(()=>{
            SteedosUI.navigationGuard.registerBeforeChange(
                async function(blocker){
                    console.log(`blocker`, blocker);
                    // 检查当前路径是否符合条件
                    const urlParams = new URLSearchParams(window.location.search);
                    const sideListViewId = urlParams.get('side_listview_id');
                    if((window.location.pathname.startsWith('/app/approve_workflow/instance_tasks/view/') || window.location.pathname.startsWith('/app/approve_workflow/instances/view/')) && (sideListViewId==='draft' || sideListViewId==='inbox')){
                        // 检查是否发生了变化
                        if(window.SteedosWorkflow.Instance.changed){
                            // 触发保存按钮点击
                            $(".instance-save-btn").trigger('click');
                            await sleep(100);
                            try {
                                // 等待 SteedosWorkflow.Instance.changed 变为 false
                                await waitForThing(window, 'SteedosWorkflow.Instance', function(){
                                    console.log('SteedosWorkflow.Instance.changed===>', SteedosWorkflow.Instance.changed)
                                    return SteedosWorkflow.Instance.changed === false;
                                });
                                console.log('暂存完成....')
                                // 等待完成后返回 true
                                return true;
                            } catch (error) {
                                console.error('等待保存完成时发生错误:', error);
                                return true;
                            }
                        } else {
                            return true;
                        }
                    }
                    return true;
                }
            )
        })
        
    }
)()