fix preload+utterances

This commit is contained in:
xaoxuu 2021-02-21 23:20:13 +08:00
parent b92efdfc58
commit 41f0bcb8ab
3 changed files with 32 additions and 37 deletions

View File

@ -80,8 +80,6 @@ comments:
theme: theme:
light: github-light light: github-light
dark: github-dark dark: github-dark
label: ✨💬✨
crossorigin: anonymous
######## Footer ######## ######## Footer ########

View File

@ -1 +1,23 @@
<div id="utterances"></div> <%
function layoutDiv() {
var el = '';
el += '<div id="utterances"';
let utterances = Object.assign({}, theme.comments.utterances, page.utterances);
if (page.comment_id) {
utterances['issue-term'] = page.comment_id;
}
if (page.dark && utterances.theme.dark) {
utterances.theme = utterances.theme.dark;
} else if (utterances.theme.light) {
utterances.theme = utterances.theme.light;
}
for (let key of Object.keys(utterances)) {
if (utterances[key]) {
el += ' ' + key + '="' + utterances[key] + '"';
}
}
el += '></div>';
return el;
}
%>
<%- layoutDiv() %>

View File

@ -1,48 +1,23 @@
<%
if (page.utterances == undefined) {
page.utterances = theme.comments.utterances;
} else {
for (let key of Object.keys(theme.comments.utterances)) {
if (page.utterances[key] == undefined) {
page.utterances[key] = theme.comments.utterances[key];
}
}
}
if (page.comment_id) {
page.utterances['issue-term'] = page.comment_id;
}
if (theme.comments.utterances.theme) {
if (page.dark) {
page.utterances.theme = theme.comments.utterances.theme.dark;
} else {
page.utterances.theme = theme.comments.utterances.theme.light;
}
}
%>
<script> <script>
stellar.comments = {
service: 'utterances',
utterances: Object.assign(<%- JSON.stringify(page.utterances) %>),
}
function utterances(){ function utterances(){
if(!document.getElementById("utterances"))return; if(!document.getElementById("utterances"))return;
setTimeout(function() { setTimeout(function() {
var checkUtterances = setInterval(function () { var checkUtterances = setInterval(function () {
var HEAD = document.getElementById("utterances"); var el = document.getElementById("utterances");
if (!HEAD) return if (!el) return
clearInterval(checkUtterances) clearInterval(checkUtterances)
try { try {
HEAD.innerHTML=""; el.innerHTML="";
} catch (error) {} } catch (error) {}
var script = document.createElement('script'); var script = document.createElement('script');
const keys = ['repo', 'issue-term', 'theme', 'label', 'crossorigin'];
script.src = 'https://utteranc.es/client.js'; script.src = 'https://utteranc.es/client.js';
for (let key of Object.keys(stellar.comments.utterances)) { const keys = ['repo', 'issue-term', 'theme', 'label', 'crossorigin'];
if (keys.includes(key) && stellar.comments.utterances[key]) { keys.forEach((key, i) => {
script.setAttribute(key, stellar.comments.utterances[key]); if (el.attributes[key] && el.attributes[key].value) {
script.setAttribute(key, el.attributes[key].value);
} }
} });
HEAD.appendChild(script); el.appendChild(script);
}, 200) }, 200)
}); });
} }