Skip to content

Commit

Permalink
Modify code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zkqiang committed Apr 4, 2020
1 parent db8041f commit 65bc026
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
44 changes: 1 addition & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,3 @@
'use strict';

hexo.extend.filter.register('after_render:html', function (html) {
const config = hexo.config;
if (!config.memorial_day.enable) {
return;
}

if (html.search('<html') === -1 || html.search('grayscale(100%)') > -1) {
return;
}

const css = 'html { \
-webkit-filter: grayscale(100%); /* webkit */ \
-moz-filter: grayscale(100%); /* firefox */ \
-ms-filter: grayscale(100%); /* ie9 */ \
-o-filter: grayscale(100%); /* opera */ \
filter: grayscale(100%); \
filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); filter:gray; /* ie9- */ \
}';

const js = `
<script type="text/javascript">
(function() {
var now = new Date();
var dayStr = '${ config.memorial_day.day }';
var day = dayStr ? new Date(dayStr) : new Date();
var isMemorialDay = now.getFullYear() === day.getFullYear() && now.getMonth() === day.getMonth() && now.getDay() === day.getDay();
if (isMemorialDay) {
if (document.all) {
window.style = '${ css }';
document.createStyleSheet('javascript:style');
} else {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '${ css }';
document.getElementsByTagName('HEAD').item(0).appendChild(style);
}
}
})();
</script>
`;

return js + html;
});
hexo.extend.filter.register('after_render:html', require('./lib/filter'));
40 changes: 40 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = function (html, data) {
const config = this.config;

if (!config.memorial_day || !config.memorial_day.enable || html.search('<html') === -1 || html.search('grayscale(100%)') > -1) {
return html;
}

const css = 'html { \
-webkit-filter: grayscale(100%); /* webkit */ \
-moz-filter: grayscale(100%); /* firefox */ \
-ms-filter: grayscale(100%); /* ie9 */ \
-o-filter: grayscale(100%); /* opera */ \
filter: grayscale(100%); \
filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); filter:gray; /* ie9- */ \
}';

const js = `
<script type="text/javascript">
(function() {
var now = new Date();
var dayStr = '${ config.memorial_day.day }';
var day = dayStr ? new Date(dayStr) : new Date();
var isMemorialDay = now.getFullYear() === day.getFullYear() && now.getMonth() === day.getMonth() && now.getDay() === day.getDay();
if (isMemorialDay) {
if (document.all) {
window.style = '${ css }';
document.createStyleSheet('javascript:style');
} else {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '${ css }';
document.getElementsByTagName('HEAD').item(0).appendChild(style);
}
}
})();
</script>
`;

return js + html;
};

0 comments on commit 65bc026

Please sign in to comment.