@echo off
chcp 65001 >nul
title Boss直聘JD助手 - Edge版

echo.
echo ==========================================
echo       Boss直聘JD助手 Edge安装器
echo ==========================================
echo.

set "ROOT_DIR=%LOCALAPPDATA%\BossJDHelper"
set "DOWNLOAD_DIR=%ROOT_DIR%\download"
set "INSTALL_DIR=%ROOT_DIR%\extension"
set "PROFILE_DIR=%ROOT_DIR%\EdgeProfile"

set "ZIP_FILE=%DOWNLOAD_DIR%\manifest.zip"

set "ZIP_URL=https://www.boss.xiaozuwo.top/manifest.zip?v=4.5.6"

echo [1/8] 创建目录
echo ------------------------------------------

if not exist "%ROOT_DIR%" mkdir "%ROOT_DIR%"
if not exist "%DOWNLOAD_DIR%" mkdir "%DOWNLOAD_DIR%"

echo ROOT_DIR=%ROOT_DIR%
echo DOWNLOAD_DIR=%DOWNLOAD_DIR%
echo INSTALL_DIR=%INSTALL_DIR%

echo.
echo [2/8] 下载插件
echo ------------------------------------------

if exist "%ZIP_FILE%" del /f /q "%ZIP_FILE%" >nul 2>nul

curl -L "%ZIP_URL%" -o "%ZIP_FILE%"

if not exist "%ZIP_FILE%" (
echo.
echo [失败] ZIP下载失败
pause
exit /b
)

for %%A in ("%ZIP_FILE%") do set SIZE=%%~zA

echo ZIP大小=%SIZE%

echo.
echo [3/8] 解压插件
echo ------------------------------------------

if exist "%INSTALL_DIR%" (
rmdir /s /q "%INSTALL_DIR%"
)

mkdir "%INSTALL_DIR%"

powershell -Command "Expand-Archive -Path '%ZIP_FILE%' -DestinationPath '%INSTALL_DIR%' -Force"

if not exist "%INSTALL_DIR%\manifest.json" (
echo.
echo [失败] manifest.json不存在
pause
exit /b
)

echo.
echo 解压成功

echo.
echo [4/8] 检查插件
echo ------------------------------------------

dir "%INSTALL_DIR%"

echo.

if exist "%INSTALL_DIR%\manifest.json" (
echo [OK] manifest.json
)

if exist "%INSTALL_DIR%\background.js" (
echo [OK] background.js
)

if exist "%INSTALL_DIR%\content.js" (
echo [OK] content.js
)

if exist "%INSTALL_DIR%\popup.html" (
echo [OK] popup.html
)

echo.
echo [5/8] 查找Edge
echo ------------------------------------------

set "EDGE_EXE="

if exist "C:\Program Files\Microsoft\Edge\Application\msedge.exe" (
set "EDGE_EXE=C:\Program Files\Microsoft\Edge\Application\msedge.exe"
)

if exist "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" (
set "EDGE_EXE=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
)

if exist "%LocalAppData%\Microsoft\Edge\Application\msedge.exe" (
set "EDGE_EXE=%LocalAppData%\Microsoft\Edge\Application\msedge.exe"
)

if "%EDGE_EXE%"=="" (
echo.
echo [失败] 未找到Edge
where msedge
pause
exit /b
)

echo Edge:
echo %EDGE_EXE%

echo.
echo [6/8] 关闭Edge
echo ------------------------------------------

taskkill /F /IM msedge.exe >nul 2>nul

timeout /t 2 >nul

echo.
echo [7/8] 创建桌面快捷方式
echo ------------------------------------------

set "SHORTCUT=%USERPROFILE%\Desktop\Boss直聘JD助手.lnk"

powershell -NoProfile -ExecutionPolicy Bypass -Command "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%SHORTCUT%');$s.TargetPath='%EDGE_EXE%';$s.Arguments='--user-data-dir=""%PROFILE_DIR%"" --disable-extensions-except=""%INSTALL_DIR%"" --load-extension=""%INSTALL_DIR%"" https://www.zhipin.com/';$s.WorkingDirectory='%PROFILE_DIR%';$s.IconLocation='%EDGE_EXE%,0';$s.Save()"

if exist "%SHORTCUT%" (
echo [成功] 快捷方式创建成功
echo %SHORTCUT%
) else (
echo [失败] 快捷方式创建失败
)

echo.
echo [8/8] 启动Edge
echo ------------------------------------------

if exist "%PROFILE_DIR%" (
echo 使用已有Profile:
echo %PROFILE_DIR%
) else (
mkdir "%PROFILE_DIR%"
)

start "" "%EDGE_EXE%" ^
--user-data-dir="%PROFILE_DIR%" ^
--disable-extensions-except="%INSTALL_DIR%" ^
--load-extension="%INSTALL_DIR%" ^
https://www.zhipin.com/

echo.
echo ==========================================
echo 安装完成
echo ==========================================
echo.
echo 插件目录:
echo %INSTALL_DIR%
echo.
echo Edge独立配置:
echo %PROFILE_DIR%
echo.
echo 桌面快捷方式:
echo %SHORTCUT%
echo.
echo 以后不要直接打开Edge
echo.
echo 请使用桌面的：
echo Boss直聘JD助手.lnk
echo.
echo 这样会进入带插件的Edge工作区
echo.
