Supporting iframe embeds from different domains?

(Syed Salahuddin) #1

I’d like embed iframes from domains outside of instagram, youtube, etc. How would I go about doing that? I noticed some people talk about GitHub - discourse/onebox: A gem for turning URLs into website previews but it only supports html previews.


Embedding HTML inside posts by Admin
(Simon Cossar) #2

What I have done is make a plugin that calls the Discourse.Markdown.whiteListIframe function. The plugin is very simple, The hard part is that you have to supply regular expressions to match any URLs you want to white-list. The code is here. GitHub - scossar/whitelist-iframe: Discourse plugin to add urls to Discourse.Markdown._validIframes

You can test any URLs you wish to whitelist in your web browser’s console window. For example, if you open the browser’s console from a Discourse forum and then enter the following line, you should be able to embed iframes from Freesound.


There may be an easier way to do this. :slight_smile:

Embedding a book from Google Books
Allow embeds for hidden vimeo videos
(Syed Salahuddin) #3

thanks! this looks super straightforward! I’ll try it out!


(Bcguy) #5

Will this support video embeds from the New York Times?

Trying to embed this video:

<iframe title="New York Times Video - Embed Player" width="480" height="321" frameborder="0" scrolling="no" allowfullscreen="true" marginheight="0" marginwidth="0" id="nyt_video_player" src=""></iframe>

(Simon Cossar) #6

Yes it works if you add this line to iframe-whitelist.js :


(Bcguy) #7

Thanks Simon. I’m not a software engineer (not even close - a business guy). Help me a little more with this please…

Where do I find the “iframe-whitelist.js”


(Mittineague) #8

Not that I know, I haven’t tried. but are you sure? It doesn’t look the same as I see here (i.e. the URL is not …/video/players/)

<iframe width="480" height="373" frameborder="0" scrolling="no" marginheight="0"
 marginwidth="0" id="nyt_video_player"
 title="New York Times Video - Embed Player"

(Simon Cossar) #9

Yes, I based it off the source that @BCHK supplied and it works. Maybe the regular expression I am using is too specific though. Would there be any risk in just using this?


(Mittineague) #10

IMHO not any moreso than the other, They both end in "one or more anything characters"
i.e. It’s the “anything” that opens up the potential for problems, eg. If it is known that they always end with digits, I’d add that in.

Not that the problem would necessarily be a security risk. I think if someone posted a bad URL it would result in a broken link either way. Only difference is the onebox wouldn’t try with a bad URL

(Simon Cossar) #11

Are you able to install plugins? If so, it is here:

Right now I am just hardcoding urls into the plugin. It currently works for SoundCloud, FreeSound and now New York Times video. That’s not an ideal way to do it. It could probably be set up to allow acceptable urls to be added through the admin section.

(Bcguy) #12

Ah - haven’t done it yet - but probably time to learn. Thanks!

(Цунский Никита) #13

Hello, We need max width and length for iframe. What we can do, to realize this?
I have found this, but I’m not sure. I’m newbie in JS.
Somebody can help me?

(Mei) #14

Just learning to install plugins into Discourse. I got the plugin installed and it works! Now… How do you find the file in Digital Ocean so I can hardcode a new domain to whitelist? /var/discourse/plugins doesn’t exist…

(Simon Cossar) #15

To find the file on Digital Ocean, ssh into your droplet and then run:

$ cd /var/discourse
$ ./launcher enter app
$ cd var/www/discourse/plugins

The problem with changing things that way is that your changes will be overwritten every time you update Discourse. The right way to do it is to update the plugin file that is in the repository on github and then run launcher rebuild app in your droplet.

You can do that by cloning my repository on github and making the changes you want in your cloned version. You then need to change the url that you have added to the app.yml file so that it pulls from your new repository. If this isn’t clear, just let me know :slightly_smiling:

I can also just add the domain you want to whitelist to the plugin file that is in my repository.

(Mei) #16

Thanks @Simon_Cossar! I got it to work through cloning your repository :slight_smile:

(Marcos P) #17

i installed the plugin and i try add my iframe of discord with command:

<iframe src="" width="350" height="500" allowtransparency="true" frameborder="0"></iframe>

but not happen, how to insert to whitelist?

(Simon Cossar) #18

You will need to cone the plugin to your own github repository and add something like this to the iframe-whitelist.js file:

(function() {

  // whitelist discordapp

You will then need to use your own repository as the source for the plugin.

(Marcos P) #19

Nice, work perfectly, its amazing! Thanks!

@Simon_Cossar i rebuild app and inserted code again in iframe-whitelist.js file, but now, not working :pensive:

(Simon Cossar) #20

Which github repo do you have in your app.yml file?

(Marcos P) #21