【问题标题】:How to detect the completion of a Google Form in a Webview如何在 Web 视图中检测 Google 表单的完成情况
【发布时间】:2020-02-24 15:38:12
【问题描述】:

我正在 web 视图中加载 Google 表单,并且需要知道用户何时完成表单。 表单可能有一个或多个部分(页面)。

我尝试了什么

解析 URL 以获取表单提交事件独有的信息:
网址在表单首页以https://.../viewform 结尾
该 URL 在表单提交感谢页面上以 https://.../formResponse 结尾,但也开始第二页和之后的每一页(以及返回页面时)。
所以这个方法只适用于单页表单。

【问题讨论】:

    标签: android ios webview google-forms


    【解决方案1】:

    简化版

    override fun onPageFinished(view: WebView, url: String?) {
        super.onPageFinished(view, url)
                webView.evaluateJavascript("document.getElementsByClassName('freebirdFormviewerViewResponseConfirmContentContainer').length > 0") {
                    val done = it == "true"
        }
            
    }
    

    【讨论】:

      【解决方案2】:

      Google 表单“谢谢”页面有一个独特的 div .freebirdFormviewerViewResponseConfirmContentContainer
      通过在页面加载时在 webview 中注入 javascript,我们可以检测到这个 div 的存在,并从我们的 JavascriptInterface 调用一个方法来通知原生代码表单已提交:

      注意:这仅适用于直接在 web 视图中加载 Google 表单的情况。如果 Google 表单通过 iframe 嵌入到网站中,这将不起作用(我发现在这种情况下无法检测到该提交事件)。

      (以Java代码为例)

      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
      {
          ...
          mWebView.setWebViewClient(new WebViewClient()
              {
                  @Override
                  public void onPageFinished(WebView view, String url)
                  {
                      String js =
                          "( " +
                          "function() { " +
                          "if(document.getElementsByClassName('freebirdFormviewerViewResponseConfirmContentContainer').length > 0) {" +
                          WEBVIEW_INTERFACE_NAME + ".googleFormSubmitted();" +
                          "}" +
                          "}) " +
                          "()";
      
                  if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
                      mWebView.evaluateJavascript(js, null);
                  else
                      mWebView.loadUrl("javascript:" + js);
                  }
              }
          );
      
          if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
              mWebView.addJavascriptInterface(new WebViewInterface(getContext()), WEBVIEW_INTERFACE_NAME);
          }
      }
      
      private class WebViewInterface
      {
              Context mContext;
      
              WebViewInterface(Context context)
              {
                  mContext = context;
              }
      
              @JavascriptInterface
              public void googleFormSubmitted()
              {
                  // Do what you need
              }
      }
      

      【讨论】:

        【解决方案3】:

        这只是一夜之间改变。 @Julian Honma 的答案仍然是正确的,但据我所知,类名现在是 vHW8K,至少在我使用的形式中是这样。

        【讨论】:

          猜你喜欢
          • 2019-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-19
          • 1970-01-01
          • 1970-01-01
          • 2015-11-05
          • 1970-01-01
          相关资源
          最近更新 更多