fix(android): cross-origin Bearer auth in Tauri shell
Inject a Tauri shell script during CI prep to disable service workers and attach Bearer tokens on cross-origin API requests. Enable Android third-party cookies in MainActivity and eval the same script from Rust on page load.
This commit is contained in:
32
.github/scripts/inject-tauri-shell.mjs
vendored
Normal file
32
.github/scripts/inject-tauri-shell.mjs
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const root = path.resolve(__dirname, "../..");
|
||||
const MARKER = "data-web2app-tauri-shell";
|
||||
const scriptPath = path.join(root, "template", "scripts", "tauri-shell.js");
|
||||
|
||||
export function injectTauriShell(distDir) {
|
||||
const indexPath = path.join(distDir, "index.html");
|
||||
if (!fs.existsSync(indexPath)) return;
|
||||
|
||||
let html = fs.readFileSync(indexPath, "utf8");
|
||||
if (html.includes(MARKER)) return;
|
||||
|
||||
const script = fs.readFileSync(scriptPath, "utf8").trim();
|
||||
const tag = `<script ${MARKER}>${script}</script>`;
|
||||
|
||||
if (html.includes("<head>")) {
|
||||
html = html.replace("<head>", `<head>\n${tag}`);
|
||||
} else if (html.includes("</head>")) {
|
||||
html = html.replace("</head>", `${tag}\n</head>`);
|
||||
} else if (html.includes("</body>")) {
|
||||
html = html.replace("</body>", `${tag}\n</body>`);
|
||||
} else {
|
||||
html += `\n${tag}\n`;
|
||||
}
|
||||
|
||||
fs.writeFileSync(indexPath, html);
|
||||
console.log("Injected Tauri shell script into index.html");
|
||||
}
|
||||
@@ -32,6 +32,8 @@ function patchMainActivity(filePath) {
|
||||
const content = `package ${pkg}
|
||||
|
||||
import android.os.Bundle
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.WebView
|
||||
import androidx.core.view.WindowCompat
|
||||
|
||||
class MainActivity : TauriActivity() {
|
||||
@@ -39,6 +41,13 @@ class MainActivity : TauriActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
// 内容不延伸到状态栏 / 导航栏区域
|
||||
WindowCompat.setDecorFitsSystemWindows(window, true)
|
||||
CookieManager.getInstance().setAcceptCookie(true)
|
||||
}
|
||||
|
||||
override fun onWebViewCreate(webView: WebView) {
|
||||
super.onWebViewCreate(webView)
|
||||
// Android WebView 默认拦截跨站 Cookie;桌面壳请求远程 API 时需放行
|
||||
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true)
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
2
.github/scripts/prepare-template.mjs
vendored
2
.github/scripts/prepare-template.mjs
vendored
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
|
||||
import AdmZip from "adm-zip";
|
||||
import { BUNDLE_ICONS, generateAppIcons } from "./generate-app-icons.mjs";
|
||||
import { injectInAppNav } from "./inject-in-app-nav.mjs";
|
||||
import { injectTauriShell } from "./inject-tauri-shell.mjs";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const root = path.resolve(__dirname, "../..");
|
||||
@@ -109,6 +110,7 @@ function patchTauriConfig(filePath) {
|
||||
fs.writeFileSync(filePath, `${JSON.stringify(conf, null, 2)}\n`);
|
||||
}
|
||||
|
||||
injectTauriShell(distDir);
|
||||
injectInAppNav(distDir);
|
||||
|
||||
patchTauriConfig(confPath);
|
||||
|
||||
Reference in New Issue
Block a user