-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 1.97 KB
/
index.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
"use strict";
function isJSON(json) {
try {
JSON.parse(JSON.stringify(json));
return true;
} catch (error) {
return false;
}
}
hexo.extend.filter.register("after_generate", function () {
const log = this.log;
const config = this.config.discuss;
if (!config.enable) return;
const str = "Not detected Discuss$ configuration";
if (!isJSON(config)) return log.w(str.replace("$", ""));
if (!config.el) return log.w(str.replace("$", ".el"));
if (!config.serverURLs) return log.w(str.replace("$", ".serverURLs"));
const { siblingEl, el, serverURLs, options, location, source, page } = config;
const latest = "https://cdn.jsdelivr.net/npm/discuss@latest/dist/Discuss.js";
const initData = Object.assign(
{ el, serverURLs },
isJSON(options) ? options : {}
);
const createElStr = `
(function(){
var createNextElementSibling = document.querySelector("${siblingEl}")
if (!createNextElementSibling) return
var div = document.createElement('div')
var str = "${el}".slice(1)
"${el}".slice(0,1) === '.' ? div.className = str : div.id = str
createNextElementSibling.parentNode.insertBefore(div,createNextElementSibling.nextElementSibling)
})()`;
const script = `<script>
(function () {${createElStr || ""}
if (!document.querySelector("${el}")) return
var script = document.createElement("script")
var initData = ${JSON.stringify(initData)}
script.src = "${source || latest}"
script.type = "text/javascript"
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null
Discuss.init(initData)
}
};
} else {
script.onload = function () {
Discuss.init(initData)
};
}
document.body.appendChild(script)
})();
</script>`;
hexo.extend.injector.register(
location || "body_end",
script,
page || "default"
);
});