{"size": 2040, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "yupix/hexo-cookieconsent", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "yupix/hexo-cookieconsent", "max_issues_repo_issues_event_min_datetime": "2021-10-09T04:25:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T04:32:36.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "yupix/hexo-cookieconsent", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 32.380952381, "max_line_length": 156, "alphanum_fraction": 0.5269607843}
'use strict';
/* global hexo */
const cheerio = require('cheerio');
const Promise = require('bluebird');

const CookieConsent = () => {
    const route = hexo.route;
    const routeList = route.list();
    const routes = routeList.filter(hpath => hpath.endsWith('.html'));

    const htmls = {};
    return Promise.all(routes.map(hpath => {
        return new Promise((resolve, reject) => {
            const contents = route.get(hpath);
            let htmlTxt = '';
            contents.on('data', (chunk) => (htmlTxt += chunk));
            contents.on('end', () => {
                const $ = cheerio.load(htmlTxt, {decodeEntities: false});
                const options = JSON.stringify(hexo.config.cookieconsent.options);

                let injection = '<script ' +
                    'src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"></script>' +
                    `<script>window.addEventListener("load", function(){window.cookieconsent.initialise(${options})});</script>`;

                $('body').after(injection);

                let styles = '<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.css" />';
                $('head').append(styles);

                htmls[hpath] = {hpath, $};
                resolve();
            });
        });
    }))
        .then(() => {
            const htmlPaths = Object.keys(htmls);
            for (const hpath of htmlPaths) {
                const html = htmls[hpath];
                route.set(hpath, html.$.html());
            }
        });
};


if (!hexo.config.cookieconsent || !hexo.config.cookieconsent.enable) {
    return;
}

hexo.config.cookieconsent = Object.assign({
    options: {
        "palette": {
            "popup": {
                "background": "#000"
            },
            "button": {
                "background": "#f1d600"
            }
        }
    }
}, hexo.config.cookieconsent);

hexo.extend.filter.register('after_generate', CookieConsent);
