An injector is used to add static code snippet to the <head> or/and <body> of generated HTML files. Hexo run injector before after_render:html filter is executed.
Synopsis
hexo.extend.injector.register(entry, value, to);
entry <string>
Where the code will be injected inside the HTML.
Support those values:
head_begin: Inject code snippet right after<head>(Default).head_end: Inject code snippet right before</head>.body_begin: Inject code snippet right after<body>.body_end: Inject code snippet right before</body>.
value <string> | <Function>
A function that returns string is supported.
The code snippet to be injected.
to <string>
Which page will code snippets being injected.
default: Inject to every page (Default).home: Only inject to home page (which hasis_home()helper beingtrue)post: Only inject to post pages (which hasis_post()helper beingtrue)page: Only inject to pages (which hasis_page()helper beingtrue)archive: Only inject to archive pages (which hasis_archive()helper beingtrue)category: Only inject to category pages (which hasis_category()helper beingtrue)tag: Only inject to tag pages (which hasis_tag()helper beingtrue)- Custom layout name could be used as well, see Writing - Layout.
There are other internal functions, see hexojs/hexo#4049 for more details.
Example
const css = hexo.extend.helper.get("css").bind(hexo);
const js = hexo.extend.helper.get("js").bind(hexo);
hexo.extend.injector.register(
"head_end",
() => {
return css(
"https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css",
);
},
"music",
);
hexo.extend.injector.register(
"body_end",
'<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js">',
"music",
);
hexo.extend.injector.register("body_end", () => {
return js("/js/jquery.js");
});
Above setup will inject APlayer.min.css (<link> tag) to the </head> of any page which layout is music, and APlayer.min.js (<script> tag) to the </body> of those pages. Also, jquery.js (<script> tag) will be injected to </body> of every page generated.
Accessing user configuration
Use any of the following options:
const css = hexo.extend.helper.get("css").bind(hexo);
hexo.extend.injector.register("head_end", () => {
const { cssPath } = hexo.config.fooPlugin;
return css(cssPath);
});
/* global hexo */
hexo.extend.injector.register("head_end", require("./lib/inject").bind(hexo));
module.exports = function () {
const css = this.extend.helper.get("css");
const { cssPath } = this.config.fooPlugin;
return css(cssPath);
};
function injectFn() {
const css = this.extend.helper.get("css");
const { cssPath } = this.config.fooPlugin;
return css(cssPath);
}
module.exports = injectFn;
/* global hexo */
hexo.extend.injector.register("head_end", require("./lib/inject")(hexo));
module.exports = (hexo) => () => {
const css = hexo.extend.helper.get("css").bind(hexo);
const { cssPath } = hexo.config.fooPlugin;
return css(cssPath);
};
const injectFn = (hexo) => {
const css = hexo.extend.helper.get("css").bind(hexo);
const { cssPath } = hexo.config.fooPlugin;
return css(cssPath);
};
module.exports = (hexo) => injectFn(hexo);