play.wenpai.net/scripts/play-localize
feibisi e715d3b0df fix: serve blueprints locally to avoid CORS issues with feicode raw URLs
- Blueprint JSON + screenshots served from /community-blueprints/ (same origin)
- play-deploy mirror now syncs blueprint files from feicode to local
- play-localize updated to use local paths instead of feicode raw
2026-02-24 14:03:48 +08:00

160 lines
7.3 KiB
Bash
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# play-localize — play.wenpai.net 中国本地化补丁
# 引擎升级后运行此脚本重新应用所有本地化修改
# 幂等:可安全重复运行
#
# 用法: play-localize [--dry-run] [--remote]
# --dry-run 只显示会修改什么,不实际修改
# --remote 在远程服务器上执行(通过 SSH
# 无参数 在当前目录执行(需要 cd 到站点根目录)
set -euo pipefail
REMOTE_HOST="feicode-prod"
REMOTE_ROOT="/www/wwwroot/play.wenpai.net"
DRY_RUN=false
REMOTE=false
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=true ;;
--remote) REMOTE=true ;;
esac
done
# 如果 --remote把自己传到远程执行
if $REMOTE; then
echo "[localize] 在远程服务器执行..."
EXTRA_ARGS=""
$DRY_RUN && EXTRA_ARGS="--dry-run"
# shellcheck disable=SC2029
scp -q "$0" "$REMOTE_HOST:/tmp/_play-localize.sh"
ssh "$REMOTE_HOST" "cd $REMOTE_ROOT && bash /tmp/_play-localize.sh $EXTRA_ARGS && rm -f /tmp/_play-localize.sh"
exit $?
fi
# ── 以下在站点根目录执行 ──────────────────────────
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info() { echo -e "${GREEN}[✓]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
CHANGED=0
# 安全替换函数:只在文件包含 old 且不包含 new 时才替换(幂等)
safe_replace() {
local old="$1" new="$2"
shift 2
local files=("$@")
for f in "${files[@]}"; do
[ -f "$f" ] || continue
# 跳过已经替换过的文件new 已存在说明之前已处理)
if grep -qF "$new" "$f" 2>/dev/null; then
continue
fi
if grep -qF "$old" "$f" 2>/dev/null; then
if $DRY_RUN; then
warn "[dry-run] $f: '$old' → '$new'"
else
sed -i "s|$old|$new|g" "$f"
info "$f: 替换完成"
fi
CHANGED=$((CHANGED + 1))
fi
done
}
# 收集目标文件
JS_FILES=( assets/*.js )
WORKER_FILES=( playground-worker-endpoint-*.js )
HTML_FILES=( *.html )
CLIENT_FILES=( client/index.js )
ALL_CODE=( "${JS_FILES[@]}" "${WORKER_FILES[@]}" "${HTML_FILES[@]}" "${CLIENT_FILES[@]}" )
echo "========================================="
echo " play.wenpai.net 本地化补丁"
echo " $(date '+%Y-%m-%d %H:%M')"
$DRY_RUN && echo " [DRY RUN 模式]"
echo "========================================="
echo ""
# ── 1. 域名替换 ──────────────────────────────────
echo "── 1. 域名替换 ──"
safe_replace "https://playground.wordpress.net" "https://play.wenpai.net" "${ALL_CODE[@]}"
safe_replace "playground.wordpress.net" "play.wenpai.net" "${ALL_CODE[@]}"
# ── 2. CORS 代理替换 ─────────────────────────────
echo ""
echo "── 2. CORS 代理替换 ──"
safe_replace "https://wordpress-playground-cors-proxy.net/?" "https://play.wenpai.net/cors-proxy/?" "${ALL_CODE[@]}"
# ── 3. API/下载镜像 ──────────────────────────────
echo ""
echo "── 3. API/下载镜像加速 ──"
safe_replace "https://api.wordpress.org" "https://api.wenpai.net" "${ALL_CODE[@]}"
safe_replace "http://api.wordpress.org" "https://api.wenpai.net" "${ALL_CODE[@]}"
safe_replace "https://downloads.wordpress.org" "https://downloads.wenpai.net" "${ALL_CODE[@]}"
# ── 4. GitHub 资源本地化 ─────────────────────────
echo ""
echo "── 4. GitHub 资源本地化 ──"
# 4a. 修复双重代理gh.wpcy.net 包裹 feicode URL 的情况)
safe_replace "https://gh.wpcy.net/https://feicode.com/" "https://feicode.com/" "${ALL_CODE[@]}"
# 4b. wp-cli.phar: 已有本地副本(也处理被 gh.wpcy.net 包裹的情况)
safe_replace "https://gh.wpcy.net/https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar" "/wp-cli.phar" "${ALL_CODE[@]}"
safe_replace "https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar" "/wp-cli.phar" "${ALL_CODE[@]}"
# 4c. 蓝图库索引: 镜像到本地
safe_replace "https://raw.githubusercontent.com/WordPress/blueprints/trunk/index.json" "https://play.wenpai.net/blueprints-index.json" "${ALL_CODE[@]}"
# 4d. 蓝图库内容: 本地 community-blueprints/(同源,无 CORS 问题)
safe_replace "https://feicode.com/WenPai-org/blueprints/raw/branch/trunk/blueprints/" "/community-blueprints/" "${ALL_CODE[@]}"
safe_replace "https://gh.wpcy.net/https://raw.githubusercontent.com/WordPress/blueprints/trunk/blueprints/" "/community-blueprints/" "${ALL_CODE[@]}"
safe_replace "https://raw.githubusercontent.com/WordPress/blueprints/trunk/blueprints/" "/community-blueprints/" "${ALL_CODE[@]}"
# 4e. 主题测试数据
safe_replace "https://raw.githubusercontent.com/WordPress/theme-test-data/master/themeunittestdata.wordpress.xml" "https://play.wenpai.net/data/themeunittestdata.wordpress.xml" "${ALL_CODE[@]}"
# 4f. trunk/nightly WordPress 下载
safe_replace "https://github.com/WordPress/WordPress/archive/refs/heads/master.zip" "/plugins/wordpress-trunk.zip" "${WORKER_FILES[@]}"
safe_replace "https://github.com/nicknisi/dotfiles/archive/refs/heads/main.zip" "/plugins/wordpress-trunk.zip" "${WORKER_FILES[@]}"
# 4g. 剩余动态 GitHub URL 走 gh.wpcy.net 代理
# 注意:必须在上面所有精确替换之后执行
safe_replace "https://raw.githubusercontent.com/" "https://gh.wpcy.net/https://raw.githubusercontent.com/" "${ALL_CODE[@]}"
safe_replace "https://github.com/WordPress/WordPress/archive/" "https://gh.wpcy.net/https://github.com/WordPress/WordPress/archive/" "${WORKER_FILES[@]}"
# ── 5. Google 依赖移除 ──────────────────────────
echo ""
echo "── 5. Google 依赖检查 ──"
for f in "${HTML_FILES[@]}"; do
[ -f "$f" ] || continue
if grep -q "googleapis.com\|googletagmanager.com\|fonts.gstatic.com" "$f" 2>/dev/null; then
if $DRY_RUN; then
warn "[dry-run] $f: 包含 Google 依赖"
else
# 移除包含 googleapis/googletagmanager/gstatic 的行(含跨行 href/src
sed -i '/fonts\.googleapis\.com/d' "$f"
sed -i '/googletagmanager\.com/d' "$f"
sed -i '/fonts\.gstatic\.com/d' "$f"
# 清理残留的空 link/script 开标签href 被删后剩下的)
sed -i '/<link[[:space:]]*$/d; /<script[[:space:]]*$/d' "$f"
info "$f: Google 依赖已移除"
fi
CHANGED=$((CHANGED + 1))
fi
done
# ── 6. wasm.wordpress.net 替换 ───────────────────
echo ""
echo "── 6. WASM 域名替换 ──"
safe_replace "https://wasm.wordpress.net" "https://play.wenpai.net" "${ALL_CODE[@]}"
# ── 汇总 ─────────────────────────────────────────
echo ""
echo "========================================="
if [ "$CHANGED" -gt 0 ]; then
if $DRY_RUN; then
warn "发现 $CHANGED 处需要修改"
else
info "完成 $CHANGED 处修改"
fi
else
info "所有文件已是最新状态,无需修改"
fi
echo "========================================="