hexo-lazyload-image
Version:
a hexo plugin which is used to have all images support lazyload, with the help of this functionality, it will improve lots of the loading proformance.
39 lines (37 loc) • 1.58 kB
JavaScript
const fs = require('hexo-fs');
const UglifyJS = require('uglify-js');
const lazyLoadPath = __dirname + '/simple-lazyload.js';
const thirdPartyFixPath = __dirname + '/third-party-fix.js';
module.exports.addScript = function(htmlContent){
let injectSetting = function () {
return `
<style>
[bg-lazy] {
background-image: none !important;
background-color: #eee !important;
}
</style>
<script>
window.imageLazyLoadSetting = {
isSPA: ${!!this.config.lazyload.isSPA},
preloadRatio: ${this.config.lazyload.preloadRatio || 1},
processImages: null,
};
</script>`;
};
let injectExtraScript = function (filePath) {
if (!fs.exists(filePath)) throw new TypeError(filePath + ' not found!');
let sourceCode = fs.readFileSync(filePath, { escape: true });
return '<script>' + UglifyJS.minify(sourceCode).code + '</script>';
};
let appendScript = function(content, htmlContent) {
let lastIndex = htmlContent.lastIndexOf('</body>');
return htmlContent.substring(0, lastIndex) + content + htmlContent.substring(lastIndex, htmlContent.length);
};
if (/<\/body>/gi.test(htmlContent)) {
htmlContent = appendScript(injectSetting.bind(this)(), htmlContent);
htmlContent = appendScript(injectExtraScript(thirdPartyFixPath), htmlContent);
htmlContent = appendScript(injectExtraScript(lazyLoadPath), htmlContent);
}
return htmlContent;
};