-1

Android WebView have trouble with emscripten generated code. When trying to load page, it complains = is unexpected in ||= string. ||= is logical OR operator with assignment. I am locking for documentation in case, where both operands are objects. I think it simply copy each property of right-side onto left-side with overlap, so if right object had property a = 1 and left object had a = 2, result object will have a = 1. Have I right?

Edit 1:

I read comments, thanks, but I have trouble with add support for new operator. Not all languages allowing to define custom operators, like nim.

My implementation looks like:

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

It does not generate errors on browsers, which supports this operator, but do not take effect on Android WebView. Some way to solve this?

Edit 2:

I wrote this Java code:

 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);

But I probably makes mistake with regexp. Could I use regexp with substitutions (parameters/string from input to output string)?

I am trying to replace string like:

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

So I cannot do this, but I solved my issue. Code

  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);

You can read regular expression I've used.

5
  • 2
    a ||= b is essentially the same as a = a || b. Commented Feb 2, 2024 at 18:59
  • 1
    It doesn't copy properties. It's simply using b as a default value for a if a is not initialized. Commented Feb 2, 2024 at 20:23
  • I've read stackoverflow.com/questions/20728460/… adding custom operator is not possible on JS. Any way to solve this Android WebView's problem? Commented Feb 3, 2024 at 12:33
  • Transpile or write portable js Commented Feb 3, 2024 at 12:35
  • No, you cannot define custom operators in JS. Commented Feb 3, 2024 at 13:01

1 Answer 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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.