2014-01-01 03:37:43 +08:00
|
|
|
(function() {
|
2015-06-10 04:24:04 +08:00
|
|
|
|
|
|
|
var DE = window.DiscourseEmbed || {};
|
|
|
|
var comments = document.getElementById('discourse-comments');
|
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
|
|
|
|
['discourseUrl', 'discourseEmbedUrl', 'discourseUserName'].forEach(function(i) {
|
|
|
|
if (window[i]) { DE[i] = DE[i] || window[i]; }
|
|
|
|
});
|
|
|
|
|
|
|
|
var queryParams = {};
|
|
|
|
|
|
|
|
if (DE.discourseEmbedUrl) {
|
2017-02-14 00:18:24 +08:00
|
|
|
if (DE.discourseEmbedUrl.indexOf('/') === 0) {
|
|
|
|
console.error("discourseEmbedUrl must be a full URL, not a relative path");
|
|
|
|
}
|
|
|
|
|
2015-06-10 04:24:04 +08: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-25 00:48:45 +08:00
|
|
|
}
|
2015-06-10 04:24:04 +08:00
|
|
|
|
|
|
|
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;
|
2014-01-01 03:37:43 +08:00
|
|
|
iframe.id = 'discourse-embed-frame';
|
|
|
|
iframe.width = "100%";
|
|
|
|
iframe.frameBorder = "0";
|
|
|
|
iframe.scrolling = "no";
|
|
|
|
comments.appendChild(iframe);
|
|
|
|
|
2014-01-04 03:45:22 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2014-01-01 03:37:43 +08:00
|
|
|
|
2016-04-27 03:03:44 +08:00
|
|
|
function normalizeUrl(url) {
|
2016-10-13 03:05:51 +08:00
|
|
|
return url.toLowerCase().replace(/^https?(\:\/\/)?/, '');
|
2016-04-27 03:03:44 +08:00
|
|
|
}
|
|
|
|
|
2014-01-01 03:37:43 +08:00
|
|
|
function postMessageReceived(e) {
|
|
|
|
if (!e) { return; }
|
2016-04-27 03:03:44 +08:00
|
|
|
if (normalizeUrl(DE.discourseUrl).indexOf(normalizeUrl(e.origin)) === -1) { return; }
|
2014-01-01 03:37:43 +08:00
|
|
|
|
|
|
|
if (e.data) {
|
|
|
|
if (e.data.type === 'discourse-resize' && e.data.height) {
|
|
|
|
iframe.height = e.data.height + "px";
|
|
|
|
}
|
2014-01-04 03:45:22 +08:00
|
|
|
|
|
|
|
if (e.data.type === 'discourse-scroll' && e.data.top) {
|
|
|
|
// find iframe offset
|
|
|
|
var destY = findPosY(iframe) + e.data.top;
|
|
|
|
window.scrollTo(0, destY);
|
|
|
|
}
|
2014-01-01 03:37:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener('message', postMessageReceived, false);
|
|
|
|
|
|
|
|
})();
|