notion-zh_CN icon indicating copy to clipboard operation
notion-zh_CN copied to clipboard

汉化脚本

Open unknowissue opened this issue 10 months ago • 17 comments

仅适用win:

逻辑

  1. 读取环境变量 $username = $env:USERNAME
  2. 读取文件 C:\Users\$username\AppData\Roaming\Notion\latestVersion.json 中 version 字段
  3. 根据 version 字段的值,是否存在以下两个文件 C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2\$version\assets_assets\index-en-US-***.html C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2\$version\assets_assets\localeSetup-zh-CN-***.js 4.如果存在这两个文件, 读取 C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2\$version\assets_assets\localeSetup-zh-CN-***.js 的文件名 并复制给 $name_localeSetup-zh-CN
  4. 得到以下字符串
'
  1. 在文件 C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2\$version\assets_assets\index-en-US-***.html 将<!doctype html> 替换为
'

另外,由于win安全策略设置,需要用bat启动powershell脚本

实现:

D:\soft\notion_zh\mod_zh-ch.ps1

# 读取当前用户名环境变量
$username = $env:USERNAME

# 更新 Notion 版本文件的路径
$jsonFilePath = "C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2\latestVersion.json"
$assetsDir = "C:\Users\$username\AppData\Roaming\Notion\notionAssetCache-v2"

# 获取当前日期,格式为 yyyyMMddHHmmss
$date = Get-Date -Format "yyyyMMddHHmmss"

# 读取 JSON 文件中的 version 字段
if (Test-Path $jsonFilePath) {
    $version = (Get-Content $jsonFilePath | ConvertFrom-Json).version
} else {
    Write-Error "Version file does not exist"
    exit
}

# 定义需要检查的文件路径
$htmlFilePath = "$assetsDir\$version\assets\_assets\index-en-US-*.html"
$jsFilePath = "$assetsDir\$version\assets\_assets\localeSetup-zh-CN-*.js"

# 检查是否存在 HTML 和 JS 文件
$htmlFile = Get-ChildItem $htmlFilePath -ErrorAction SilentlyContinue
$jsFile = Get-ChildItem $jsFilePath -ErrorAction SilentlyContinue

if ($htmlFile -and $jsFile) {
    # 定义要替换的 HTML 内容
    $name_localeSetup = $jsFile.Name
    $newHeadHtml = "<!doctype html><html class=""notion-html""><head lang=""en""><script defer=""defer"" src=""/_assets/$name_localeSetup""></script>"

    # 创建备份文件
    $backupFile = $htmlFile.DirectoryName + "\bak_" + $date + "_" + $htmlFile.Name
    Copy-Item $htmlFile.FullName -Destination $backupFile

    # 读取 HTML 文件内容
    $htmlContent = Get-Content $htmlFile.FullName -Raw

    # 检查是否已包含新的头部 HTML
    if ($htmlContent -notmatch $newHeadHtml) {
        $htmlContent = $htmlContent -replace '<!doctype html><html class="notion-html"><head lang="en">', $newHeadHtml
        Set-Content $htmlFile.FullName -Value $htmlContent
        Write-Output "HTML file updated and backup created."
    } else {
        Write-Output "No changes needed. The HTML file already contains the required script tag."
    }
} else {
    Write-Output "Required files do not exist."
}




Start-Process -FilePath "C:\Users\$username\AppData\Local\Programs\Notion\Notion.exe"  -ArgumentList "--proxy-server=socks5://127.0.0.1:8181  —enable-features=OverlayScrollbar"  -NoNewWindow

D:\soft\notion_zh\start_notion.bat

powershell -ExecutionPolicy RemoteSigned  -File D:\soft\notion_zh\mod_zh-ch.ps1

unknowissue avatar Apr 29 '24 04:04 unknowissue