Browse Source

fix: geckoview loadUri works but http server wrong

ryanemax 1 week ago
parent
commit
ddc38b49fd

+ 41 - 16
6.2.1/capacitor/src/main/java/com/getcapacitor/Bridge.java

@@ -71,8 +71,10 @@ import org.mozilla.geckoview.GeckoSession;
 import org.mozilla.geckoview.GeckoSessionSettings;
 import org.mozilla.geckoview.GeckoView;
 import org.mozilla.geckoview.WebExtension;
+import org.mozilla.geckoview.ContentBlocking;
 
 import fi.iki.elonen.NanoHTTPD;
+
 /**
  * The Bridge class is the main engine of Capacitor. It manages
  * loading and communicating with all Plugins,
@@ -211,7 +213,7 @@ public class Bridge implements IPostMessage {
         AppCompatActivity context,
         ServerPath serverPath,
         Fragment fragment,
-        WebView webView,
+        GeckoView webView,
         List<Class<? extends Plugin>> initialPlugins,
         List<Plugin> pluginInstances,
         MockCordovaInterfaceImpl cordovaInterface,
@@ -338,6 +340,7 @@ public class Bridge implements IPostMessage {
                 appUrl += "/";
             }
         }
+        // appUrl = "https://app.fmode.cn/dev/english/?v=20250410";
 
         String appUrlPath = this.config.getStartPath();
         if (appUrlPath != null && !appUrlPath.trim().isEmpty()) {
@@ -347,17 +350,18 @@ public class Bridge implements IPostMessage {
         final boolean html5mode = this.config.isHTML5Mode();
 
         // Start the local web server
-        JSInjector injector = getJSInjector();
-        if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
-            String allowedOrigin = Uri.parse(appUrl).buildUpon().path(null).fragment(null).clearQuery().build().toString();
-            try {
-                WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(allowedOrigin));
-                injector = null;
-            } catch (IllegalArgumentException ex) {
-                Logger.warn("Invalid url, using fallback");
-            }
-        }
-        localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode);
+        // JSInjector injector = getJSInjector();
+        // if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
+        //     String allowedOrigin = Uri.parse(appUrl).buildUpon().path(null).fragment(null).clearQuery().build().toString();
+        //     try {
+        //         WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(allowedOrigin));
+        //         injector = null;
+        //     } catch (IllegalArgumentException ex) {
+        //         Logger.warn("Invalid url, using fallback");
+        //     }
+        // }
+        // localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode);
+        localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode);
         localServer.hostAssets(DEFAULT_WEB_ASSET_DIR);
 
         Logger.debug("Loading app at " + appUrl);
@@ -599,6 +603,12 @@ public class Bridge implements IPostMessage {
     public WebviewExtension getWebView() {
         return this.webviewExtension;
     }
+    // public GeckoCustomWebViewExtension getWebView() {
+    //     if (this.webviewExtension == null) {
+    //         return null;
+    //     }
+    //     return new GeckoCustomWebViewExtension(this.webviewExtension);
+    // }
 
     /**
      * Get the URI that was used to launch the app (if any)
@@ -695,8 +705,8 @@ public class Bridge implements IPostMessage {
             Logger.debug("WebView background color not applied");
         }
 
-        settings.setDisplayZoomControls(false);
-        settings.setBuiltInZoomControls(this.config.isZoomableWebView());
+        // settings.setDisplayZoomControls(false);
+        // settings.setBuiltInZoomControls(this.config.isZoomableWebView());
 
         if (config.isInitialFocus()) {
             // webView.requestFocusFromTouch();
@@ -730,6 +740,7 @@ public class Bridge implements IPostMessage {
                 appUrl += "/";
             }
         }
+        // appUrl = "https://app.fmode.cn/dev/english/?v=20250410";
 
         String appUrlPath = this.config.getStartPath();
         if (appUrlPath != null && !appUrlPath.trim().isEmpty()) {
@@ -1111,7 +1122,8 @@ public class Bridge implements IPostMessage {
             String cordovaJS = JSExport.getCordovaJS(context);
             String cordovaPluginsJS = JSExport.getCordovaPluginJS(context);
             String cordovaPluginsFileJS = JSExport.getCordovaPluginsFileJS(context);
-            String localUrlJS = "window.WEBVIEW_SERVER_URL = '" + localUrl + "';";
+            String localUrlJS = "window.WEBVIEW_SERVER_URL = '" + localUrl + "';"+"Services.prefs.setBoolPref('services.settings.remote-settings.enabled', false);";
+            String remoteDisableJS = "Services.prefs.setBoolPref('services.settings.remote-settings.enabled', false);";
 
             return new JSInjector(globalJS, bridgeJS, pluginJS, cordovaJS, cordovaPluginsJS, cordovaPluginsFileJS, localUrlJS);
         } catch (Exception ex) {
@@ -1494,7 +1506,8 @@ public class Bridge implements IPostMessage {
      */
     public void setServerBasePath(String path) {
         localServer.hostFiles(path);
-        webView.post(() -> webView.loadUrl(appUrl));
+        webviewExtension.getWebview().post(() -> this.webviewExtension.getSession().loadUri(appUrl));
+        // webView.post(() -> webView.loadUrl(appUrl));
     }
 
     /**
@@ -1690,6 +1703,14 @@ public class Bridge implements IPostMessage {
                     .webManifest(false)
                     .aboutConfigEnabled(false)
                     .remoteDebuggingEnabled(true)
+                    .contentBlocking(new ContentBlocking.Settings.Builder()
+                        .antiTracking(ContentBlocking.AntiTracking.NONE) // Disable tracking protection
+                        .safeBrowsing(ContentBlocking.SafeBrowsing.NONE)
+                        .build()
+                    )
+                    .aboutConfigEnabled(true)     // 启用 about:config 修改验证配置
+                    .allowInsecureConnections(1)  // 允许自签名证书,允许未签名https
+                    // .remoteSettingsEnabled(false)  // 禁用远程设置,避免运行时报错,网络无法访问http://remote-settings.content-signature.mozilla.org/
                     .build();
 
             GeckoRuntime sRuntime = GeckoRuntime
@@ -1715,6 +1736,10 @@ public class Bridge implements IPostMessage {
             // }
 
             webView.setSession(bridge.getWebView().getSession());
+            // bridge.getWebView().getSession().evaluateJavaScript(
+            //     "Services.prefs.setBoolPref('services.settings.remote-settings.enabled', false);",
+            //     null
+            // );
             webView.getSession().setProgressDelegate(new GeckoSession.ProgressDelegate() {
                 @Override
                 public void onSessionStateChange(@NonNull GeckoSession session, @NonNull GeckoSession.SessionState sessionState) {

+ 48 - 0
6.2.1/capacitor/src/main/java/com/getcapacitor/GeckoCustomWebViewExtension.java

@@ -0,0 +1,48 @@
+// package com.getcapacitor;
+
+// import android.view.ViewParent;
+// import android.view.View;
+// import android.view.View.OnTouchListener;
+
+// public class GeckoCustomWebViewExtension extends WebviewExtension {
+//     private final WebviewExtension original;
+
+//     public GeckoCustomWebViewExtension(WebviewExtension original) {
+//         this.original = original;
+//     }
+
+//     // 仅补全缺失的方法(不重复父类已有的方法)
+//     @Override
+//     public ViewParent getParent() {
+//         if (original instanceof View) {
+//             return ((View) original).getParent();
+//         }
+//         return null;
+//     }
+
+//     @Override
+//     public void setBackgroundColor(int color) {
+//         if (original instanceof View) {
+//             ((View) original).setBackgroundColor(color);
+//         }
+//     }
+
+//     @Override
+//     public void setClickable(boolean clickable) {
+//         if (original instanceof View) {
+//             ((View) original).setClickable(clickable);
+//         }
+//     }
+
+//     @Override
+//     public void setOnTouchListener(OnTouchListener listener) {
+//         if (original instanceof View) {
+//             ((View) original).setOnTouchListener(listener);
+//         }
+//     }
+
+//     // 代理 getWebview()(如果父类没有,但 Bridge.java 需要)
+//     // public WebviewExtension getWebview() {
+//     //     return original != null ? original : this;
+//     // }
+// }