fix: 修復 pjax 下, waline js 會重複加載的 bug

This commit is contained in:
Jerry
2023-01-15 01:39:16 +08:00
parent 9a3b41912a
commit 929a5608d3
12 changed files with 59 additions and 45 deletions

View File

@@ -634,7 +634,8 @@ subtitle:
# source 調用第三方服務 # source 調用第三方服務
# source: false 關閉調用 # source: false 關閉調用
# source: 1 調用一言網的一句話(簡體) https://hitokoto.cn/ # source: 1 調用一言網的一句話(簡體) https://hitokoto.cn/
# source: 2 調用今日詩詞(簡體) https://www.jinrishici.com/ # source: 2 調用一句網(簡體) https://yijuzhan.com/
# source: 3 調用今日詩詞(簡體) https://www.jinrishici.com/
# subtitle 會先顯示 source , 再顯示 sub 的內容 # subtitle 會先顯示 source , 再顯示 sub 的內容
source: false source: false
# 如果關閉打字效果subtitle 只會顯示 sub 的第一行文字 # 如果關閉打字效果subtitle 只會顯示 sub 的第一行文字

View File

@@ -10,7 +10,7 @@ script.
const waline = Waline.init(initData) const waline = Waline.init(initData)
} }
if (typeof Waline === 'function') initWaline() if (typeof Waline === 'object') initWaline()
else getScript('!{url_for(theme.asset.waline_js)}').then(initWaline) else getScript('!{url_for(theme.asset.waline_js)}').then(initWaline)
} }

View File

@@ -1,13 +1,6 @@
- const { server, site, option } = theme.artalk - const { server, site, option } = theme.artalk
script. script.
function addArtalkSource () {
const ele = document.createElement('link')
ele.rel = 'stylesheet'
ele.href= '!{theme.asset.artalk_css}'
document.getElementsByTagName('head')[0].appendChild(ele)
}
function loadArtalk () { function loadArtalk () {
function initArtalk () { function initArtalk () {
window.artalkItem = new Artalk(Object.assign({ window.artalkItem = new Artalk(Object.assign({
@@ -22,7 +15,7 @@ script.
if (typeof window.artalkItem === 'object') setTimeout(()=>{initArtalk()},200) if (typeof window.artalkItem === 'object') setTimeout(()=>{initArtalk()},200)
else { else {
addArtalkSource() getCSS('!{theme.asset.artalk_css}')
typeof Artalk !== 'function' ? getScript('!{theme.asset.artalk_js}').then(initArtalk) typeof Artalk !== 'function' ? getScript('!{theme.asset.artalk_js}').then(initArtalk)
: setTimeout(()=>{initArtalk()},200) : setTimeout(()=>{initArtalk()},200)
} }

View File

@@ -2,13 +2,6 @@
script. script.
function loadDisqusjs () { function loadDisqusjs () {
function addDisqusjsCSS () {
const ele = document.createElement('link')
ele.rel = 'stylesheet'
ele.href= '!{url_for(theme.asset.disqusjs_css)}'
document.getElementsByTagName('head')[0].appendChild(ele)
}
function initDisqusjs () { function initDisqusjs () {
window.disqusjs = null window.disqusjs = null
disqusjs = new DisqusJS(Object.assign({ disqusjs = new DisqusJS(Object.assign({
@@ -34,7 +27,7 @@ script.
if (window.disqusJsLoad) initDisqusjs() if (window.disqusJsLoad) initDisqusjs()
else { else {
addDisqusjsCSS() getCSS('!{url_for(theme.asset.disqusjs_css)}')
getScript('!{url_for(theme.asset.disqusjs)}').then(initDisqusjs) getScript('!{url_for(theme.asset.disqusjs)}').then(initDisqusjs)
window.disqusJsLoad = true window.disqusJsLoad = true
} }

View File

@@ -1,11 +1,4 @@
script. script.
function addGitalkSource () {
const ele = document.createElement('link')
ele.rel = 'stylesheet'
ele.href= '!{url_for(theme.asset.gitalk_css)}'
document.getElementsByTagName('head')[0].appendChild(ele)
}
function loadGitalk () { function loadGitalk () {
function initGitalk () { function initGitalk () {
var gitalk = new Gitalk(Object.assign({ var gitalk = new Gitalk(Object.assign({
@@ -23,7 +16,7 @@ script.
if (typeof Gitalk === 'function') initGitalk() if (typeof Gitalk === 'function') initGitalk()
else { else {
addGitalkSource() getCSS('!{url_for(theme.asset.gitalk_css)}')
getScript('!{url_for(theme.asset.gitalk)}').then(initGitalk) getScript('!{url_for(theme.asset.gitalk)}').then(initGitalk)
} }
} }

View File

@@ -3,13 +3,6 @@
script. script.
function loadWaline () { function loadWaline () {
function insertCSS () {
const link = document.createElement("link")
link.rel = "stylesheet"
link.href = "!{url_for(theme.asset.waline_css)}"
document.head.appendChild(link)
}
function initWaline () { function initWaline () {
const waline = Waline.init(Object.assign({ const waline = Waline.init(Object.assign({
el: '#waline-wrap', el: '#waline-wrap',
@@ -21,10 +14,11 @@ script.
}, !{JSON.stringify(option)})) }, !{JSON.stringify(option)}))
} }
if (typeof Waline === 'function') initWaline() if (typeof Waline === 'object') initWaline()
else { else {
insertCSS() getCSS('!{url_for(theme.asset.waline_css)}').then(() => {
getScript('!{url_for(theme.asset.waline_js)}').then(initWaline) getScript('!{url_for(theme.asset.waline_js)}').then(initWaline)
})
} }
} }

View File

@@ -64,7 +64,7 @@ script.
}) })
} }
if (typeof Waline === 'function') loadWaline() if (typeof Waline === 'object') loadWaline()
else getScript('!{url_for(theme.asset.waline_js)}').then(loadWaline) else getScript('!{url_for(theme.asset.waline_js)}').then(loadWaline)
} }

View File

@@ -36,6 +36,38 @@ case source
} }
when 2 when 2
script.
function subtitleType () {
getScript('https://yijuzhan.com/api/word.php?m=js').then(() => {
const con = str[0]
if (!{effect}) {
const from = '出自 ' + str[1]
const sub = !{JSON.stringify(subContent)}
sub.unshift(con, from)
window.typed = new Typed('#subtitle', {
strings: sub,
startDelay: !{startDelay},
typeSpeed: !{typeSpeed},
loop: !{loop},
backSpeed: !{backSpeed},
})
} else {
document.getElementById('subtitle').innerHTML = con
}
})
}
if (!{effect}) {
if (typeof Typed === 'function') {
subtitleType()
} else {
getScript('!{url_for(theme.asset.typed)}').then(subtitleType)
}
} else {
subtitleType()
}
when 3
script. script.
function subtitleType () { function subtitleType () {
getScript('https://sdk.jinrishici.com/v2/browser/jinrishici.js').then(() => { getScript('https://sdk.jinrishici.com/v2/browser/jinrishici.js').then(() => {

View File

@@ -1,6 +1,6 @@
{ {
"name": "hexo-theme-butterfly", "name": "hexo-theme-butterfly",
"version": "4.6.0-b4", "version": "4.6.0",
"description": "A Simple and Card UI Design theme for Hexo", "description": "A Simple and Card UI Design theme for Hexo",
"main": "package.json", "main": "package.json",
"scripts": { "scripts": {

View File

@@ -42,12 +42,12 @@ waline_js:
name: '@waline/client' name: '@waline/client'
file: dist/waline.js file: dist/waline.js
other_name: waline other_name: waline
version: 2.14.6 version: 2.14.7
waline_css: waline_css:
name: '@waline/client' name: '@waline/client'
file: dist/waline.css file: dist/waline.css
other_name: waline other_name: waline
version: 2.14.6 version: 2.14.7
sharejs: sharejs:
name: butterfly-extsrc name: butterfly-extsrc
file: sharejs/dist/js/social-share.min.js file: sharejs/dist/js/social-share.min.js

View File

@@ -60,6 +60,17 @@ hexo.extend.helper.register('inject_head_js', function () {
}) })
` `
const getCSS = `
win.getCSS = url => new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
link.onload = () => resolve()
link.onerror = () => reject()
document.head.appendChild(link)
})
`
let darkmodeJs = '' let darkmodeJs = ''
if (darkmode.enable) { if (darkmode.enable) {
darkmodeJs = ` darkmodeJs = `
@@ -144,5 +155,5 @@ hexo.extend.helper.register('inject_head_js', function () {
detectApple() detectApple()
` `
return `<script>(win=>{${localStore + getScript + darkmodeJs + asideStatus + detectApple}})(window)</script>` return `<script>(win=>{${localStore + getScript + getCSS + darkmodeJs + asideStatus + detectApple}})(window)</script>`
}) })

View File

@@ -275,10 +275,7 @@ document.addEventListener('DOMContentLoaded', function () {
return return
} }
const newEle = document.createElement('link') getCSS(`${GLOBAL_CONFIG.source.justifiedGallery.css}`)
newEle.rel = 'stylesheet'
newEle.href = GLOBAL_CONFIG.source.justifiedGallery.css
document.body.appendChild(newEle)
getScript(`${GLOBAL_CONFIG.source.justifiedGallery.js}`).then(() => { btf.initJustifiedGallery(ele) }) getScript(`${GLOBAL_CONFIG.source.justifiedGallery.js}`).then(() => { btf.initJustifiedGallery(ele) })
} }