sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Strategy for passing quotes to a webdialog?

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 211 Views 3 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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      I'm finally doing a project with webdialogs and finally beginning to grasp what is happening. And I am passing a string to the webdialog and it is full of lengths and things that have double quote and single quote characters in them from the foot and inch values from SketchUp. How do I go about safely getting that data to my webdialog? The quotes are really messing my string up.

      So what strategy should I use? Do I convert the lengths into floats first so the quote marks get removed and then re-add them back in the web dialog? Or is there another way to define a string in java script other than with quotes so it will leave my string alone?

      Chris

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

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

        Ensure you escape the quotes in the string you pass on to the webdialog.

        String.inspect is a handy function to automatically escape double quotes and other special characters.

        Also - using alternative quote delimiters in Ruby aids to reduce confusion when it comes to handling quotes.

        my_string = %{Length: 12"5'} my_escaped_string = my_string.inspect webdialog.execute_script( %{do_something("my_escaped_string");} )

        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 made myself a wrapper to simplify the executing of Javascript function. In fact - I made it so that I get return values from Javascript functions:

          <span class="syntaxdefault"><br />  </span><span class="syntaxcomment"># Wrapper to build a script string and return the return value of the called<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># Javascript.<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># This method also ensures a that the +<SCRIPT>+ elements which<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># +UI;;WebDialog.execute_script+ leaves behind is cleaned up.<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#  return_value = window.call_script('alert', 'Hello World')<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># @deprecated Not fully implemented yet!<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># @param [String] function Name of JavaScript function to call.<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># @return [Mixed]<br /></span><span class="syntaxdefault">  </span><span class="syntaxcomment"># @since 2.5.0<br /></span><span class="syntaxdefault">  def call_script</span><span class="syntaxkeyword">(function,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">*</span><span class="syntaxdefault">args</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Ensure that we don't pull old data back from the WebDialog if it should fail.<br /></span><span class="syntaxdefault">    self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Bridge.reset()'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># (!) SU-0415<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Reports of .execute_script might have a hard limit - possibly under OSX only.<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Windows does seem unaffected. <br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Test case;<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#  w.execute_script("alert('#{'x'*10000000}'.length);")<br /></span><span class="syntaxdefault">    arguments </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> args</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">map </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">arg</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> TT</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Javascript</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_js</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">arg</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}.</span><span class="syntaxdefault">join</span><span class="syntaxkeyword">(</span><span class="syntaxstring">','</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    javascript </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#{function}(#{arguments});"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inspect<br />    </span><span class="syntaxcomment"># If WebDialog is not visible, or no HTML is populated (lacking DOM) then<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># .execute_script returns false.<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># (i) OSX - SU6<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># http://forums.sketchucation.com/viewtopic.php?f=180&t=8316#p49259<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># Indicates that ; might cause the call to fail. Seems to work without,<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># so keeping it like that to be on the safe size.<br /></span><span class="syntaxdefault">    if not self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">execute_script</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">"Bridge.execute(#{javascript})"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">      raise </span><span class="syntaxstring">"Script could not be executed. Was window visible? (#{self.visible?})"<br /></span><span class="syntaxdefault">    end<br />    </span><span class="syntaxcomment"># (?) Catch JavaScript errors? Or just let the WebDialog display the error?<br /></span><span class="syntaxdefault">    raw_data </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_element_value</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'RUBY_bridge'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># The JS Bridge converts the JS values into Ruby code strings.<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># (?) Catch exceptions? Re-raise with custom exception?<br /></span><span class="syntaxdefault">    eval</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> raw_data </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  end<br /></span>
          

          The method is an extra method I added to a custom class that extends WebDialog. So I execute JS script like this:
          return_value = webdialog.call_script('my_js_function', 5, false, 'Hello')

          Class: TT::GUI::Window — Documentation by YARD 0.9.9

          favicon

          (www.thomthom.net)

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

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            If you use str=e.length.to_s it'll keep the format with ' & " etc - e.g. 1' 2".
            You can pass that as a property argument to an innerHTML or innerTEXT etc using a javascript string you execute in the dialog. If you are using ' ' as the enclosing quotes for the argument use str.gsub!(/'/,"\\\\'") on the string; if you are using " " use str.gsub!(/"/,"\\\"") to escape the equivalent character in the string - giving ' or " resepctively [i.e. 1\' 2" or 1' 2\"]...
            See here too http://forums.sketchucation.com/viewtopic.php?p=334009#p334009 and other posts in that thread...

            TIG

            1 Reply Last reply Reply Quote 0
            • Chris FullmerC Offline
              Chris Fullmer
              last edited by

              Oh this stuff looks great guys, thanks! I'll probably get a chance to test it tonight,

              Chris

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

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

              Advertisement