discourse/public/javascripts/embed.js
Rafael dos Santos Silva 3893e55bd5
FEATURE: Full Ember app embed mode for blog comments (#36613)
## Summary

Adds a new `fullApp` option to the embed snippet that renders the
complete Discourse Ember application in the iframe instead of the
simplified Rails-templated view. This allows embedded comments to have
the full Discourse experience including likes, reactions, and inline
replies.

When `fullApp: true` is set:
- `/embed/comments` redirects to the topic URL with `?embed_mode=true`
- Header, sidebar, and footer are hidden
- All links open in new tabs (to avoid navigating away within the
iframe)
- Iframe is scrollable with configurable height (default 600px)

Feature is gated on a hidden site setting, default disabled, named `embed_full_app`

## Demo

https://discourse-full-embed.pages.dev/embed-test


## Usage

```js
DiscourseEmbed = {
  discourseUrl: 'https://forum.example.com/',
  discourseEmbedUrl: 'EMBED_URL',
  fullApp: true,
  // embedHeight: '800px',  // optional, defaults to 600px
};
```

## Security

The `embed_mode` parameter only removes the X-Frame-Options header if:
- `embed_any_origin` site setting is enabled, OR
- The request referer matches a configured embeddable host

This matches the existing security model for `/embed/comments`.
2026-02-18 18:22:57 -03:00

112 lines
2.6 KiB
JavaScript

(function () {
var DE = window.DiscourseEmbed || {};
var comments = document.getElementById("discourse-comments");
var iframe = document.createElement("iframe");
[
"discourseUrl",
"discourseEmbedUrl",
"discourseUserName",
"discourseReferrerPolicy",
].forEach(function (i) {
if (window[i]) {
DE[i] = DE[i] || window[i];
}
});
var queryParams = {};
if (DE.discourseEmbedUrl) {
if (DE.discourseEmbedUrl.startsWith("/")) {
console.error(
"discourseEmbedUrl must be a full URL, not a relative path"
);
}
queryParams.embed_url = encodeURIComponent(DE.discourseEmbedUrl);
}
if (DE.discourseUserName) {
queryParams.discourse_username = DE.discourseUserName;
}
if (DE.topicId) {
queryParams.topic_id = DE.topicId;
}
if (DE.className) {
queryParams.class_name = DE.className;
}
if (DE.fullApp) {
queryParams.full_app = "true";
}
var src = DE.discourseUrl + "embed/comments";
var keys = Object.keys(queryParams);
if (keys.length > 0) {
src += "?";
for (var i = 0; i < keys.length; i++) {
if (i > 0) {
src += "&";
}
var k = keys[i];
src += k + "=" + queryParams[k];
}
}
iframe.src = src;
iframe.id = "discourse-embed-frame";
iframe.width = "100%";
iframe.frameBorder = "0";
iframe.scrolling = DE.fullApp ? "yes" : "no";
if (DE.fullApp) {
iframe.style.height = DE.embedHeight || "600px";
}
iframe.referrerPolicy =
DE.discourseReferrerPolicy || "no-referrer-when-downgrade";
comments.appendChild(iframe);
// Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html
function findPosY(obj) {
var top = 0;
if (obj.offsetParent) {
while (1) {
top += obj.offsetTop;
if (!obj.offsetParent) break;
obj = obj.offsetParent;
}
} else if (obj.y) {
top += obj.y;
}
return top;
}
function normalizeUrl(url) {
return url.replace(/^https?(\:\/\/)?/, "");
}
function postMessageReceived(e) {
if (!e) {
return;
}
if (!normalizeUrl(DE.discourseUrl).includes(normalizeUrl(e.origin))) {
return;
}
if (e.data) {
if (e.data.type === "discourse-resize" && e.data.height && !DE.fullApp) {
iframe.height = e.data.height + "px";
}
if (e.data.type === "discourse-scroll" && e.data.top) {
// find iframe offset
var destY = findPosY(iframe) + e.data.top;
window.scrollTo(0, destY);
}
}
}
window.addEventListener("message", postMessageReceived, false);
})();