Candinya / Kratos-Rebirth

一个可爱而现代的 Hexo 主题
GNU General Public License v3.0
630 stars 80 forks source link

Bug 回报:开启`Rocket-Loader`后,暗色模式切换按钮失效 #135

Closed BH6AOV closed 2 years ago

BH6AOV commented 2 years ago
  1. 报告前须知
  1. 对Bug的描述: 页面手动暗色模式切换按钮失效

  2. 复现Bug的方法: 可以使用如下的方案进行复现: 1 使用npm安装主题v2.0.2 2 通过npm update升级主题至v2.0.3 3 hexo g生成网站 4 看,出错了吧!

  3. 期望的行为: 按钮应可以正常手动切换暗色主题

  4. 相关的截图: 可直接访问 https://blog.ypa.moe/ 进行按钮测试

  5. 运行环境(如果该Bug的产生和环境有关,例如浏览器不兼容导致):

    • 操作系统: Windows 10 专业版 / Android Q
    • 浏览器: Chrome
    • 版本: 均为最新版本
  6. 服务器环境 以下参数来自hexo v

    • hexo: 6.2.0
    • hexo-cli: 4.3.0
    • os: linux 5.4.0-107-generic Ubuntu 20.04.4 LTS (Focal Fossa)
    • node: 17.9.0
    • v8: 9.6.180.15-node.16
    • uv: 1.43.0
    • zlib: 1.2.11
    • brotli: 1.0.9
    • ares: 1.18.1
    • modules: 102
    • nghttp2: 1.47.0
    • napi: 8
    • llhttp: 6.0.4
    • openssl: 3.0.2+quic
    • cldr: 40.0
    • icu: 70.1
    • tz: 2021a3
    • unicode: 14.0
    • ngtcp2: 0.1.0-DEV
    • nghttp3: 0.1.0-DEV Nginx版本: 1.18.0
  7. 您的配置文件: /*****/_config.kratos-rebirth.yml Line 10 ...... enable_dark: true ......

  8. 补充内容: 暂无

Candinya commented 2 years ago

有点奇怪,感觉是切换监听事件没有被挂载到按钮上导致的,看控制台这里有一个 clarity.ms 的代码加载报错(但不确定是不是被我的什么拦截器规则误伤了),其他暂时没法发现什么,手动执行样式切换是可行的

image

可以先去掉这行(应该是站点统计代码?)试试,或是在 script 里看看加个 type="text/javascript" 看看能否解决问题?

BH6AOV commented 2 years ago

我回去注释掉试一下,按理说应该没影响x,之前用老版本(指不用npm安装的那个)的时候啥问题没有(?) type="text/javascript"一直是存在的,刚刚试着注释掉之后重新生成也还是完全没有用:

site_analytics:
  <!--Bing/Clarity Analytics--><script type="text/javascript">(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);})(window, document, "clarity", "script", "********");</script><!--End of Bing/Clarity Analytics-->
BH6AOV commented 2 years ago

刚刚尝试了一下,将控制台里所有的报错项目都删掉了(除了cloudflare那个烦人的static),一样还是不能用的,怪诶

ghost commented 2 years ago

根据加载失败的内容来看,你应该是使用了 cloudflare cdn 同时在 对应域名-Apps 里安装了一些应用

/cdn-cgi/apps/head/o8VSJLPlJ630VzpFGAYxShTLMQY.js

```js ;window.CloudflareApps=window.CloudflareApps||{};CloudflareApps.siteId="3bf9f6cadf55be30dc8bd75986975e99";CloudflareApps.installs=CloudflareApps.installs||{};;(function(){'use strict' CloudflareApps.internal=CloudflareApps.internal||{} var errors=[] CloudflareApps.internal.placementErrors=errors var errorHashes={} function noteError(options){var hash=options.selector+'::'+options.type+'::'+(options.installId||'') if(errorHashes[hash]){return} errorHashes[hash]=true errors.push(options)} var initializedSelectors={} var currentInit=false CloudflareApps.internal.markSelectors=function markSelectors(){if(!currentInit){check() currentInit=true setTimeout(function(){currentInit=false})}} function check(){var installs=window.CloudflareApps.installs for(var installId in installs){if(!installs.hasOwnProperty(installId)){continue} var selectors=installs[installId].selectors if(!selectors){continue} for(var key in selectors){if(!selectors.hasOwnProperty(key)){continue} var hash=installId+'::'+key if(initializedSelectors[hash]){continue} var els=document.querySelectorAll(selectors[key]) if(els&&els.length>1){noteError({type:'init:too-many',option:key,selector:selectors[key],installId:installId}) initializedSelectors[hash]=true continue}else if(!els||!els.length){continue} initializedSelectors[hash]=true els[0].setAttribute('cfapps-selector',selectors[key])}}} CloudflareApps.querySelector=function querySelector(selector){if(selector==='body'||selector==='head'){return document[selector]} CloudflareApps.internal.markSelectors() var els=document.querySelectorAll('[cfapps-selector="'+selector+'"]') if(!els||!els.length){noteError({type:'select:not-found:by-attribute',selector:selector}) els=document.querySelectorAll(selector) if(!els||!els.length){noteError({type:'select:not-found:by-query',selector:selector}) return null}else if(els.length>1){noteError({type:'select:too-many:by-query',selector:selector})} return els[0]} if(els.length>1){noteError({type:'select:too-many:by-attribute',selector:selector})} return els[0]}}());(function(){'use strict' var prevEls={} CloudflareApps.createElement=function createElement(options,prevEl){options=options||{} CloudflareApps.internal.markSelectors() try{if(prevEl&&prevEl.parentNode){var replacedEl if(prevEl.cfAppsElementId){replacedEl=prevEls[prevEl.cfAppsElementId]} if(replacedEl){prevEl.parentNode.replaceChild(replacedEl,prevEl) delete prevEls[prevEl.cfAppsElementId]}else{prevEl.parentNode.removeChild(prevEl)}} var element=document.createElement('cloudflare-app') var container if(options.pages&&options.pages.URLPatterns&&!CloudflareApps.matchPage(options.pages.URLPatterns)){return element} try{container=CloudflareApps.querySelector(options.selector)}catch(e){} if(!container){return element} if(!container.parentNode&&(options.method==='after'||options.method==='before'||options.method==='replace')){return element} if(container===document.body){if(options.method==='after'){options.method='append'}else if(options.method==='before'){options.method='prepend'}} switch(options.method){case'prepend':if(container.firstChild){container.insertBefore(element,container.firstChild) break} case'append':container.appendChild(element) break case'after':if(container.nextSibling){container.parentNode.insertBefore(element,container.nextSibling)}else{container.parentNode.appendChild(element)} break case'before':container.parentNode.insertBefore(element,container) break case'replace':try{var id=element.cfAppsElementId=Math.random().toString(36) prevEls[id]=container}catch(e){} container.parentNode.replaceChild(element,container)} return element}catch(e){if(typeof console!=='undefined'&&typeof console.error!=='undefined'){console.error('Error creating Cloudflare Apps element',e)}}}}());(function(){'use strict' CloudflareApps.matchPage=function matchPage(patterns){if(!patterns||!patterns.length){return true} var loc=document.location.host+document.location.pathname if(window.CloudflareApps&&CloudflareApps.proxy&&CloudflareApps.proxy.originalURL){var url=CloudflareApps.proxy.originalURL.parsed loc=url.host+url.path} for(var i=0;i

可以尝试去cloudflare那边暂时停用cdn功能检查一下

BH6AOV commented 2 years ago

根据加载失败的内容来看,你应该是使用了 cloudflare cdn 同时在 对应域名-Apps 里安装了一些应用

/cdn-cgi/apps/head/o8VSJLPlJ630VzpFGAYxShTLMQY.js

```js ;window.CloudflareApps=window.CloudflareApps||{};CloudflareApps.siteId="3bf9f6cadf55be30dc8bd75986975e99";CloudflareApps.installs=CloudflareApps.installs||{};;(function(){'use strict' CloudflareApps.internal=CloudflareApps.internal||{} var errors=[] CloudflareApps.internal.placementErrors=errors var errorHashes={} function noteError(options){var hash=options.selector+'::'+options.type+'::'+(options.installId||'') if(errorHashes[hash]){return} errorHashes[hash]=true errors.push(options)} var initializedSelectors={} var currentInit=false CloudflareApps.internal.markSelectors=function markSelectors(){if(!currentInit){check() currentInit=true setTimeout(function(){currentInit=false})}} function check(){var installs=window.CloudflareApps.installs for(var installId in installs){if(!installs.hasOwnProperty(installId)){continue} var selectors=installs[installId].selectors if(!selectors){continue} for(var key in selectors){if(!selectors.hasOwnProperty(key)){continue} var hash=installId+'::'+key if(initializedSelectors[hash]){continue} var els=document.querySelectorAll(selectors[key]) if(els&&els.length>1){noteError({type:'init:too-many',option:key,selector:selectors[key],installId:installId}) initializedSelectors[hash]=true continue}else if(!els||!els.length){continue} initializedSelectors[hash]=true els[0].setAttribute('cfapps-selector',selectors[key])}}} CloudflareApps.querySelector=function querySelector(selector){if(selector==='body'||selector==='head'){return document[selector]} CloudflareApps.internal.markSelectors() var els=document.querySelectorAll('[cfapps-selector="'+selector+'"]') if(!els||!els.length){noteError({type:'select:not-found:by-attribute',selector:selector}) els=document.querySelectorAll(selector) if(!els||!els.length){noteError({type:'select:not-found:by-query',selector:selector}) return null}else if(els.length>1){noteError({type:'select:too-many:by-query',selector:selector})} return els[0]} if(els.length>1){noteError({type:'select:too-many:by-attribute',selector:selector})} return els[0]}}());(function(){'use strict' var prevEls={} CloudflareApps.createElement=function createElement(options,prevEl){options=options||{} CloudflareApps.internal.markSelectors() try{if(prevEl&&prevEl.parentNode){var replacedEl if(prevEl.cfAppsElementId){replacedEl=prevEls[prevEl.cfAppsElementId]} if(replacedEl){prevEl.parentNode.replaceChild(replacedEl,prevEl) delete prevEls[prevEl.cfAppsElementId]}else{prevEl.parentNode.removeChild(prevEl)}} var element=document.createElement('cloudflare-app') var container if(options.pages&&options.pages.URLPatterns&&!CloudflareApps.matchPage(options.pages.URLPatterns)){return element} try{container=CloudflareApps.querySelector(options.selector)}catch(e){} if(!container){return element} if(!container.parentNode&&(options.method==='after'||options.method==='before'||options.method==='replace')){return element} if(container===document.body){if(options.method==='after'){options.method='append'}else if(options.method==='before'){options.method='prepend'}} switch(options.method){case'prepend':if(container.firstChild){container.insertBefore(element,container.firstChild) break} case'append':container.appendChild(element) break case'after':if(container.nextSibling){container.parentNode.insertBefore(element,container.nextSibling)}else{container.parentNode.appendChild(element)} break case'before':container.parentNode.insertBefore(element,container) break case'replace':try{var id=element.cfAppsElementId=Math.random().toString(36) prevEls[id]=container}catch(e){} container.parentNode.replaceChild(element,container)} return element}catch(e){if(typeof console!=='undefined'&&typeof console.error!=='undefined'){console.error('Error creating Cloudflare Apps element',e)}}}}());(function(){'use strict' CloudflareApps.matchPage=function matchPage(patterns){if(!patterns||!patterns.length){return true} var loc=document.location.host+document.location.pathname if(window.CloudflareApps&&CloudflareApps.proxy&&CloudflareApps.proxy.originalURL){var url=CloudflareApps.proxy.originalURL.parsed loc=url.host+url.path} for(var i=0;i

可以尝试去cloudflare那边暂时停用cdn功能检查一下

感谢,我去尝试一下

BH6AOV commented 2 years ago

根据加载失败的内容来看,你应该是使用了 cloudflare cdn 同时在 对应域名-Apps 里安装了一些应用

/cdn-cgi/apps/head/o8VSJLPlJ630VzpFGAYxShTLMQY.js

```js ;window.CloudflareApps=window.CloudflareApps||{};CloudflareApps.siteId="3bf9f6cadf55be30dc8bd75986975e99";CloudflareApps.installs=CloudflareApps.installs||{};;(function(){'use strict' CloudflareApps.internal=CloudflareApps.internal||{} var errors=[] CloudflareApps.internal.placementErrors=errors var errorHashes={} function noteError(options){var hash=options.selector+'::'+options.type+'::'+(options.installId||'') if(errorHashes[hash]){return} errorHashes[hash]=true errors.push(options)} var initializedSelectors={} var currentInit=false CloudflareApps.internal.markSelectors=function markSelectors(){if(!currentInit){check() currentInit=true setTimeout(function(){currentInit=false})}} function check(){var installs=window.CloudflareApps.installs for(var installId in installs){if(!installs.hasOwnProperty(installId)){continue} var selectors=installs[installId].selectors if(!selectors){continue} for(var key in selectors){if(!selectors.hasOwnProperty(key)){continue} var hash=installId+'::'+key if(initializedSelectors[hash]){continue} var els=document.querySelectorAll(selectors[key]) if(els&&els.length>1){noteError({type:'init:too-many',option:key,selector:selectors[key],installId:installId}) initializedSelectors[hash]=true continue}else if(!els||!els.length){continue} initializedSelectors[hash]=true els[0].setAttribute('cfapps-selector',selectors[key])}}} CloudflareApps.querySelector=function querySelector(selector){if(selector==='body'||selector==='head'){return document[selector]} CloudflareApps.internal.markSelectors() var els=document.querySelectorAll('[cfapps-selector="'+selector+'"]') if(!els||!els.length){noteError({type:'select:not-found:by-attribute',selector:selector}) els=document.querySelectorAll(selector) if(!els||!els.length){noteError({type:'select:not-found:by-query',selector:selector}) return null}else if(els.length>1){noteError({type:'select:too-many:by-query',selector:selector})} return els[0]} if(els.length>1){noteError({type:'select:too-many:by-attribute',selector:selector})} return els[0]}}());(function(){'use strict' var prevEls={} CloudflareApps.createElement=function createElement(options,prevEl){options=options||{} CloudflareApps.internal.markSelectors() try{if(prevEl&&prevEl.parentNode){var replacedEl if(prevEl.cfAppsElementId){replacedEl=prevEls[prevEl.cfAppsElementId]} if(replacedEl){prevEl.parentNode.replaceChild(replacedEl,prevEl) delete prevEls[prevEl.cfAppsElementId]}else{prevEl.parentNode.removeChild(prevEl)}} var element=document.createElement('cloudflare-app') var container if(options.pages&&options.pages.URLPatterns&&!CloudflareApps.matchPage(options.pages.URLPatterns)){return element} try{container=CloudflareApps.querySelector(options.selector)}catch(e){} if(!container){return element} if(!container.parentNode&&(options.method==='after'||options.method==='before'||options.method==='replace')){return element} if(container===document.body){if(options.method==='after'){options.method='append'}else if(options.method==='before'){options.method='prepend'}} switch(options.method){case'prepend':if(container.firstChild){container.insertBefore(element,container.firstChild) break} case'append':container.appendChild(element) break case'after':if(container.nextSibling){container.parentNode.insertBefore(element,container.nextSibling)}else{container.parentNode.appendChild(element)} break case'before':container.parentNode.insertBefore(element,container) break case'replace':try{var id=element.cfAppsElementId=Math.random().toString(36) prevEls[id]=container}catch(e){} container.parentNode.replaceChild(element,container)} return element}catch(e){if(typeof console!=='undefined'&&typeof console.error!=='undefined'){console.error('Error creating Cloudflare Apps element',e)}}}}());(function(){'use strict' CloudflareApps.matchPage=function matchPage(patterns){if(!patterns||!patterns.length){return true} var loc=document.location.host+document.location.pathname if(window.CloudflareApps&&CloudflareApps.proxy&&CloudflareApps.proxy.originalURL){var url=CloudflareApps.proxy.originalURL.parsed loc=url.host+url.path} for(var i=0;i

可以尝试去cloudflare那边暂时停用cdn功能检查一下

尝试了一下,把APPS卸载,cdn改bypass之后一样是没有效果的 基本排除cloudflare的问题了

ghost commented 2 years ago

怪... 我用的是 cloudflare analyticsSPA 版本 没有出现这样的问题

你也许可以尝试使用 Hexo injector 而不是主题设置中的 site_analytics 项来设置统计功能 文档在这里

删除主题设置中的 site_analytics 的值 添加以下内容到 site_folder/scripts/analytics.js

hexo.extend.injector.register('head_end', '<!--Bing/Clarity Analytics--><script type="text/javascript">(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);})(window, document, "clarity", "script", "********");</script><!--End of Bing/Clarity Analytics-->', 'default');
BH6AOV commented 2 years ago

怪... 我用的是 cloudflare analyticsSPA 版本 没有出现这样的问题

你也许可以尝试使用 Hexo injector 而不是主题设置中的 site_analytics 项来设置统计功能 文档在这里

删除主题设置中的 site_analytics 的值 添加以下内容到 site_folder/scripts/analytics.js

hexo.extend.injector.register('head_end', '<!--Bing/Clarity Analytics--><script type="text/javascript">(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);})(window, document, "clarity", "script", "********");</script><!--End of Bing/Clarity Analytics-->', 'default');

属于是非常不理解了 晚上试试看吧🤷‍♀️

ghost commented 2 years ago

潜在的相关问题: https://github.com/Candinya/Kratos-Rebirth/issues/103

这个并不是主题的问题,而是 YAML 的语法规范,因为使用 : 分隔键名和键值,所以当键值里出现同样的 : 分隔符时就会出现错误,花括号在 YAML 里也是特殊符号,所以触发了无法解析的问题; 对于字符串类型,使用单引号或是双引号包括起来一般能正确解析,但由于这行代码里已经包含了单双引号,所以目前可能只有删去字符串中冒号后的空格作为比较有效的解决方案。

ArcticLampyrid commented 2 years ago

您好,在新的主题模式实现(#113)中,监听按钮这一步骤在DOMContentLoaded事件触发后进行 这边注意到您开启了CloudflareRocket-Loader,这似乎影响了DOMContentLoaded的正常触发,导致按钮无法被监听 这似乎属于Cloudflare的bug

See also: Understanding Rocket Loader Solution to Missing DOMContentLoaded Event when Enabling both HTML Auto Minify and Rocket Loader in Cloudflare by @HollowMan6

BH6AOV commented 2 years ago

您好,在新的主题模式实现(#113)中,监听按钮这一步骤在DOMContentLoaded事件触发后进行 这边注意到您开启了CloudflareRocket-Loader,这似乎影响了DOMContentLoaded的正常触发,导致按钮无法被监听 这似乎属于Cloudflare的bug

See also: Understanding Rocket Loader Solution to Missing DOMContentLoaded Event when Enabling both HTML Auto Minify and Rocket Loader in Cloudflare by @HollowMan6

非常感谢,刚刚对Rocket-Loader的设置进行了更改,已经解决了这一问题,同时我在对相关问题进行检索后发现一篇文章有提到Rocket-Loader对中国大陆访问者的不便利之处,可能是同一原因造成的这一问题。 再次感谢!

ArcticLampyrid commented 2 years ago

可能是同一原因造成的这一问题。

应该也不仅仅是墙的问题,毕竟我开着梯子也没法正常触发DOMContentLoaded

BH6AOV commented 2 years ago

可能是同一原因造成的这一问题。

应该也不仅仅是墙的问题,毕竟我开着梯子也没法正常触发DOMContentLoaded

我这里也是,就很奇怪,但是至少目前能暂时解决这个问题了