【问题标题】:Android webview - single line javascript comments causing Uncaught SyntaxError errors?Android webview - 单行javascript注释导致Uncaught SyntaxError错误?
【发布时间】:2014-10-21 19:50:20
【问题描述】:

我正在尝试将以下 html 作为字符串加载到 webview 中:

<html>
  <head>
    <script>
      function foo() {
        // test.
      }
    </script>
  </head>
  <body>
    <p>hi.</p>
  </body>
</html>

------------------------------

String content = readAboveContentIntoString();
WebView webview = ...;
webview.loadData(content, "text/html", "utf-8");

我从 webview 控制台收到以下消息:

Uncaught SyntaxError: Unexpected end of input

如果我删除“//测试”。评论,我没有收到语法错误。就好像 webview 正在剥离换行符,因此函数体将注释应用到右大括号,如下所示:

function foo() { // test. }

其他人可以复制吗?我想也许我的 readAboveContentIntoString() 正在剥离换行符,但经过测试,事实并非如此。我使用的是安卓 4.4.4。

谢谢

-- 编辑 ---

此外,块注释可以很好地代替行注释:

/* test. */

【问题讨论】:

  • 与最新的 Android Studio 版本和 API 21 有同样的问题
  • 该故障已确认存在并在 API 23 中上升到 webview/Chromium 44。我建议主演 the issue,因为它被认为是“过时的”?

标签: android webview android-webview


【解决方案1】:

我遇到了同样的问题。似乎唯一的方法是先从内容字符串中删除 cmets,然后将其加载到 webview。

String content = readAboveContentIntoString();
WebView webview = ...;

// Add This :
content = removeComment(content);

webview.loadData(content, "text/html", "utf-8");

函数 removeComment() 删除单行 cmets 和块 cmets。

private String removeComment(String codeString){

    int pointer;
    int[] pos;
    String str = codeString;

    while(true) {

        pointer = 0;
        pos = new int[2];
        pos[0] = str.indexOf("/*",pointer);
        pos[1] = str.indexOf("//",pointer);
        int xPos = xMin(pos);

        if(xPos != -1){

            //========================= Pos 0
            if(xPos == pos[0]){
                pointer = xPos + 2;
                int pos2 = str.indexOf("*/", pointer);
                if(pos2 != -1){
                    str = str.substring(0,xPos) + str.substring(pos2+2,str.length());
                }
                else{
                    str = str.substring(0,xPos);
                    break;
                }
            }
            //========================= Pos 1
            if(xPos == pos[1]){
                pointer = xPos + 2;
                int pos2 = str.indexOf('\n', pointer);
                if(pos2 != -1){
                    str = str.substring(0,xPos) + str.substring(pos2+1,str.length());
                }
                else{
                    str = str.substring(0,xPos);
                    break;
                }
            }
        }
        else break;
    }
    return str;
}

private int xMin(int[] x){
    int out = -1;

    for(int i = 0;i < x.length;i++){
        if(x[i] > out)out = x[i];
    }
    if(out == -1)return out;

    for(int i = 0;i < x.length;i++){
        if(x[i] != -1 && x[i] < out)out = x[i];
    }

    return out;
}

【讨论】:

    【解决方案2】:

    您的 Java 字符串在注释行之后是否包含换行符?如果不是,则第一个“//”之后的 javascript 元素中的所有内容都将被解释为未终止的单行注释,因为它在“// test”之后没有找到“\n”。

    请参阅本期评论 5: https://issuetracker.google.com/issues/36937564#comment5

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 2011-10-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多