菜鸟教程小白 发表于 2022-12-13 05:20:20

ios - JavaFX WebView - 如何捕获 URL 加载?


                                            <p><p>我目前正在使用 GluonHQ (JavaFXPorts) 开发一个应用程序,我正在使用 WebView 加载一些 Internet 页面。当我加载 <a href="https://signin.ebay.com/" rel="noreferrer noopener nofollow">https://signin.ebay.com/</a>页面我在控制台上看到以下消息</p>

<pre><code> I can handle this protocol for https://signin.ebay.com/
ES2ResourceFactory: Prism - createStockShader: AlphaTexture_RadialGradient.frag
I can handle this protocol for https://www.ebay.com/ws/
I can&#39;t handle this protocol for about:blank
I can handle this protocol for https://www.ebay.com/t_n.html?org_id=usllpic0&amp;session_id=f5b8d48d1670a9cce3431193fffe9431&amp;suppressFlash=true
I can&#39;t handle this protocol for about:blank
2018-12-28 18:49:53 Watchdog WF: _userSettingsForUser mobile: {
    filterBlacklist =   (
    );
    filterWhitelist =   (
    );
    restrictWeb = 1;
    useContentFilter = 0;
    useContentFilterOverrides = 0;
    whitelistEnabled = 0;
}
2018-12-28 18:49:53 Watchdog WF: _WebFilterIsActive returning: NO
2018-12-28 18:49:55 Watchdog CoreAnimation: was called from a non-main thread in an implicit transaction! Note that this may be unsafe without an explicit CATransaction or a call to .
I can handle this protocol for https://src.ebay-us.com/fp/top_fp.html;CIS3SID=08561C52B9A266EE68861D27BCE99A74?org_id=usllpic0&amp;session_id=f5b8d48d1670a9cce3431193fffe9431&amp;nonce=b6190f233a143165
</code></pre>

<p>似乎该应用程序也调用了其他站点的负载,但我找不到捕获它们的方法。我也尝试在状态更改时获取位置</p>

<pre><code>webEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -&gt; {
    System.out.println(&#34;State(&#34; + newState + &#34;): &#34; + webEngine.getLocation());
}
</code></pre>

<p>但没有任何成功。在第一次加载站点之前,该位置似乎为空,然后返回原始 URL,即 <a href="https://signin.ebay.com" rel="noreferrer noopener nofollow">https://signin.ebay.com</a> .我相信出现的额外“协议(protocol)”是 AJAX 的一部分。任何人都知道如何实现监听器来捕获这些?</p>

<pre><code> I can handle this protocol for https://signin.ebay.com/
State(SCHEDULED): null
State(RUNNING):   null
ES2ResourceFactory: Prism - createStockShader: AlphaTexture_RadialGradient.frag
I can handle this protocol for https://www.ebay.com/ws/
I can&#39;t handle this protocol for about:blank
State(SCHEDULED): null
State(RUNNING):   null
State(SUCCEEDED): null
I can handle this protocol for https://www.ebay.com/t_n.html?org_id=usllpic0&amp;session_id=f5c7ab7f1670a9ccaac210b2ffe8709e&amp;suppressFlash=true
State(SCHEDULED): https://signin.ebay.com
State(RUNNING):   https://signin.ebay.com
I can&#39;t handle this protocol for about:blank
State(SCHEDULED): https://signin.ebay.com
State(RUNNING):   https://signin.ebay.com
State(SUCCEEDED): https://signin.ebay.com
2018-12-28 19:06:05 Watchdog WF: _userSettingsForUser mobile: {
    filterBlacklist =   (
    );
    filterWhitelist =   (
    );
    restrictWeb = 1;
    useContentFilter = 0;
      useContentFilterOverrides = 0;
      whitelistEnabled = 0;
    }
2018-12-28 19:06:05 Watchdog WF: _WebFilterIsActive returning: NO
2018-12-28 19:06:09 Watchdog CoreAnimation: was called from a non-main thread in an implicit transaction! Note that this may be unsafe without an explicit CATransaction or a call to .
I can handle this protocol for https://src.ebay-us.com/fp/top_fp.html;CIS3SID=D553152B77DCBE35641E606BDAB6AFDA?org_id=usllpic0&amp;session_id=f5c7ab7f1670a9ccaac210b2ffe8709e&amp;nonce=f3e7bb25c25eb5c1
State(SCHEDULED): https://signin.ebay.com
State(RUNNING):   https://signin.ebay.com
State(SUCCEEDED): https://signin.ebay.com
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>作为类似 <a href="https://stackoverflow.com/questions/53958332/javafx-webview-progress-always-goes-from-0-0-to-1-0-no-intermediate-values" rel="noreferrer noopener nofollow">question</a> 的后续行动,其中指出:</p>

<blockquote>
<p>With JavaFXPorts, on mobile (both Android and iOS) the WebView control is not the JavaFX built-in control but the native control</p>
</blockquote>

<p>在原生<a href="https://bitbucket.org/javafxports/8u-dev-rt/src/7bb974e4073cae570e6106deb4afff9b56d96dd5/modules/web/src/ios/native/WebViewImpl.m?at=default#lines-195" rel="noreferrer noopener nofollow">implementation</a>中可以看到这个方法:</p>

<pre><code>- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *url = [ absoluteString];
    JNIEnv *env = ;
    if (env != NULL) {
      jstring jUrl = createJString(env, url);
      (*env)-&gt;CallVoidMethod(env, jObject, jmidHasAppProtocolHandler, jUrl);
    ...
}
</code></pre>

<p>进入 JavaFX <code>WebEngine</code> <a href="https://bitbucket.org/javafxports/8u-dev-rt/src/7bb974e4073cae570e6106deb4afff9b56d96dd5/modules/web/src/ios/java/javafx/scene/web/WebEngine.java?at=default#lines-947" rel="noreferrer noopener nofollow">callback</a> :</p>

<pre><code>/**
* check if there is a handler registered for dealing with this protocol.
*/
boolean hasAppProtocolHandler(String url) {
    boolean answer = false;
    try {
      URL u = newURL(null, url);
      System.out.println (&#34; I can handle this protocol for &#34;+url);
      u.openConnection();
      // location.set(url);
      answer = true;
    }
    // catch (MalformedURLException e) {
    catch (Exception e) {
      System.out.println (&#34; I can&#39;t handle this protocol for &#34;+url);
      // no handler known for this protocol
    }
    return answer;
}
</code></pre>

<p>这解释了控制台打印输出:何时可以打开 URL,或者何时出现异常。所有不同的结果似乎都与 native 浏览器处理 URL 加载的方式有关,但您只会在 JavaFX 层中得到回声。</p>

<p>关于使用<code>WebEngine::getLocation</code>,注意只有当 native 浏览器加载完一个Url后,才会发回通知,<a href="https://bitbucket.org/javafxports/8u-dev-rt/src/7bb974e4073cae570e6106deb4afff9b56d96dd5/modules/web/src/ios/java/javafx/scene/web/WebEngine.java?at=default#lines-917" rel="noreferrer noopener nofollow">location is set</a> :</p>

<pre><code>void notifyLoadFinished(String loc, String content) {
    synchronized (loadedLock) {
      this.pageContent = &#34;&lt;html&gt;&#34;+content+&#34;&lt;/html&gt;&#34;;
      loaded = true;
      updateProgress(1.0);
      updateState(Worker.State.SUCCEEDED);
      location.set(loc); // &lt;--- location from native browser is set
      document.invalidate(true);
      if (pageListener != null) {
            pageListener.onLoadFinished();
      }
    }
}
</code></pre>

<p>我建议您查看适用于 iOS 平台的 <code>WebView</code> 和 <code>WebEngine</code> 的 JavaFX 代码,了解哪些可以做,哪些不能做。</p>

<p>例如,脚本引擎可以工作,所以您也许可以 <a href="https://bitbucket.org/javafxports/8u-dev-rt/src/7bb974e4073cae570e6106deb4afff9b56d96dd5/modules/web/src/ios/java/javafx/scene/web/WebEngine.java?at=default#lines-608" rel="noreferrer noopener nofollow">execute</a>一些将报告一些信息的脚本。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - JavaFX WebView - 如何捕获 URL 加载?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53962027/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53962027/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - JavaFX WebView - 如何捕获 URL 加载?