summaryrefslogtreecommitdiffstats
path: root/static/js/webmention.min.js
blob: 61f107d16bb4ca52782510c80544036e162d5acb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt
!(function () {
    "use strict";
    window.i18next = window.i18next || {
        t: function t(e) {
            return e;
        },
    };
    let t = window.i18next.t.bind(window.i18next);
    function e(t, e) {
        return document.currentScript.getAttribute("data-" + t) || e;
    }
    let o = e("page-url", window.location.href.replace(/#.*$/, "")),
        n = e("add-urls", void 0),
        r = e("id", "webmentions"),
        s = e("wordcount"),
        l = e("max-webmentions", 30),
        i = e("prevent-spoofing") ? "wm-source" : "url",
        a = e("sort-by", "published"),
        u = e("sort-dir", "up"),
        p = e("comments-are-reactions", !1),
        c = {
            "in-reply-to": t("replied"),
            "like-of": t("liked"),
            "repost-of": t("reposted"),
            "bookmark-of": t("bookmarked"),
            "mention-of": t("mentioned"),
            rsvp: t("RSVPed"),
            "follow-of": t("followed"),
        },
        f = {
            "in-reply-to": "\uD83D\uDCAC",
            "like-of": "❤️",
            "repost-of": "\uD83D\uDD04",
            "bookmark-of": "⭐️",
            "mention-of": "\uD83D\uDCAC",
            rsvp: "\uD83D\uDCC5",
            "follow-of": "\uD83D\uDC1C",
        },
        m = {
            yes: "✅",
            no: "❌",
            interested: "\uD83D\uDCA1",
            maybe: "\uD83D\uDCAD",
        };
    function d(t) {
        return t.replace(
            /[&<>"]/g,
            (t) =>
                ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[
                    t
                ] || t,
        );
    }
    function h(e, o) {
        let n = d(e.author?.name || e.url.split("/")[2]),
            r = c[e["wm-property"]] || t("reacted");
        !o && e.content && e.content.text && (r += ": " + x(e));
        let s = "";
        s =
            e.author && e.author.photo
                ? `
        <img
          src="${d(e.author.photo)}"
          loading="lazy"
          decoding="async"
          alt="${n}"
        >
      `
                : `
        <img
          class="missing"
          src="data:image/webp;base64,UklGRkoCAABXRUJQVlA4TD4CAAAvP8APAIV0WduUOLr/m/iqY6SokDJSMD5xYX23SQizRsVdZmIj/f6goYUbiOj/BED7MOPReuBNT3vBesSzIex+SeqMFFkjebFmzH3S7POxDSJ1yaCbCmMnS2R46cRMPyQLw4GBK4esdK60pYwsZakecUCl5zsHv/5cPH08nx9/7i6rEEVCg2hR8VSd30PxMZpVoJZQO6Dixgg6X5oKFCmlVHIDmmMFShWumAXgCuyqVN8hHff/k+9fj8+ei7BVjpxBmZCUJv+6DhWGZwWvs+UoLHFCKsPYpfJtIcEXBTopEEsKwedZUv4ku1FZErKULLyQwFGgnmTs2vBD5qu44xwnG9uyjgrFOd+KRVlXyQfwQlauydaU6AVI7OjKXLUEqNtxJBmQegNDZgV7lxxqYMOMrDyC1NdAGbdiH9Ij0skjG+oTyfO0lmjdgvoH8iIgreuBMRYLSH+R3sAztXgL+XfS7E2bmfo6gnS0TrpnzHT7kL+skj7PgHuBwv/zpN8LDLQg7zfJZLBubMKnyeh6ZGyfDEfc2LYpnlUtG7JqsSHq1WoASbUS4KVaLwB8be5mfsGMDwBcm5VxbuxWxx3nkFanB6lYqsqSkOGkKicoDvXsneR7BkKU7DtaEuT7+pxBGVwx+9gVyqf2pVA9sC2CsmjZ1RJqEJHS4Tj/pCcS0JoyBYOsB91Xjh3OFfQPQhvCAYyeLJlaOoFp0XNNuD0BC8exr8uPx7D1JWkwFdZIXmD3MOPReuDNzHjBesSzIbQD"
          alt="${n}$"
        >
      `;
        let l = "";
        return (
            e.rsvp && m[e.rsvp] && (l = `<sub>${m[e.rsvp]}</sub>`),
            `
      <a
        class="reaction"
        rel="nofollow ugc"
        title="${n} ${r}"
        href="${e[i]}"
      >
        ${s}
        ${f[e["wm-property"]] || "\uD83D\uDCA5"}
        ${l}
      </a>
    `
        );
    }
    function g(t) {
        return t.substr(t.indexOf("//"));
    }
    function w(t) {
        let e = [],
            o = {};
        return (
            t.forEach(function (t) {
                let n = g(t.url);
                o[n] || (e.push(t), (o[n] = !0));
            }),
            e
        );
    }
    function x(t) {
        let e = d(t.content.text);
        if (s) {
            let o = e.replace(/\s+/g, " ").split(" ", s + 1);
            o.length > s &&
                ((o[s - 1] += "&hellip;"), (e = (o = o.slice(0, s)).join(" ")));
        }
        return e;
    }
    window.addEventListener("load", async function () {
        let e = document.getElementById(r);
        if (!e) return;
        let s = [g(o)];
        n &&
            n.split("|").forEach(function (t) {
                s.push(g(t));
            });
        let c = `https://webmention.io/api/mentions.jf2?per-page=${l}&sort-by=${a}&sort-dir=${u}`;
        s.forEach(function (t) {
            c += `&target[]=${encodeURIComponent("http:" + t)}&target[]=${encodeURIComponent("https:" + t)}`;
        });
        let f = {};
        try {
            let m = await window.fetch(c);
            m.status >= 200 && m.status < 300
                ? (f = await m.json())
                : (console.error("Could not parse response"),
                  Error(m.statusText));
        } catch (y) {
            console.error("Request failed", y);
        }
        let b = [],
            k = [];
        p && (b = k);
        let B = {
            "in-reply-to": b,
            "like-of": k,
            "repost-of": k,
            "bookmark-of": k,
            "follow-of": k,
            "mention-of": b,
            rsvp: b,
        };
        f.children.forEach(function (t) {
            let e = B[t["wm-property"]];
            e && e.push(t);
        });
        let S = "";
        b.length > 0 &&
            b !== k &&
            (S = (function e(o) {
                let n = `<h2>${t("Responses")}</h2>`,
                    r = o
                        .map((e) => {
                            let o = h(e, !0),
                                n = d(e.url.split("/")[2]);
                            e.author && e.author.name && (n = d(e.author.name));
                            let r = `<a class="source" rel="nofollow ugc" href="${e[i]}">${n}</a>`,
                                s = "name",
                                l = `(${t("mention")})`;
                            e.name
                                ? ((s = "name"), (l = d(e.name)))
                                : e.content &&
                                  e.content.text &&
                                  ((s = "text"), (l = x(e)));
                            let a = `<span class="${s}">${l}</span>`;
                            return `<li>${o} ${r} ${a}</li>`;
                        })
                        .join("");
                return `
      ${n}
      <ul class="comments">${r}</ul>
    `;
            })(w(b)));
        let D = "";
        k.length > 0 &&
            (D = (function e(o) {
                let n = `<h2>${t("Reactions")}</h2>`,
                    r = o.map((t) => h(t)).join("");
                return `
      ${n}
      <ul class="reacts">${r}</ul>
    `;
            })(w(k)));
        let $ = `${S}${D}`;
        ($.trim() || ($ = '<p class="wm-empty">暂无 Webmention 回应。</p>'),
            (e.innerHTML = $));
    });
})(); // @license-end