# Behavior of iframes within oneboxes in emails

**URL:** https://meta.discourse.org/t/behavior-of-iframes-within-oneboxes-in-emails/40160
**Category:** Development
**Created:** [2016年二月25日 14:31 UTC](https://meta.discourse.org/t/behavior-of-iframes-within-oneboxes-in-emails/40160 "2016-02-25T14:31:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2016年二月25日 14:31 UTC](https://meta.discourse.org/t/behavior-of-iframes-within-oneboxes-in-emails/40160/1 "2016-02-25T14:31:20Z")

</div>

I’ve developed [a small Onebox plugin](https://github.com/fefrei/discourse-pseuco-onebox/blob/master/plugin.rb) that embeds an interactive element when certain links are posted. They look like this:

 ![](https://global.discourse-cdn.com/meta/original/3X/5/9/5942ad6f4c9069f9ece0d313200060545e3a5b12.png)

This is implemented by inserting an iframe into the onebox:

 ![](https://global.discourse-cdn.com/meta/original/3X/f/e/fe636091116b4d8d85442dc33dd1201491785fbe.png)

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:

 ![](https://global.discourse-cdn.com/meta/original/3X/4/7/47d6b181f1bf9ac9e0f1547c37bf5544caf72ca2.png)

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?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2016年二月25日 17:12 UTC](https://meta.discourse.org/t/behavior-of-iframes-within-oneboxes-in-emails/40160/2 "2016-02-25T17:12:09Z")

</div>

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

```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.

```rb
    # 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.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [2016年二月25日 18:45 UTC](https://meta.discourse.org/t/behavior-of-iframes-within-oneboxes-in-emails/40160/3 "2016-02-25T18:45:08Z")

</div>

Thank you so much!

[quote=“Simon\_Cossar, post:2, topic:40160”]

````ruby
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.
````
