-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.js
133 lines (133 loc) · 5.22 KB
/
comment.js
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
var GISCUS_SESSION_KEY = 'giscus-session';
var script = document.currentScript;
var giscusOrigin = new URL(script.src).origin;
function formatError(message) {
return "[tegursapa] An error occurred. Error message: \"" + message + "\".";
}
function loadScript(url, callback) {
var target = document.createElement('script');
target.setAttribute('src', url);
target.onload = callback;
script.insertAdjacentElement('beforeend', target);
}
loadScript('https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.min.js', function () {
return iFrameResize({ checkOrigin: [giscusOrigin], resizeFrom: 'child' }, '.giscus-frame');
});
// Set up iframe src URL and params
var url = new URL(location.href);
var session = url.searchParams.get('giscus');
var savedSession = localStorage.getItem(GISCUS_SESSION_KEY);
if (session) {
localStorage.setItem(GISCUS_SESSION_KEY, JSON.stringify(session));
url.searchParams["delete"]('giscus');
history.replaceState(undefined, document.title, url.toString());
}
else {
try {
session = JSON.parse(savedSession) || '';
}
catch (e) {
session = '';
localStorage.removeItem(GISCUS_SESSION_KEY);
console.warn(formatError(e === null || e === void 0 ? void 0 : e.message) + " Session has been cleared.");
}
}
var attributes = script.dataset;
var params = {};
var ogDescriptionMeta = document.querySelector("meta[property='og:description'],meta[name='description']");
params.origin = location.href;
params.session = session;
params.theme = attributes.theme;
params.reactionsEnabled = attributes.reactionsEnabled || '1';
params.emitMetadata = attributes.emitMetadata || '0';
params.repo = attributes.repo;
params.repoId = attributes.repoId;
params.category = attributes.category || '';
params.categoryId = attributes.categoryId;
params.description = ogDescriptionMeta ? ogDescriptionMeta.content : '';
switch (attributes.mapping) {
case 'url':
params.term = location.href;
break;
case 'title':
params.term = document.title;
break;
case 'og:title':
{
var ogtitleMeta = document.querySelector("meta[property='og:title'],meta[name='og:title']");
params.term = ogtitleMeta ? ogtitleMeta.content : '';
}
break;
case 'specific':
params.term = attributes.term;
break;
case 'number':
params.number = attributes.term;
break;
case 'pathname':
default:
params.term =
location.pathname.length < 2 ? 'index' : location.pathname.substr(1).replace(/\.\w+$/, '');
break;
}
var src = giscusOrigin + "/widget?" + new URLSearchParams(params);
// Set up iframe element
var iframeElement = document.createElement('iframe');
var iframeAttributes = {
"class": 'giscus-frame',
title: 'Comments',
scrolling: 'no',
src: src
};
Object.entries(iframeAttributes).forEach(function (_a) {
var key = _a[0], value = _a[1];
return iframeElement.setAttribute(key, value);
});
// Create default style and prepend as <head>'s first child to make override possible.
var style = document.getElementById('giscus-css') || document.createElement('style');
style.id = 'giscus-css';
style.textContent = "\n .giscus, .giscus-frame {\n width: 100%;\n }\n\n .giscus-frame {\n border: none;\n color-scheme: auto;\n }\n";
document.head.prepend(style);
// Insert iframe element
var existingContainer = document.querySelector('.giscus');
if (!existingContainer) {
var iframeContainer = document.createElement('div');
iframeContainer.setAttribute('class', 'giscus');
iframeContainer.appendChild(iframeElement);
script.insertAdjacentElement('afterend', iframeContainer);
}
else {
while (existingContainer.firstChild)
existingContainer.firstChild.remove();
existingContainer.appendChild(iframeElement);
}
var suggestion = "Please consider reporting this error at https://github.com/laymonage/giscus/issues/new.";
// Listen to error messages
window.addEventListener('message', function (event) {
var _a;
if (event.origin !== giscusOrigin)
return;
var data = event.data;
if (!(typeof data === 'object' && ((_a = data === null || data === void 0 ? void 0 : data.giscus) === null || _a === void 0 ? void 0 : _a.error)))
return;
var message = data.giscus.error;
if (message.includes('Bad credentials') || message.includes('Invalid state value')) {
// Might be because token is expired or other causes
if (localStorage.getItem(GISCUS_SESSION_KEY) !== null) {
localStorage.removeItem(GISCUS_SESSION_KEY);
console.warn(formatError(message) + " Session has been cleared.");
delete params.session;
var src_1 = giscusOrigin + "/widget?" + new URLSearchParams(params);
iframeElement.src = src_1; // Force reload
}
else if (!savedSession) {
console.error(formatError(message) + " No session is stored initially. " + suggestion);
}
}
else if (message.includes('Discussion not found')) {
console.warn("[tegursapa] " + message + ". A new discussion will be created if a comment/reaction is submitted.");
}
else {
console.error(formatError(message) + " " + suggestion);
}
});