flarum-msteams-webhook/flarum-notifiy-v2.js

120 lines
2.5 KiB
JavaScript

(function () {
'use strict';
var forumOrigin = window.location.origin;
function showTarget(targetValue) {
var targetUrl;
var forumLink;
var targetUrlLink;
var postPanel;
var fallbackPanel;
if (!targetValue) {
return false;
}
try {
targetUrl = new URL(targetValue);
if (targetUrl.origin !== forumOrigin) {
console.warn(
'Rejected forum target with unexpected origin:',
targetUrl.origin
);
return false;
}
if (targetUrl.pathname.indexOf('/d/') !== 0) {
console.warn(
'Rejected forum target with unexpected path:',
targetUrl.pathname
);
return false;
}
forumLink = document.getElementById('forum-link');
targetUrlLink = document.getElementById('target-url');
postPanel = document.getElementById('post-panel');
fallbackPanel = document.getElementById('fallback-panel');
if (
!forumLink ||
!targetUrlLink ||
!postPanel ||
!fallbackPanel
) {
console.warn(
'Required landing-page elements were not found.'
);
return false;
}
forumLink.href = targetUrl.href;
targetUrlLink.href = targetUrl.href;
targetUrlLink.textContent = targetUrl.href;
postPanel.hidden = false;
fallbackPanel.hidden = true;
return true;
} catch (error) {
console.warn(
'The forum target URL is invalid.',
error
);
return false;
}
}
async function initialize() {
var parameters;
var queryTarget;
var context;
var subPageId;
parameters = new URLSearchParams(window.location.search);
queryTarget = parameters.get('target');
if (showTarget(queryTarget)) {
return;
}
if (
!window.microsoftTeams ||
!window.microsoftTeams.app
) {
console.info(
'TeamsJS is unavailable; showing the notifications fallback.'
);
return;
}
try {
await window.microsoftTeams.app.initialize();
context = await window.microsoftTeams.app.getContext();
subPageId =
context &&
context.page &&
context.page.subPageId
? context.page.subPageId
: null;
console.info(
'Teams page.subPageId:',
subPageId || '[none]'
);
showTarget(subPageId);
} catch (error) {
console.warn(
'Could not read the Microsoft Teams context.',
error
);
}
}
initialize();
})();