From 7b81a0a172646c96140f7ed99931e802c64bb69e Mon Sep 17 00:00:00 2001 From: EAimTY Date: Fri, 4 Sep 2020 13:47:37 +0900 Subject: [PATCH] minor improvements and upgrade lazysizes --- CHANGELOG.md | 5 +++++ README.md | 4 ++-- assets/js/lazysizes.min.js | 4 ++-- comments.php | 8 ++++++-- footer.php | 2 +- functions.php | 27 ++++++++++++++++++++++++--- index.php | 2 +- page.php | 9 ++++----- 8 files changed, 45 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e04b35..b94cc7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 4.3 +- 更新 lazysizes 至 5.2.2,旧版本 lazysizes 存在跨站漏洞,请尽快更新本主题至 4.3 版本及以上 +- 为图片懒加载(延迟加载)加入开关,并优化了 img 元素的处理过程 +- 现在可以在主题设置中选择未设置 Gravatar 头像的评论者使用的默认头像 +- 独立页面不再显示发布日期 ## 4.2 - 更新 MDUI 至 v1.0.0 ## 4.1.1 diff --git a/README.md b/README.md index ce6b792..29a2d31 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ https://github.com/EAimTY/materiality-typecho-theme - 严格遵循 Material Design - 样式简洁,专注于显示文字内容 - Pjax 无刷新加载页面 -- 图片懒加载 +- 支持图片懒加载 - 可自由切换主题颜色 - 可手动或根据时间(20:00~7:00)自动切换暗色模式 - 可在中文、西文、数字间自动插入空格 @@ -42,7 +42,7 @@ https://github.com/EAimTY/materiality-typecho-theme 4. 清除浏览器缓存、CDN 缓存与网站 Cookies 以免与之前使用的主题发生冲突 # 版本 -4.2 +4.3 # 更新日志 见 [CHANGELOG](https://github.com/EAimTY/materiality-typecho-theme/blob/master/CHANGELOG.md) diff --git a/assets/js/lazysizes.min.js b/assets/js/lazysizes.min.js index dd65ab5..39b2e4f 100644 --- a/assets/js/lazysizes.min.js +++ b/assets/js/lazysizes.min.js @@ -1,2 +1,2 @@ -/* https://github.com/aFarkas/lazysizes */ -!function(a,b){var c=b(a,a.document,Date);a.lazySizes=c,"object"==typeof module&&module.exports&&(module.exports=c)}("undefined"!=typeof window?window:{},function(a,b,c){"use strict";var d,e;if(function(){var b,c={lazyClass:"lazyload",loadedClass:"lazyloaded",loadingClass:"lazyloading",preloadClass:"lazypreload",errorClass:"lazyerror",autosizesClass:"lazyautosizes",srcAttr:"data-src",srcsetAttr:"data-srcset",sizesAttr:"data-sizes",minSize:40,customMedia:{},init:!0,expFactor:1.5,hFac:.8,loadMode:2,loadHidden:!0,ricTimeout:0,throttleDelay:125};e=a.lazySizesConfig||a.lazysizesConfig||{};for(b in c)b in e||(e[b]=c[b])}(),!b||!b.getElementsByClassName)return{init:function(){},cfg:e,noSupport:!0};var f=b.documentElement,g=a.HTMLPictureElement,h="addEventListener",i="getAttribute",j=a[h].bind(a),k=a.setTimeout,l=a.requestAnimationFrame||k,m=a.requestIdleCallback,n=/^picture$/i,o=["load","error","lazyincluded","_lazyloaded"],p={},q=Array.prototype.forEach,r=function(a,b){return p[b]||(p[b]=new RegExp("(\\s|^)"+b+"(\\s|$)")),p[b].test(a[i]("class")||"")&&p[b]},s=function(a,b){r(a,b)||a.setAttribute("class",(a[i]("class")||"").trim()+" "+b)},t=function(a,b){var c;(c=r(a,b))&&a.setAttribute("class",(a[i]("class")||"").replace(c," "))},u=function(a,b,c){var d=c?h:"removeEventListener";c&&u(a,b),o.forEach(function(c){a[d](c,b)})},v=function(a,c,e,f,g){var h=b.createEvent("Event");return e||(e={}),e.instance=d,h.initEvent(c,!f,!g),h.detail=e,a.dispatchEvent(h),h},w=function(b,c){var d;!g&&(d=a.picturefill||e.pf)?(c&&c.src&&!b[i]("srcset")&&b.setAttribute("srcset",c.src),d({reevaluate:!0,elements:[b]})):c&&c.src&&(b.src=c.src)},x=function(a,b){return(getComputedStyle(a,null)||{})[b]},y=function(a,b,c){for(c=c||a.offsetWidth;c49?function(){m(h,{timeout:g}),g!==e.ricTimeout&&(g=e.ricTimeout)}:A(function(){k(h)},!0);return function(a){var e;(a=!0===a)&&(g=33),b||(b=!0,e=f-(c.now()-d),e<0&&(e=0),a||e<9?i():k(i,e))}},C=function(a){var b,d,e=99,f=function(){b=null,a()},g=function(){var a=c.now()-d;a0)&&"visible"!=x(e,"overflow")&&(d=e.getBoundingClientRect(),g=I>d.left&&Hd.top-1&&G500&&f.clientWidth>500?500:370:e.expand,d._defEx=r,s=r*e.expFactor,t=e.hFac,K=null,P2&&p>2&&!b.hidden?(P=s,R=0):P=p>1&&R>1&&Q<6?r:O),q!==l&&(D=innerWidth+l*t,F=innerHeight+l,n=-1*l,q=l),h=u[c].getBoundingClientRect(),(J=h.bottom)>=n&&(G=h.top)<=F&&(I=h.right)>=n*t&&(H=h.left)<=D&&(J||I||H||G)&&(e.loadHidden||T(u[c]))&&(m&&Q<3&&!o&&(p<3||R<4)||U(u[c],l))){if(ba(u[c]),k=!0,Q>9)break}else!k&&m&&!j&&Q<4&&R<4&&p>2&&(g[0]||e.preloadAfterLoad)&&(g[0]||!o&&(J||I||H||G||"auto"!=u[c][i](e.sizesAttr)))&&(j=g[0]||u[c]);j&&!k&&ba(j)}},W=B(V),X=function(a){var b=a.target;if(b._lazyCache)return void delete b._lazyCache;S(a),s(b,e.loadedClass),t(b,e.loadingClass),u(b,Z),v(b,"lazyloaded")},Y=A(X),Z=function(a){Y({target:a.target})},$=function(a,b){try{a.contentWindow.location.replace(b)}catch(c){a.src=b}},_=function(a){var b,c=a[i](e.srcsetAttr);(b=e.customMedia[a[i]("data-media")||a[i]("media")])&&a.setAttribute("media",b),c&&a.setAttribute("srcset",c)},aa=A(function(a,b,c,d,f){var g,h,j,l,m,p;(m=v(a,"lazybeforeunveil",b)).defaultPrevented||(d&&(c?s(a,e.autosizesClass):a.setAttribute("sizes",d)),h=a[i](e.srcsetAttr),g=a[i](e.srcAttr),f&&(j=a.parentNode,l=j&&n.test(j.nodeName||"")),p=b.firesLoad||"src"in a&&(h||g||l),m={target:a},s(a,e.loadingClass),p&&(clearTimeout(o),o=k(S,2500),u(a,Z,!0)),l&&q.call(j.getElementsByTagName("source"),_),h?a.setAttribute("srcset",h):g&&!l&&(M.test(a.nodeName)?$(a,g):a.src=g),f&&(h||l)&&w(a,{src:g})),a._lazyRace&&delete a._lazyRace,t(a,e.lazyClass),z(function(){var b=a.complete&&a.naturalWidth>1;p&&!b||(b&&s(a,"ls-is-cached"),X(m),a._lazyCache=!0,k(function(){"_lazyCache"in a&&delete a._lazyCache},9)),"lazy"==a.loading&&Q--},!0)}),ba=function(a){if(!a._lazyRace){var b,c=L.test(a.nodeName),d=c&&(a[i](e.sizesAttr)||a[i]("sizes")),f="auto"==d;(!f&&m||!c||!a[i]("src")&&!a.srcset||a.complete||r(a,e.errorClass)||!r(a,e.lazyClass))&&(b=v(a,"lazyunveilread").detail,f&&E.updateElem(a,!0,a.offsetWidth),a._lazyRace=!0,Q++,aa(a,b,f,d,c))}},ca=C(function(){e.loadMode=3,W()}),da=function(){3==e.loadMode&&(e.loadMode=2),ca()},ea=function(){if(!m){if(c.now()-y<999)return void k(ea,999);m=!0,e.loadMode=3,W(),j("scroll",da,!0)}};return{_:function(){y=c.now(),d.elements=b.getElementsByClassName(e.lazyClass),g=b.getElementsByClassName(e.lazyClass+" "+e.preloadClass),j("scroll",W,!0),j("resize",W,!0),j("pageshow",function(a){if(a.persisted){var c=b.querySelectorAll("."+e.loadingClass);c.length&&c.forEach&&l(function(){c.forEach(function(a){a.complete&&ba(a)})})}}),a.MutationObserver?new MutationObserver(W).observe(f,{childList:!0,subtree:!0,attributes:!0}):(f[h]("DOMNodeInserted",W,!0),f[h]("DOMAttrModified",W,!0),setInterval(W,999)),j("hashchange",W,!0),["focus","mouseover","click","load","transitionend","animationend"].forEach(function(a){b[h](a,W,!0)}),/d$|^c/.test(b.readyState)?ea():(j("load",ea),b[h]("DOMContentLoaded",W),k(ea,2e4)),d.elements.length?(V(),z._lsFlush()):W()},checkElems:W,unveil:ba,_aLSL:da}}(),E=function(){var a,c=A(function(a,b,c,d){var e,f,g;if(a._lazysizesWidth=d,d+="px",a.setAttribute("sizes",d),n.test(b.nodeName||""))for(e=b.getElementsByTagName("source"),f=0,g=e.length;f49?function(){l(t,{timeout:n});if(n!==H.ricTimeout){n=H.ricTimeout}}:te(function(){I(t)},true);return function(e){var t;if(e=e===true){n=33}if(i){return}i=true;t=r-(f.now()-a);if(t<0){t=0}if(e||t<9){s()}else{I(s,t)}}},ae=function(e){var t,i;var a=99;var r=function(){t=null;e()};var n=function(){var e=f.now()-i;if(e0;if(r&&Z(a,"overflow")!="visible"){i=a.getBoundingClientRect();r=C>i.left&&pi.top-1&&g500&&O.clientWidth>500?500:370:H.expand;k._defEx=u;f=u*H.expFactor;c=H.hFac;A=null;if(w2&&h>2&&!D.hidden){w=f;M=0}else if(h>1&&M>1&&N<6){w=u}else{w=_}}if(o!==n){y=innerWidth+n*c;z=innerHeight+n;s=n*-1;o=n}i=d[t].getBoundingClientRect();if((b=i.bottom)>=s&&(g=i.top)<=z&&(C=i.right)>=s*c&&(p=i.left)<=y&&(b||C||p||g)&&(H.loadHidden||W(d[t]))&&(m&&N<3&&!l&&(h<3||M<4)||S(d[t],n))){R(d[t]);r=true;if(N>9){break}}else if(!r&&m&&!a&&N<4&&M<4&&h>2&&(v[0]||H.preloadAfterLoad)&&(v[0]||!l&&(b||C||p||g||d[t][$](H.sizesAttr)!="auto"))){a=v[0]||d[t]}}if(a&&!r){R(a)}}};var i=ie(t);var B=function(e){var t=e.target;if(t._lazyCache){delete t._lazyCache;return}x(e);K(t,H.loadedClass);Q(t,H.loadingClass);V(t,L);X(t,"lazyloaded")};var a=te(B);var L=function(e){a({target:e.target})};var T=function(t,i){try{t.contentWindow.location.replace(i)}catch(e){t.src=i}};var F=function(e){var t;var i=e[$](H.srcsetAttr);if(t=H.customMedia[e[$]("data-media")||e[$]("media")]){e.setAttribute("media",t)}if(i){e.setAttribute("srcset",i)}};var s=te(function(t,e,i,a,r){var n,s,l,o,u,f;if(!(u=X(t,"lazybeforeunveil",e)).defaultPrevented){if(a){if(i){K(t,H.autosizesClass)}else{t.setAttribute("sizes",a)}}s=t[$](H.srcsetAttr);n=t[$](H.srcAttr);if(r){l=t.parentNode;o=l&&j.test(l.nodeName||"")}f=e.firesLoad||"src"in t&&(s||n||o);u={target:t};K(t,H.loadingClass);if(f){clearTimeout(c);c=I(x,2500);V(t,L,true)}if(o){G.call(l.getElementsByTagName("source"),F)}if(s){t.setAttribute("srcset",s)}else if(n&&!o){if(d.test(t.nodeName)){T(t,n)}else{t.src=n}}if(r&&(s||o)){Y(t,{src:n})}}if(t._lazyRace){delete t._lazyRace}Q(t,H.lazyClass);ee(function(){var e=t.complete&&t.naturalWidth>1;if(!f||e){if(e){K(t,"ls-is-cached")}B(u);t._lazyCache=true;I(function(){if("_lazyCache"in t){delete t._lazyCache}},9)}if(t.loading=="lazy"){N--}},true)});var R=function(e){if(e._lazyRace){return}var t;var i=n.test(e.nodeName);var a=i&&(e[$](H.sizesAttr)||e[$]("sizes"));var r=a=="auto";if((r||!m)&&i&&(e[$]("src")||e.srcset)&&!e.complete&&!J(e,H.errorClass)&&J(e,H.lazyClass)){return}t=X(e,"lazyunveilread").detail;if(r){re.updateElem(e,true,e.offsetWidth)}e._lazyRace=true;N++;s(e,t,r,a,i)};var r=ae(function(){H.loadMode=3;i()});var l=function(){if(H.loadMode==3){H.loadMode=2}r()};var o=function(){if(m){return}if(f.now()-e<999){I(o,999);return}m=true;H.loadMode=3;i();q("scroll",l,true)};return{_:function(){e=f.now();k.elements=D.getElementsByClassName(H.lazyClass);v=D.getElementsByClassName(H.lazyClass+" "+H.preloadClass);q("scroll",i,true);q("resize",i,true);q("pageshow",function(e){if(e.persisted){var t=D.querySelectorAll("."+H.loadingClass);if(t.length&&t.forEach){U(function(){t.forEach(function(e){if(e.complete){R(e)}})})}}});if(u.MutationObserver){new MutationObserver(i).observe(O,{childList:true,subtree:true,attributes:true})}else{O[P]("DOMNodeInserted",i,true);O[P]("DOMAttrModified",i,true);setInterval(i,999)}q("hashchange",i,true);["focus","mouseover","click","load","transitionend","animationend"].forEach(function(e){D[P](e,i,true)});if(/d$|^c/.test(D.readyState)){o()}else{q("load",o);D[P]("DOMContentLoaded",i);I(o,2e4)}if(k.elements.length){t();ee._lsFlush()}else{i()}},checkElems:i,unveil:R,_aLSL:l}}(),re=function(){var i;var n=te(function(e,t,i,a){var r,n,s;e._lazysizesWidth=a;a+="px";e.setAttribute("sizes",a);if(j.test(t.nodeName||"")){r=t.getElementsByTagName("source");for(n=0,s=r.length;n
-
+
author(); ?>
date(); ?>
-
/', '', $comments->content); ?>
+
content(); ?>
reply('
'); ?> children): ?>
@@ -30,7 +30,11 @@
respondId(); ?>
comments()->to($comments); ?> have()): ?> + listComments(['before' => '', 'after' => '']); ?> + + + options->defaultGravatar, $commentsHTML); ?> allow('comment')): ?>
diff --git a/footer.php b/footer.php index 7685770..9290a8d 100644 --- a/footer.php +++ b/footer.php @@ -45,4 +45,4 @@ footer(); ?> -options->feature), in_array('compressHTML', $this->options->feature)); ?> +options->feature), in_array('compressHTML', $this->options->feature), in_array('lazyLoad', $this->options->feature)); ?> diff --git a/functions.php b/functions.php index cfb3aab..853f5f0 100644 --- a/functions.php +++ b/functions.php @@ -216,10 +216,19 @@ function outputStart() { ob_start(); } -function outputEnd($pangu, $compressHTML) { +function outputEnd($pangu, $compressHTML, $lazyLoad) { $output = ob_get_contents(); ob_end_clean(); - $output = preg_replace('//', '', $output); + if ($lazyLoad) { + $dom = new DOMDocument(); + @$dom->loadHTML($output); + foreach ($dom->getElementsByTagName('img') as $node) { + $node->setAttribute("class", $node->getAttribute('class') . " lazyload"); + $node->setAttribute("data-src", $node->getAttribute('src')); + $node->removeAttribute("src"); + } + $output = $dom->saveHtml(); + } if ($pangu || $compressHTML) { $output = preg_split('/(||||
)/msi', $output, NULL, PREG_SPLIT_DELIM_CAPTURE); foreach ($output as $key => $value) { @@ -299,8 +308,9 @@ function themeConfig($cfg) { 'autoDark' => _t('自动切换至暗色模式(20:00~7:00)'), 'pangu' => _t('在中文、西文、数字间自动插入空格'), 'compressHTML' => _t('启用 HTML 压缩(需要消耗一定性能,不建议在服务器性能低或网站 PV 高时开启)'), + 'lazyLoad' => _t('延迟加载图片(在页面中其它内容加载完毕后再加载图片,能够优化多图片页面的加载速度)'), 'smoothScroll' => _t('启用惯性滚动(将改善页面滚动时的体验,但可能会造成页面滚动时轻微掉帧)') - ], ['autoDark', 'pangu', 'smoothScroll'], _t('主题功能设置')); + ], ['autoDark', 'smoothScroll'], _t('主题功能设置')); $cfg->addInput($feature->multiMode()); $appbar = new Typecho_Widget_Helper_Form_Element_Checkbox('appbar', [ @@ -329,6 +339,17 @@ function themeConfig($cfg) { ], ['author', 'category', 'comment_disabled'], _t('文章信息设置')); $cfg->addInput($article->multiMode()); + $defaultGravatar = new Typecho_Widget_Helper_Form_Element_Select('defaultGravatar', [ + 'mp' => '神秘人', + '' => 'Gravatar Logo', + 'identicon' => '随机的几何图案', + 'monsterid' => '随机的小怪兽', + 'wavatar' => '随机的卡通脸', + 'retro' => '随机的像素图案', + 'robohash' => '随机的小机器人' + ], 'mp', _t('评论者默认头像'), _t('在评论者没有设置过 Gravatar 时使用的头像')); + $cfg->addInput($defaultGravatar->multiMode()); + $primaryColor = new Typecho_Widget_Helper_Form_Element_Select('primaryColor', [ 'indigo' => 'Indigo', 'red' => 'Red', diff --git a/index.php b/index.php index f5ead55..9fc1b5c 100644 --- a/index.php +++ b/index.php @@ -4,7 +4,7 @@ * * @package materiality-typecho-theme * @author EAimTY - * @version 4.2 + * @version 4.3 * @link https://www.eaimty.com/ */ ?> diff --git a/page.php b/page.php index 6b8f1c9..8b6a709 100644 --- a/page.php +++ b/page.php @@ -3,12 +3,11 @@
title(); ?>
-
- date(); ?> - options->article)): ?> + options->article)): ?> +
|author(); ?> - -
+
+
fields->index == "show"): ?>