/* webmention.js
Simple thing for embedding webmentions from webmention.io into a page, client-side.
(c)2018-2022 fluffy (http://beesbuzz.biz)
2025 mmai (https://misc.rhumbs.fr)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Basic usage:
Allowed parameters:
page-url:
The base URL to use for this page. Defaults to window.location
add-urls:
Additional URLs to check, separated by |s
id:
The HTML ID for the object to fill in with the webmention data.
Defaults to "webmentions"
wordcount:
The maximum number of words to render in reply mentions.
max-webmentions:
The maximum number of mentions to retrieve. Defaults to 30.
prevent-spoofing:
By default, Webmentions render using the mf2 'url' element, which plays
nicely with webmention bridges (such as brid.gy and telegraph)
but allows certain spoofing attacks. If you would like to prevent
spoofing, set this to a non-empty string (e.g. "true").
sort-by:
What to order the responses by; defaults to 'published'. See
https://github.com/aaronpk/webmention.io#api
sort-dir:
The order to sort the responses by; defaults to 'up' (i.e. oldest
first). See https://github.com/aaronpk/webmention.io#api
comments-are-reactions:
If set to a non-empty string (e.g. "true"), will display comment-type responses
(replies/mentions/etc.) as being part of the reactions
(favorites/bookmarks/etc.) instead of in a separate comment list.
A more detailed example:
*/
// Begin LibreJS code licensing
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt
(function () {
"use strict";
// Shim i18next
window.i18next = window.i18next || {
t: function t(/** @type {string} */key) { return key; }
}
const t = window.i18next.t.bind(window.i18next);
/**
* Read the configuration value.
*
* @param {string} key The configuration key.
* @param {string} dfl The default value.
* @returns {string}
*/
function getCfg(key, dfl) {
return document.currentScript.getAttribute("data-" + key) || dfl;
}
const refurl = getCfg("page-url", window.location.href.replace(/#.*$/, ""));
const addurls = getCfg("add-urls", undefined);
const containerID = getCfg("id", "webmentions");
/** @type {Number} */
const textMaxWords = getCfg("wordcount");
const maxWebmentions = getCfg("max-webmentions", 30);
const mentionSource = getCfg("prevent-spoofing") ? "wm-source" : "url";
const sortBy = getCfg("sort-by", "published");
const sortDir = getCfg("sort-dir", "up");
/**
* Strip the protocol off a URL.
*
* @param {string} url The URL to strip protocol off.
* @returns {string}
*/
function stripurl(url) {
return url.substr(url.indexOf('//'));
}
/**
* Deduplicate multiple mentions from the same source URL.
*
* @param {Array} mentions Mentions of the source URL.
* @return {Array}
*/
function dedupe(mentions) {
/** @type {Array} */
const filtered = [];
/** @type {Record} */
const seen = {};
mentions.forEach(function (r) {
// Strip off the protocol (i.e. treat http and https the same)
const source = stripurl(r.url);
if (!seen[source]) {
filtered.push(r);
seen[source] = true;
}
});
return filtered;
}
/**
* Format comments as HTML.
*
* @param {Array} comments The comments to format.
* @returns string
*/
function formatComments(type, comments) {
let html = `
` + getIcon('comment') + ` ` + comments.length + ` ` + type + `s
`; comments.forEach(function (comment) { let content = ''; if (comment.hasOwnProperty('content')) { if (comment.content.hasOwnProperty('html')) { content = comment.content.html; } else if (comment.content.hasOwnProperty('text')) { content = comment.content.text; } } html += `-
`+ comment.author.name + `
`;
if (comment.published) {
const published = new Date(comment.published);
html += `
`;
}
html += `
`;
});
html += `
`+ content + ` source