2020-10-16 11:47:59 -04:00
|
|
|
(function () {
|
2015-06-09 16:24:04 -04:00
|
|
|
var DE = window.DiscourseEmbed || {};
|
2020-10-16 11:47:59 -04:00
|
|
|
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];
|
|
|
|
}
|
2015-06-09 16:24:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
var queryParams = {};
|
|
|
|
|
|
|
|
if (DE.discourseEmbedUrl) {
|
2022-07-17 14:16:39 -04:00
|
|
|
if (DE.discourseEmbedUrl.startsWith("/")) {
|
2020-10-16 11:47:59 -04:00
|
|
|
console.error(
|
|
|
|
"discourseEmbedUrl must be a full URL, not a relative path"
|
|
|
|
);
|
2017-02-13 11:18:24 -05:00
|
|
|
}
|
|
|
|
|
2015-06-09 16:24:04 -04:00
|
|
|
queryParams.embed_url = encodeURIComponent(DE.discourseEmbedUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DE.discourseUserName) {
|
|
|
|
queryParams.discourse_username = DE.discourseUserName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DE.topicId) {
|
|
|
|
queryParams.topic_id = DE.topicId;
|
2014-04-24 12:48:45 -04:00
|
|
|
}
|
2015-06-09 16:24:04 -04:00
|
|
|
|
2020-10-16 11:47:59 -04:00
|
|
|
var src = DE.discourseUrl + "embed/comments";
|
2015-06-09 16:24:04 -04:00
|
|
|
var keys = Object.keys(queryParams);
|
|
|
|
if (keys.length > 0) {
|
|
|
|
src += "?";
|
|
|
|
|
2020-10-16 11:47:59 -04:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
src += "&";
|
|
|
|
}
|
2015-06-09 16:24:04 -04:00
|
|
|
|
|
|
|
var k = keys[i];
|
|
|
|
src += k + "=" + queryParams[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.src = src;
|
2020-10-16 11:47:59 -04:00
|
|
|
iframe.id = "discourse-embed-frame";
|
2013-12-31 14:37:43 -05:00
|
|
|
iframe.width = "100%";
|
|
|
|
iframe.frameBorder = "0";
|
|
|
|
iframe.scrolling = "no";
|
2020-10-16 11:47:59 -04:00
|
|
|
iframe.referrerPolicy =
|
|
|
|
DE.discourseReferrerPolicy || "no-referrer-when-downgrade";
|
2013-12-31 14:37:43 -05:00
|
|
|
comments.appendChild(iframe);
|
|
|
|
|
2014-01-03 14:45:22 -05:00
|
|
|
// Thanks http://amendsoft-javascript.blogspot.ca/2010/04/find-x-and-y-coordinate-of-html-control.html
|
2020-10-16 11:47:59 -04:00
|
|
|
function findPosY(obj) {
|
2014-01-03 14:45:22 -05:00
|
|
|
var top = 0;
|
2020-10-16 11:47:59 -04:00
|
|
|
if (obj.offsetParent) {
|
|
|
|
while (1) {
|
|
|
|
top += obj.offsetTop;
|
|
|
|
if (!obj.offsetParent) break;
|
|
|
|
obj = obj.offsetParent;
|
|
|
|
}
|
|
|
|
} else if (obj.y) {
|
|
|
|
top += obj.y;
|
2014-01-03 14:45:22 -05:00
|
|
|
}
|
|
|
|
return top;
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
|
2016-04-26 15:03:44 -04:00
|
|
|
function normalizeUrl(url) {
|
2020-10-16 11:47:59 -04:00
|
|
|
return url.replace(/^https?(\:\/\/)?/, "");
|
2016-04-26 15:03:44 -04:00
|
|
|
}
|
|
|
|
|
2013-12-31 14:37:43 -05:00
|
|
|
function postMessageReceived(e) {
|
2020-10-16 11:47:59 -04:00
|
|
|
if (!e) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-17 14:48:36 -04:00
|
|
|
if (!normalizeUrl(DE.discourseUrl).includes(normalizeUrl(e.origin))) {
|
2020-10-16 11:47:59 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
|
|
|
|
if (e.data) {
|
2020-10-16 11:47:59 -04:00
|
|
|
if (e.data.type === "discourse-resize" && e.data.height) {
|
2013-12-31 14:37:43 -05:00
|
|
|
iframe.height = e.data.height + "px";
|
|
|
|
}
|
2014-01-03 14:45:22 -05:00
|
|
|
|
2020-10-16 11:47:59 -04:00
|
|
|
if (e.data.type === "discourse-scroll" && e.data.top) {
|
2014-01-03 14:45:22 -05:00
|
|
|
// find iframe offset
|
|
|
|
var destY = findPosY(iframe) + e.data.top;
|
|
|
|
window.scrollTo(0, destY);
|
|
|
|
}
|
2013-12-31 14:37:43 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-16 11:47:59 -04:00
|
|
|
window.addEventListener("message", postMessageReceived, false);
|
2013-12-31 14:37:43 -05:00
|
|
|
})();
|