Perf: remove inline script (#32)

This commit is contained in:
Cell
2022-02-13 15:10:48 +08:00
parent 94a37b191b
commit e7b65c47fa
4 changed files with 74 additions and 78 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+10 -76
View File
@@ -170,18 +170,27 @@
{{- if eq .Site.Params.pangu.enable true -}}
{{- $source := $cdn.metingJS | default "lib/pangu/pangu.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "enablePangu" true | merge $config -}}
{{- end -}}
{{- /* Watermark */ -}}
{{- if eq .Site.Params.watermark.enable true -}}
{{- $source := "lib/watermark/watermark.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "watermark" .Site.Params.watermark | merge $config -}}
{{- end -}}
{{- /* 不蒜子 */ -}}
{{- if eq .Site.Params.ibruce.enable true -}}
{{- $source := "//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Async" true "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $ibruceConfig := dict "enable" true "siteTime" .Site.Params.ibruce.siteTime -}}
{{- $config = dict "ibruce" $ibruceConfig | merge $config -}}
{{- end -}}
{{- /* PWA */ -}}
{{- if eq hugo.Environment "production" -}}
{{- $config = dict "enablePWA" .Site.Params.enablePWA | merge $config -}}
{{- end -}}
{{- range $params.library.css -}}
@@ -228,78 +237,3 @@
{{- end -}}
{{- partial "plugin/analytics.html" . -}}
{{- /* Initialize libraries scripts */ -}}
<script type="text/javascript">
{{- /* Calculate run time */ -}}
{{- if eq .Site.Params.ibruce.enable true | and .Site.Params.ibruce.siteTime -}}
function createTime() {
let now = new Date();
let run = new Date({{ $.Site.Params.ibruce.siteTime }});
//總的秒數
let runTime = (now - run) / 1000,
days = Math.floor(runTime / 60 / 60 / 24),
hours = Math.floor(runTime / 60 / 60 - (24 * days)),
minutes = Math.floor(runTime / 60 - (24 * 60 * days) - (60 * hours)),
seconds = Math.floor((now - run) / 1000 - (24 * 60 * 60 * days) - (60 * 60 * hours) - (60 * minutes));
document.querySelector(".run-times").innerHTML = `${days},${String(hours).padStart(2,0)}:${String(minutes).padStart(2,0)}:${String(seconds).padStart(2,0)}`;
}
{{- end -}}
/**
* Immediate.
*/
(() => {
{{- /* PWA Initialization */ -}}
{{- if eq hugo.Environment "production" | and .Site.Params.enablePWA -}}
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.min.js', { scope: '/' })
.then(function (registration) {
// console.log('Service Worker Registered');
})
.catch(function (error) {
console.error('error: ', error);
});
navigator.serviceWorker.ready.then(function (registration) {
// console.log('Service Worker Ready');
});
}
{{- end -}}
{{- /* Watermark Initialization */ -}}
{{- if eq .Site.Params.watermark.enable true -}}
{{- with .Site.Params.watermark -}}
new Watermark({
content: {{ .Content }} || '<i class="fa-stack fa-xs logo-icon"><i class="fas fa-bug fa-stack-1x"></i><i class="fas fa-ban fa-stack-2x"></i></i> FixIt Theme',
appendTo: {{ .AppendTo }} || '.wrapper>.main',
opacity: {{ .Opacity }},
width: {{ .Width }},
height: {{ .Height }},
rowSpacing: {{ .RowSpacing }},
colSpacing: {{ .ColSpacing }},
rotate: {{ .Rotate }},
fontSize: {{ .FontSize }},
fontFamily: {{ .fontFamily }}
});
{{- end -}}
{{- end -}}
{{- /* Site time Initialization */ -}}
{{- if eq .Site.Params.ibruce.enable true | and .Site.Params.ibruce.siteTime -}}
var siteTime = setInterval("createTime()", 500);
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
return clearInterval(siteTime);
}
var siteTime = setInterval("createTime()", 500);
});
{{- end -}}
document.addEventListener('DOMContentLoaded', () => {
// Listen to any DOM change and automatically perform spacing via MutationObserver()
{{ if eq .Site.Params.pangu.enable true }}pangu.autoSpacingPage();{{ end }}
});
})();
</script>
+62
View File
@@ -652,6 +652,64 @@ class Theme {
if (this.config.cookieconsent) cookieconsent.initialise(this.config.cookieconsent);
}
getSiteTime() {
let now = new Date();
let run = new Date(this.config.ibruce.siteTime);
let runTime = (now - run) / 1000,
days = Math.floor(runTime / 60 / 60 / 24),
hours = Math.floor(runTime / 60 / 60 - (24 * days)),
minutes = Math.floor(runTime / 60 - (24 * 60 * days) - (60 * hours)),
seconds = Math.floor((now - run) / 1000 - (24 * 60 * 60 * days) - (60 * 60 * hours) - (60 * minutes));
document.querySelector(".run-times").innerHTML = `${days},${String(hours).padStart(2,0)}:${String(minutes).padStart(2,0)}:${String(seconds).padStart(2,0)}`;
}
initSiteTime() {
if(this.config.ibruce.enable && this.config.ibruce.siteTime){
this.siteTime = setInterval(this.getSiteTime, 500);
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
return clearInterval(this.siteTime);
}
this.siteTime = setInterval(this.getSiteTime, 500);
});
}
}
initServiceWorker() {
if (this.config.enablePWA && 'serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.min.js', { scope: '/' })
.then(function (registration) {
// console.log('Service Worker Registered');
})
.catch(function (error) {
console.error('error: ', error);
});
navigator.serviceWorker.ready.then(function (registration) {
// console.log('Service Worker Ready');
});
}
}
initWatermark() {
this.config.watermark.enable && new Watermark({
content: this.config.watermark.content || '<i class="fa-stack fa-xs logo-icon"><i class="fas fa-bug fa-stack-1x"></i><i class="fas fa-ban fa-stack-2x"></i></i> FixIt Theme',
appendTo:this.config.watermark.appendTo || '.wrapper>.main',
opacity: this.config.watermark.opacity,
width: this.config.watermark.width,
height: this.config.watermark.height,
rowSpacing: this.config.watermark.rowSpacing,
colSpacing: this.config.watermark.colSpacing,
rotate: this.config.watermark.rotate,
fontSize: this.config.watermark.fontSize,
fontFamily: this.config.watermark.fontFamily
});
}
initPangu() {
this.config.enablePangu && pangu.autoSpacingPage();
}
onScroll() {
const $headers = [];
if (document.body.getAttribute('header-desktop') === 'auto') $headers.push(document.getElementById('header-desktop'));
@@ -736,6 +794,10 @@ class Theme {
this.initTypeit();
this.initMapbox();
this.initCookieconsent();
this.initSiteTime();
this.initServiceWorker();
this.initWatermark();
this.initPangu();
} catch (err) {
console.error(err);
}