Data Transmission

SketchUp's WebDialog#execute_script method can be used to transmit data (eg, dynamically-generated HTML content). Just send and execute a JavaScript string assignment, etc:

js = "var content = '... some content ...'; " +
     "$('...pattern...').html(content);"
wd.execute_script(js)

Unfortunately, this technique will break (silently) if handed too large a string. I don't know the exact limit, but naive testing found that 20 lines of JavaScript transmitted cleanly, but 50 lines did not. So, for example, this code should fail:

# Assumptions:
#   The dialog is stored in a variable named wd.
#   The client DOM contains a div with id='foo'.
#   The client has loaded jQuery.

line  = '123456789 ' * 7 + "\n"
lines = line * 100
patt  = 'div#foo'
js    = ("var content = '#{lines}'; " +
        ("$('#{patt}').html(content);").gsub(/\n/, ' ')
wd.execute_script(js)

Anyway, here's a script that I use to work around the problem:

  def push_html(dialog, patt, html)
    ## hand HTML to WebDialog in small blocks

    lim   = 10
    lines = html.split("\n");
    size  = lines.size
    quot  = size / lim
    mod   = size % lim
    blks  = []

    0.upto(quot-1) do |n|                 # Collect full blocks.
      beg   = n * lim
      blks << lines[beg, lim].join("\n")
    end

    if mod > 0                            # Add partial block, if any.
      beg   = quot * lim
      blks << lines[beg..-1].join("\n")
    end

    blks.each_with_index do |blk,ndx|     # Push all blocks.
      js  = (ndx == 0) ? "portmanteau = ''; " : ''
      js += "portmanteau += '#{blk}';".gsub(/\n/, ' ')
      dialog.execute_script(js)
    end

    # Replace HTML.

    js  = "$('#{patt}').html(portmanteau);"
    dialog.execute_script(js)
  end


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r4 - 02 Jan 2012, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email