为旧版浏览器实现 ||= (object,object) 运算符

问题描述 投票:0回答:1

Android WebView 在使用 emscripten 生成的代码时遇到问题。当尝试加载页面时,它抱怨 ||= 字符串中的 = 是意外的。 ||= 是带赋值的逻辑或运算符。我正在锁定文档,以防两个操作数都是对象。 我认为它只是将右侧的每个属性复制到左侧并重叠,因此如果右侧对象的属性 a = 1 而左侧对象的属性 a = 2,则结果对象将具有 a = 1。我说得对吗?

编辑1:

我阅读了评论,谢谢,但我在添加对新运营商的支持方面遇到了麻烦。并非所有语言都允许定义自定义运算符,例如 nim。

我的实现如下:

object.prototype["||="] = function (b) {
    
      if (! this) {
        this.prototype = b;
      }
    }

在支持该算子的浏览器上不会产生错误,但在Android WebView上不生效。有办法解决这个问题吗?

编辑2:

我写了这段Java代码:

 try {
        if (extension != null) {

            if (extension.equals("js")) {
                byte bytes[] = new byte[999999];
              
                input.read(bytes);
                String content = new String(bytes, StandardCharsets.UTF_8);
               

                content.replaceAll("([a-zA-Z0-9\\.\\_]+) \\|\\|\\= (\\{((?!\\}\\;)|\n)+\\}\\;)",
                        "replaceEmpty(\\1, \\2)");
             
                input = new ByteArrayInputStream(content.getBytes());
                
            }
           
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
     
        }
    }
    catch (Exception a) {

    }
    return new WebResourceResponse(type, "UTF-8", input);

但是我可能在正则表达式上犯了错误。我可以使用带有替换的正则表达式(从输入到输出字符串的参数/字符串)吗?

我正在尝试替换字符串,例如:

Object1 ||= {
      prop1: "value",
      prop2: value2,
      subobject: {
      },
      array: [],
      ...
    };

所以我不能这样做,但我解决了我的问题。 代码

  InputStream input = activity.getAssets().open("Game" + url);
        //FileInputStream input = new FileInputStream(url);
                String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);

       try {
        if (extension != null) {
            if (extension.equals("js")) {
                byte[] bytes = new byte[999999];
                

                int length = input.read(bytes);

                bytes[length] ='\0';

                byte[] bytes2 = new byte[length];
                System.arraycopy(bytes,0,  bytes2, 0, length);
                String content = new String(bytes2, StandardCharsets.UTF_8);;
               

                content = content.replaceAll("([a-zA-Z0-9\\.\\_]+?) \\|\\|\\=", "if (! $1 ) $1  =");

  

                
                
                input = new ByteArrayInputStream(content.getBytes());
                
            }
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            }
    }
    catch (Exception a) {

    }

    return new WebResourceResponse(type, "UTF-8", input);

你可以阅读我使用过的正则表达式。

javascript documentation operator-keyword
1个回答
0
投票
class MainWebClient extends WebViewClient {
....

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 

try {
         // In case, when we use domain name to detect if resource is local
         url = url.substring(prefix.length() - 1);
         // Game could be directory in assets, when we kept our webpagee
         InputStream input = activity.getAssets().open("Game" + url);
            
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {

            if (extension.equals("js")) {
                byte bytes[] = new byte[999999];
              
                input.read(bytes);
                String content = new String(bytes, StandardCharsets.UTF_8);
               

                content.replaceAll("([a-zA-Z0-9\\.\\_]+) \\|\\|\\= (\\{((?!\\}\\;)|\n)+\\}\\;)",
                        "replaceEmpty(\\1, \\2)");
             
                input = new ByteArrayInputStream(content.getBytes());
                
            }
           
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
     
        }
    }
    catch (Exception a) {

    }
    return new WebResourceResponse(type, "UTF-8", input);
}
© www.soinside.com 2019 - 2024. All rights reserved.