XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

feat: Refactor start-browser script to support multiple browsers and improve path handling

a600e8e1
hlohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +132 -6
Modified g4f/gui/server/backend_api.py +1 -1
@@ -225,7 +225,7 @@ class Backend_Api(Api):
225 225 except json.JSONDecodeError as e:
226 226 logger.exception(e)
227 227 return jsonify({"error": {"message": "Invalid JSON data"}}), 400
228 for key in ["base_url", "proxy", "media"]:
228 for key in ["base_url", "proxy"]:
229 229 if key in json_data:
230 230 del json_data[key] # Remove unsupported fields for security
231 231 if app.demo and has_crypto:
Modified scripts/start-browser.sh +131 -5
@@ -1,6 +1,132 @@
1 1 #!/bin/bash
2 while true; do
3 sleep 5
4 rm ~/.config/g4f/cookies/.browser_is_open
5 google-chrome --remote-allow-origins=* --no-first-run --no-service-autorun --no-default-browser-check --homepage=about:blank --no-pings --password-store=basic --disable-infobars --disable-breakpad --disable-dev-shm-usage --disable-session-crashed-bubble --disable-search-engine-choice-screen --user-data-dir="~/.config/g4f-nodriver" --disable-session-crashed-bubble --disable-features=IsolateOrigins,site-per-process --remote-debugging-host=127.0.0.1 --remote-debugging-port=57011
6 done
2
3 # Browser selection: chrome (default), chromium, brave, msedge
4 BROWSER="${1:-chrome}"
5
6 BROWSER_FLAGS=(
7 --remote-allow-origins=*
8 --no-first-run
9 --no-service-autorun
10 --no-default-browser-check
11 --homepage=about:blank
12 --no-pings
13 --password-store=basic
14 --disable-infobars
15 --disable-breakpad
16 --disable-dev-shm-usage
17 --disable-session-crashed-bubble
18 --disable-search-engine-choice-screen
19 --user-data-dir="$HOME/.config/g4f-nodriver"
20 --disable-features=IsolateOrigins,site-per-process
21 --remote-debugging-host=127.0.0.1
22 --remote-debugging-port=57011
23 )
24
25 # Resolve a Windows path to the correct format for the current shell environment.
26 # Git Bash uses /c/... whereas WSL uses /mnt/c/...
27 win_path() {
28 local p="$1"
29 # Git Bash / MSYS2: OSTYPE contains "msys" or "cygwin"
30 if [[ "$OSTYPE" == msys* || "$OSTYPE" == cygwin* ]]; then
31 # Convert "C:\foo\bar" or "/mnt/c/foo/bar" -> "/c/foo/bar"
32 p="${p//\\//}" # backslash -> slash
33 p="${p/\/mnt\/c\//\/c\/}" # /mnt/c/ -> /c/
34 echo "$p"
35 else
36 # WSL / Linux: convert /c/ -> /mnt/c/
37 p="${p//\\//}"
38 p="${p/\/c\//\/mnt\/c\/}"
39 echo "$p"
40 fi
41 }
42
43 # Returns the first existing Windows path from a list of candidate paths.
44 find_win_bin() {
45 for raw in "$@"; do
46 local p
47 p="$(win_path "$raw")"
48 [ -f "$p" ] && echo "$p" && return
49 done
50 }
51
52 case "$BROWSER" in
53 chromium)
54 for bin in chromium chromium-browser chromium-freeworld; do
55 if command -v "$bin" &>/dev/null; then
56 BROWSER_BIN="$bin"; break
57 fi
58 done
59 if [ -z "$BROWSER_BIN" ]; then
60 BROWSER_BIN="$(find_win_bin \
61 "/c/Program Files/Chromium/Application/chrome.exe" \
62 "/mnt/c/Program Files/Chromium/Application/chrome.exe")"
63 fi
64 ;;
65 brave)
66 for bin in brave-browser brave brave-browser-stable; do
67 if command -v "$bin" &>/dev/null; then
68 BROWSER_BIN="$bin"; break
69 fi
70 done
71 if [ -z "$BROWSER_BIN" ]; then
72 BROWSER_BIN="$(find_win_bin \
73 "/c/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe" \
74 "/mnt/c/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe" \
75 "$LOCALAPPDATA/BraveSoftware/Brave-Browser/Application/brave.exe")"
76 fi
77 ;;
78 msedge)
79 for bin in microsoft-edge msedge microsoft-edge-stable; do
80 if command -v "$bin" &>/dev/null; then
81 BROWSER_BIN="$bin"; break
82 fi
83 done
84 if [ -z "$BROWSER_BIN" ]; then
85 BROWSER_BIN="$(find_win_bin \
86 "/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" \
87 "/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" \
88 "/c/Program Files/Microsoft/Edge/Application/msedge.exe" \
89 "/mnt/c/Program Files/Microsoft/Edge/Application/msedge.exe" \
90 "$LOCALAPPDATA/Microsoft/Edge/Application/msedge.exe")"
91 fi
92 # Last resort: ask Windows itself
93 if [ -z "$BROWSER_BIN" ] && command -v powershell.exe &>/dev/null; then
94 BROWSER_BIN="$(powershell.exe -NoProfile -Command \
95 "(Get-Command msedge -ErrorAction SilentlyContinue).Source" 2>/dev/null | tr -d '\r')"
96 fi
97 ;;
98 chrome|*)
99 for bin in google-chrome google-chrome-stable google-chrome-unstable; do
100 if command -v "$bin" &>/dev/null; then
101 BROWSER_BIN="$bin"; break
102 fi
103 done
104 if [ -z "$BROWSER_BIN" ]; then
105 BROWSER_BIN="$(find_win_bin \
106 "/c/Program Files/Google/Chrome/Application/chrome.exe" \
107 "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe" \
108 "$LOCALAPPDATA/Google/Chrome/Application/chrome.exe")"
109 fi
110 ;;
111 esac
112
113 if [ -z "$BROWSER_BIN" ]; then
114 echo "Error: No browser binary found for '$BROWSER'" >&2
115 exit 1
116 fi
117
118 PROC_NAME="$(basename "$BROWSER_BIN")"
119 echo "Starting browser: $BROWSER_BIN"
120
121 if [[ "$BROWSER_BIN" == *.exe ]]; then
122 # Windows: launch once and exit — the browser manages its own lifecycle
123 rm -f ~/g4f/cookies/.browser_is_open
124 "$BROWSER_BIN" "${BROWSER_FLAGS[@]}"
125 else
126 # Linux: loop and relaunch if the browser exits
127 while true; do
128 rm -f ~/g4f/cookies/.browser_is_open
129 "$BROWSER_BIN" "${BROWSER_FLAGS[@]}"
130 sleep 5
131 done
132 fi