Simple Javascript Web Bridge example?

Does anyone have a working example of a Javascript Web Bridge? The sample projects in the documentation have expired links so I can’t view them. I’ve tried the scripts provided there as well as my own at https://github.com/tatiang/scripts/blob/main/javascriptWebBridgeDemo.html but I don’t get a response in the app when previewed in a browser.

My Project

https://x.thunkable.com/copy/73b9f8b329d2e8bed0fd391f565e77ef

Code from Github link above:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Include the Thunkable Web Viewer Extension -->
    <script src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js" type="text/javascript"></script>
    <title>Thunkable Web Bridge Demo</title>
  </head>
  <body>
    <!-- Optional: Display received messages -->
    <span id="messageDisplay"></span>
  </body>
  <script type="text/javascript">
    // Function to handle incoming messages that expect a return value
    ThunkableWebviewerExtension.receiveMessageWithReturnValue(function(message, callback) {
      // Create the response by prefixing the received message with "demo: "
      const response = 'demo: ' + message;
      
      // Optionally display the received message on the webpage
      document.getElementById('messageDisplay').innerText = response;
      
      // Send the response back to the Thunkable app
      callback(response);
    });

    // Optional: Handle incoming messages that do not expect a return value
    ThunkableWebviewerExtension.receiveMessage(function(message) {
      // Display the received message with "demo: " prefix
      const response = 'demo: ' + message;
      document.getElementById('messageDisplay').innerText = response;
    });
  </script>
</html>

Hi @tatiang could you try and add a wait block before the call?
When I do that it works

The label still isn’t changing for me:

Maybe try using this url:
https://html-preview.github.io/?url=https://github.com/tatiang/scripts/blob/main/javascriptWebBridgeDemo.html

This one works:
https://rawcdn.githack.com/tatiang/scripts/99e8ee6dd2ddd91cb9e051c9564cab2117b794f7/javascriptWebBridgeDemo.html

I used this website to host your HTML file.

Thank you for your help with this. I was able to get a response from the Web Viewer (but not the Javascript bridge) using your hosted file. But I’m not sure why the labels’ text isn’t changing.

For the Post Message block, change it to Value from Post Message, then it should work!

I’m not sure why the Javascript Bridge doesn’t work, but you can still use the Webviewer for the same purpose

This isn’t working for me either:

Change the blocks in Screen 1 Opens, not what you did, and delete the receives message blocks. Then, it should work

Similar to this:

1 Like

Okay that works, thank you!

1 Like

Looks like I’m a little late to this and you got it figured out, but here is some more information that should help: GitHub - bliu95/webviewer-ext

It was made for web viewers but it works the same for js web bridges.

1 Like

Yes and no… I’ve had inconsistent results and I’m sure it’s my ignorance about Javascript and hosting scripts. The link is really helpful… thanks!

  1. In general, should my html-wrapped Javascript code work as a local asset or do I need to host it in the cloud?
  2. If I host it in the cloud, is it okay to use the Github share link that includes “blob” or do I need to use “raw” in the url? Or do I need to host it elsewhere?

I like setting up my code in github (since its free and pretty simple to use). If you are a coding expert and can do it in one go, I would say local is fine, but otherwise, I would recommend any cloud hosting. The reason is if you do local hosting, anytime you want to make even a minor code change, it will be a hassle. You have to delete the old one, and replace with a new one (and you have to use a different file name than the one one). With cloud or github, I can just push the new code and I know that it’ll reflect in Thunkable since the url doesn’t change.

For hosting, however you do it should be fine as long as it is publicly access-able. I’m using Github pages and that’s what I would recommend. If you are unsure, I would just try it in a webviewer and have it console log something just see if it is working

I agree. I definitely want to host it on Github if possible. I have a public repository but the Web viewer won’t load the page unless I host the file on https://rawcdn.githack.com. I’ll try a Github page. Thanks again.

Edit I can see the script file publicly (Test Page) now but this project is still not displaying any messages in a browser preview:

https://x.thunkable.com/copy/17d659bc6c2fb67ec3d20437000aab71