Behavior of iframes within oneboxes in emails


(Felix Freiberger) #1

I’ve developed a small Onebox plugin that embeds an interactive element when certain links are posted. They look like this:

This is implemented by inserting an iframe into the onebox:

Obviously, this won’t work in emails and needs special handling. There already is some special handling for this by default – the iframe is replaced by a paragraph with a very strange link inside it:

What is causing this replacement? How can I modify how the onebox behaves in email? Can I simply disable it for emails and show the original link instead?


(Simon Cossar) #2

It’s all happening in lib/email/styles.rb.

  def onebox_styles
     ...
      # iframes can't go in emails, so replace them with clickable links
      @fragment.css('iframe').each do |i|
        begin
          src_uri = URI(i['src'])

          # If an iframe is protocol relative, use SSL when displaying it
          display_src = "#{src_uri.scheme || 'https://'}#{src_uri.host}#{src_uri.path}"
          i.replace "<p><a href='#{src_uri.to_s}'>#{display_src}</a><p>"
        rescue URI::InvalidURIError
          # If the URL is weird, remove it
          i.remove
        end
      end

You can add a plugin_callback to modify the html.

    # this method is reserved for styles specific to plugin
    def plugin_styles
      @@plugin_callbacks.each { |block| block.call(@fragment, @opts) }
    end

Unfortunately, plugin_styles is called after onebox_styles in the format_html method. If that order was reversed you could do what you want.

Doing any work with this file is slow because the methods are being cached by Sidekiq. To get around that I’ve been requiring sidekiq/testing at the start of 100-sidekiq.rb instead of sidekiq/pausable. To do that you have to comment out calls to Sidekiq.pause! in restorer.rb and backuper.rb.

The other option is to restart sidekiq every time you change the code.


(Felix Freiberger) #3

Thank you so much!

[quote=“Simon_Cossar, post:2, topic:40160”]

display_src = "#{src_uri.scheme || 'https://'}#{src_uri.host}#{src_uri.path}"
```[/quote]

This is the culprit and generated the incorrect `httpspseuco.com`-link. I think this is a general bug and [proposed a fix](https://github.com/discourse/discourse/pull/4042) – with this, my emails should at least look okay.