sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    WebDialog on Mac (call to Google SU engineers)

    Scheduled Pinned Locked Moved Developers' Forum
    25 Posts 10 Posters 8.4k Views 10 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • fredo6F Offline
      fredo6
      last edited by

      I have been making some tests on how Web dialogs behave on Mac platform (via the kind support from Regis and Edson, as I don't have a Mac myself).

      I found out that there is a big discrepancy of behavior between Windows and Mac, about the communication between Javascript and Ruby, back and forth(so execute_script and callback via window.location=skp:callback@....).

      Communication Ruby <-> Javascript are Synchronous on Windows, whereas they are Asynchronous on Mac.

      As a result, if on Mac you call in sequence 2 javascripts:

      
        dlg.execute_script "document.getElementById('Field1').value = '1' ; "
        dlg.execute_script "document.getElementById('Field2').value = '2' ; "
      
      

      then,

      • the first Ruby statement returns even before its javascript function starts executing
      • Most likely, only the second javascript function will be executed, because it will override the first one.

      On Windows, it works fine. Each 'execute_script' Ruby statement will only return when the javascript function has finished execution.

      The same happens in the other direction, when Javascript is calling back Ruby.
      Let's say that in Ruby, you have

      
      dlg.add_action_callback("callback1") { |d, p| self.j_callback1 p }
      dlg.add_action_callback("callback2") { |d, p| self.j_callback2 p }
      
      

      and in the Javascript section, you have the 2 statements:

      
      window.location = 'skp;callback1@' + 'event 1'
      window.location = 'skp;callback2@' + 'event 2'
      
      

      then, Callback1 will never be called.

      Again, all this works fine in Windows. No callback call is lost, and they are sent in the right order.

      This is terribly ennoying, because it makes almost impossible to use SU Web Dialogs on Mac to built rich dialog boxes. The reason is that, to drive a rich dialog based on user actions, you'll have constantly to communicate back and forth between Javascript and Ruby, to set values of fields, or to get events (like onchange, onkeydown, ...). With nothing synchronous and many actions lost in transmission, the code will never fly.

      I wanted to know:

      1. if SU Developers acknowledge of this problem, or if I am not correct.
      2. if there is a simple workaround or something specific to do on Mac
      3. if there is a fix planned

      All this is related to make sure that I can release scripts both for Windows and Mac users.

      Thanks in advance

      Fredo

      1 Reply Last reply Reply Quote 0
      • R Offline
        RickW
        last edited by

        They have been aware of problems, but you have done a great job detailing the exact behavior. Waiting for a fix...

        RickW
        [www.smustard.com](http://www.smustard.com)

        1 Reply Last reply Reply Quote 0
        • scottliningerS Offline
          scottlininger
          last edited by

          Hey Fredo,

          As Rick said, this is a known bug to us, and a very annoying one. I wish I could say there's an easy workaround, but the behavior, I have been told via folks at Apple, is a fundamental WebKit problem. (We are still pursuing a fix.)

          To answer your questions:

          1. You're absolutely right.

          2. There is not a simple workaround. The only way to make this work is to build your javascript code to be asynchronous. As you're probably discovering, this leads to JS code that is necessarily more complex. If you think of SU as a "server" that you're making asynchronous requests to, you can use code patterns that you might be familiar with from Ajax programming. You can build complex UIs this way, but it's not easy.

          3. A fix for this is at the top of my personal list, but I do not expect a solution in time for SU7. No promises... but maybe we'll have a fix for an early maintenance release. Another thing we've considered is either building or working with the community to build a ruby/js bridge library that abstracts some of this, but it's just design discussions at this point.

          Hope that helps. When SU7 goes live I'll be able to provide a more in depth discussion of this whole issue.

          Cheers,

          • Scott Lininger
            SketchUp Software Engineer
            Have you visited the Ruby API Docs?
          1 Reply Last reply Reply Quote 0
          • fredo6F Offline
            fredo6
            last edited by

            Scott,Welcome to the forum. It's good to hear from the SU Google team, furthermore if you are specialist of the Ruby API.For the WebDialog issue on Mac, this is because Safari (Konqueror and Webkit actually) performs all redirections in asynchonous mode (so Window.location or anchor href jumps). On Windows / IE window.location is synchronous.I am surprised however that, despite of this, SU cannot do internally what you suggest the scripters shoudl do (which will be damned complex, because Ajax works mainly with HTTP requests, meaning that I don't know too much how you can support the skp: listener ; in addition Safari is known to have major issues with Synchronous requests). I may have found a workaround which I am testing currently. If this works, I'll tell you more.Note that this is in no way for very complex web dialogs. Just think of trapping key events typed in the web dialog. With the current Mac problem, you will only receive the last event (Key Up) when you type a key, all others being swallowed.I also suggest that for SU7, even if you can't fix the problem, you explore additional verbs like ElementSetValue, because most of the execute_script are actually just to put data in HTML forms. Also, would need to have extra APIs like GetSize and GetLocation.

            Again, that was a genious idea to plug Javascript and web dialog box in SU / Ruby, but the current implementation of SU6 has really so many issues that I am not sure we'll see many scripts coming with WD.Fredo

            1 Reply Last reply Reply Quote 0
            • M Offline
              mocathe1st
              last edited by

              Hi All,

              I've currently run into the synchronisation problem described above. The JavaScript file I have currently written is fairly simple, and only passes messages from Flash via JavaScript to Ruby. I've got a single function to take care of this. However, when I send a quick succession of messages from Flash to Ruby via JS most of the commands get lost on the Mac, this works fine on the PC.

              I was just wondering if anyone found a solution to the synchronisation problem, is there a way to create a JavaScript buffer or a way of synchronising the messages?

              Best Regards,
              Malcolm Murray

              1 Reply Last reply Reply Quote 0
              • chrisglasierC Offline
                chrisglasier
                last edited by

                @mocathe1st said:

                Hi All,

                I've currently run into the synchronisation problem described above. The JavaScript file I have currently written is fairly simple, and only passes messages from Flash via JavaScript to Ruby. I've got a single function to take care of this. However, when I send a quick succession of messages from Flash to Ruby via JS most of the commands get lost on the Mac, this works fine on the PC.

                I was just wondering if anyone found a solution to the synchronisation problem, is there a way to create a JavaScript buffer or a way of synchronising the messages?

                Best Regards,
                Malcolm Murray

                Please take a look at this topic which includes the following:

                @unknownuser said:

                Also included is revisions to code to enable testing Mac compatibility, viz:

                START control employed as no Sketchup.active_model with Mac when Ruby scripts are first loaded. (related post)

                Code rewritten so that no window.location call is followed by any javascript statements. (related post)

                Feedback from Mac users and/or coders would be much appreciated.

                together with a download of an example.

                I think it very important for the expansion of webdialog use to find and publish the best common solution. I don't expect the SU team has this at the top of their list.

                I look forward to further discussion.

                My regards

                Chris

                With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  @chrisglasier said:

                  As a result, if on Mac you call in sequence 2 javascripts:

                  Code: Select all
                  dlg.execute_script "document.getElementById('Field1').value = '1' ; "
                  dlg.execute_script "document.getElementById('Field2').value = '2' ; "

                  then,

                  • the first Ruby statement returns even before its javascript function starts executing
                  • Most likely, only the second javascript function will be executed, because it will override the first one.

                  On Windows, it works fine. Each 'execute_script' Ruby statement will only return when the javascript function has finished execution.

                  I've been doing some tests on OSX, using the console. First I create a new WebDialog object:

                  
                  w=UI;;WebDialog.new('test', true)
                  w.set_html('<html><body>Hello</body><html>')
                  w.show
                  
                  

                  Then I try with a loop to send lots of .execute_script commands.

                  
                  w.execute_script('document.body.innerHTML="";')
                  (0..100).each{|i| w.execute_script('document.body.innerHTML="<br>Hello #{i}";') }
                  
                  

                  When I run the script, it adds all the lines "Hello 1" to "Hello 100" - not a single .execute_script is lost...
                  Also, the ruby method did not return until it was all done.

                  Thomas Thomassen — SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    Testing the other way - making a loop in the Javascript to do 100 window.location = 'skp:callback' and I only register the last callback. All the rest appear to have been suppressed. I only see the async going from the WebDialog to Ruby. Not the other way around.

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      Here's a sample code I used to test:


                      webdialog.rb

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        I improved the test. Now the Ruby to WebDialog method creates a new DIV element every time, and it insert the DIV into the last inserted DIV. So if the calls are synchronous - all the elements should be nested.

                        On both PC and OSX - the elements are nested. It still appears to me that it's just the Webdialog to Ruby path that's async. Which means it's related to window.location.


                        webdialog.zip

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • jeff hammondJ Offline
                          jeff hammond
                          last edited by

                          i have no idea what you're actually talking about thomas but i'm glad you are 😄 👍
                          i fear the day of a great plugin coming out only to hear this once again : "sorry, pc only"

                          dotdotdot

                          1 Reply Last reply Reply Quote 0
                          • chrisglasierC Offline
                            chrisglasier
                            last edited by

                            @thomthom said:

                            window.location

                            Yes that's the culprit as I understand what Fredo said and you have demonstrated (to yourself).

                            With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              @chrisglasier said:

                              @thomthom said:

                              window.location

                              Yes that's the culprit as I understand what Fredo said and you have demonstrated (to yourself).

                              No, he also described .execute_script to be async - which I can't see. From my tests it returns after the Javascript is done, not before.

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • fredo6F Offline
                                fredo6
                                last edited by

                                @thomthom said:

                                @chrisglasier said:

                                @thomthom said:

                                window.location

                                Yes that's the culprit as I understand what Fredo said and you have demonstrated (to yourself).

                                No, he also described .execute_script to be async - which I can't see. From my tests it returns after the Javascript is done, not before.

                                Tom,

                                There are 2 issues which all boil down to the problem of windows.location being asynchronous.

                                1. when you execute a script that should call a Ruby call back, the script returns before the call back is executed. This is unfortunately a design problem due to Safari, which I understand the SU team cannot do anything about easily.

                                2. in addition, the skp protocol eats up all callbacks and keep the last one, when sent in sequence. So you simply miss some callbacks, even if they were to be recieved asynchronously. This is a problem of implementation by the SU team (it would have deserved a buffering).

                                I had to find workarounds for both problems in my web dialog implementation in LibFredo6, precisely by buffering the callbacks in a hidden HTML field and synchronizing the Ruby code.

                                If I have some time, I will check if I could isolate the code for reuse by others. No promise however.

                                Fredo

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  @unknownuser said:

                                  1. when you execute a script that should call a Ruby call back, the script returns before the call back is executed. This is unfortunately a design problem due to Safari, which I understand the SU team cannot do anything about easily.

                                  Ah, ok. So the .execute_script is synchronous then. It's just the Javascript to Ruby callback (the skp: protocol) that's async.

                                  So it's then safe to call .execute_script which calls a javascript method that stores the return data in a hidden input field where you then use .get_element_value to return data, correct? This is what I see from my testing - but I just want to make sure I'm not missing some cases here.

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • fredo6F Offline
                                    fredo6
                                    last edited by

                                    @thomthom said:

                                    So it's then safe to call .execute_script which calls a javascript method that stores the return data in a hidden input field where you then use .get_element_value to return data, correct? This is what I see from my testing - but I just want to make sure I'm not missing some cases here.

                                    Correct. This is what should be done to solve problem #1.
                                    Can you confirm however, because it's been a while since I did not work on Web dialogs, and I don't want to mislead anybody.

                                    Fredo

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      I've tried with loops of .execute_string followed immediately by .get_element_value where I call a Javascript function to modify an argument I pass to it and fetch the result with .get_element_value. Works fine on PC and OSX.

                                      
                                      // #bar is a hidden INPUT element
                                      function foo(i)
                                      {
                                        document.getElementById('bar').valie = i * i;
                                      }
                                      
                                      
                                      
                                      (2..10).each { |i|
                                        dialog.execute_script("foo(#{i});")
                                        x = dialog.get_element_value('bar')0
                                        puts "i; #{i} - x; #{x}"
                                      }
                                      
                                      

                                      This seems to work fine.

                                      Thomas Thomassen — SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • R Offline
                                        RonB
                                        last edited by

                                        Sorry for exhuming this thread, but is there a workaround available now?
                                        Actually i'm facing the same problem as described here and its totally driving me nuts.

                                        I'm hoping for good news.

                                        1 Reply Last reply Reply Quote 0
                                        • thomthomT Offline
                                          thomthom
                                          last edited by

                                          Depends on what you are doing.

                                          One way is to make SketchUp pump for information instead of Javascript pushing information.

                                          Another way, if you need Javascript to push information is to set up a pump-system. You create a stack (an array) of the callbacks from JS to SU you want to perform. A function then takes the first item on the stack and send it to SketchUp. It then waits for SketchUp to send notification back that the message was received. Then the next item on the stack is sent ... and so on.

                                          It requires you to restructure how you pass the information around.

                                          If you give a specific usage example we could probably come up with suitable design.

                                          Thomas Thomassen — SketchUp Monkey & Coding addict
                                          List of my plugins and link to the CookieWare fund

                                          1 Reply Last reply Reply Quote 0
                                          • R Offline
                                            RonB
                                            last edited by

                                            hey thomthom, thanks for your quick response.

                                            I do have a website providing two informations for sketchup

                                            1. the actual needed version of the plugin (provided by hidden input field)

                                            2. a stream (string) of informations (provided by hidden input field)

                                            3. needs to be checked directly when the webdialog loads the site and 2) when a button is clicked (because it can be chosen which set of informations to load).

                                            Actually it is working on a PC but not on a Mac.
                                            The Website contains both window.location skp callbacks but the second one is only triggered by button.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement