I, along with my users, prefer the way an Instagram embed looks compared to the onebox of an Instagram image/post. With their embed you can tell right away that you’re looking at an Instagram image, and the full caption is shown.
I was wondering if it’s possible to build a plugin that overrides the native Discourse onebox for Instagram and replaces it with the regular Instagram embed? I have been looking to get my feet wet into Discourse development and I figure this would be a fairly simple project, if it’s doable.
This is the process I came up with:
- Plugin is installed and enabled
- User adds their Instagram API authentication credentials to the plugin’s settings
- Add Instagram to the onebox domains blacklist Discourse admin setting (not sure if this can be automated when the plugin is enabled or if it has to be done manually by the user)
- Plugin detects Instagram link in post content upon saving a post (I would prefer not to use a custom BBCode but if that’s the only possible way then I’ll do that)
- Plugin makes call to the Instagram API with the URL it detected and obtains the embed code
- Embed code is saved into the cooked post
For reference, this is what the embed code looks like:
<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-permalink="https://www.instagram.com/p/Bi44Jb6Da4n/" data-instgrm-version="8" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);">
<div style="padding:8px;">
<div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:62.5% 0; text-align:center; width:100%;">
<div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;">
</div>
</div>
<p style=" margin:8px 0 0 0; padding:0 4px;">
<a href="https://www.instagram.com/p/Bi44Jb6Da4n/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;" target="_blank">When you don’t hear Laurel or Yanny so you don’t care about Laurel or Yanny... #dontaskmeagainsusan #laurelsoundlikeababetho #hitmeuplaurel #illbeyoyanny</a>
</p>
<p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">A post shared by <a href="https://www.instagram.com/waltergeoffreythefrenchie/" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px;" target="_blank"> •w a l t e r•</a> (@waltergeoffreythefrenchie) on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2018-05-17T18:31:44+00:00">May 17, 2018 at 11:31am PDT</time>
</p>
</div>
</blockquote>
<script async defer src="//www.instagram.com/embed.js"></script>
Here are two problems I see regarding the embed code:
- The plugin will need to whitelist a lot of those tags/attributes. For that, I will reference this topic: https://meta.discourse.org/t/whitelisting-some-html-tags/24280
- The script tag is not allowed, and I’m not sure if it can be whitelisted. If not, I could possibly add it before the footer? It’s just 3kb so I don’t care if it’s loaded when it’s not necessary.
Any and all feedback regarding this would be greatly appreciated! Thanks in advance!