Google Maps API latest release and support for nullish coalescing

76 views
Skip to first unread message

Dino

unread,
Aug 20, 2023, 9:01:11 AM8/20/23
to CEF Python
I have been using cefpython for several years.  We primarily access a one of our own web pages that includes Google maps.  Starting late last week the webpage fails to load in cefpython, even though it still loads in Chrome.

The reason appears to be that the latest release of the Google Map Javascript API uses nullish coalescing syntax (a??b) implemented in ES2020 which is not supported in cefpython.

My options, at least as far as I have understood them so far, seem very limited:

1.  I understand that there is no polyfill for ?? and it can only be implemented by transpiling, which I dont think is an option for Google's code

2.  An ES2020 compliant version of CEF.  It doesn't appear that is likely anytime soon

3.  Stop using Google Maps API and replace it with an alternative.

Am I misunderstanding my options? Has anyone else run across a similar ES2020 issue and found an alternative approach?  

Alireza Kazemi

unread,
Jul 27, 2025, 6:05:38 AMJul 27
to CEF Python
Hi, 
In addition to nullish coalescing `a ?? b`, optional chaining `a?.b` and `a?.[prop]` and `a?.(params)` as are available from ES2020, are making problems.
DeepSeek suggests that at least requires cef v88.

The quick fix is to modify the pages JS codes via python handler callback replacing ?? and .? using regexp for at least simple single usages. For more complex cases of chained `.?` and `??`, more sophisticated transpilation / text processing may be needed.

```
import re
from cefpython3 import cefpython as cef
class JSFixHandler(cef.ResourceHandler):
    def __init__(self):
        super(JSFixHandler, self).__init__()
        self.buffer = bytearray()
        self.content_length = 0
        self.headers = {}
    def ProcessRequest(self, request, callback):
        # Only intercept JavaScript files
        url = request.GetUrl()
        if url.endswith(".js"):
            # Continue loading the resource normally
            callback.Continue()
            return True
        return False
    def GetResponseHeaders(self, response):
        response.SetStatus(200)
        response.SetMimeType("application/javascript")
        response.SetHeader("Content-Length", str(len(self.buffer)))
    def ReadResponse(self, data_out, bytes_to_read, callback):
        # Send modified JS in chunks
        bytes_read = min(bytes_to_read, len(self.buffer) - self.read_offset)
        if bytes_read > 0:
            data_out[:bytes_read] = self.buffer[self.read_offset:self.read_offset+bytes_read]
            self.read_offset += bytes_read
        callback.Continue()
        return bytes_read
    def OnResourceLoadComplete(self, browser, frame, request, response, status, received_content_length):
        # 1. Get original JS content
        js_content = self.buffer.decode("utf-8", errors="replace")
        # 2. Apply regex replacements for modern syntax
        fixed_js = self.fix_modern_js(js_content)
        # 3. Update buffer with modified JS
        self.buffer = bytearray(fixed_js, "utf-8")
        self.read_offset = 0
    def fix_modern_js(self, js):
        # Regex replacements targeting problematic modern syntax
        patterns = [
            # Optional chaining: obj?.prop -> obj && obj.prop
            (r"(\w+)\?\.(\w+)", r"\1 && \1.\2"),
            # Optional member: obj?.[expr] -> obj && obj[expr]
            (r"(\w+)\?\.\[([^\]]+)\]", r"\1 && \1[\2]"),
            # Nullish coalescing: a ?? b -> (a !== null && a !== undefined) ? a : b
            (r"(\w+)\s*\?\?\s*(\w+)", r"((\1 !== null && \1 !== void 0) ? \1 : \2)")
        ]
        for pattern, replacement in patterns:
            js = re.sub(pattern, replacement, js)
        return js
# Register handler
request_handler = cef.RequestHandler()
def get_resource_handler(browser, frame, request):
    if request.GetUrl().endswith(".js"):
        return JSFixHandler()
request_handler.GetResourceHandler = get_resource_handler
```


There is a branch cefpython123 in the github repo mentioned to work for py 3.10 and 3.11 on windows. But that requires build setups, would that some one build and release for that.
Reply all
Reply to author
Forward
0 new messages