Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate third party inline scripts #241

Merged
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ quicklink:

# For more flexibility you can add some patterns (RegExp, Function, or Array) to ignores.
# See: https://github.com/GoogleChromeLabs/quicklink#custom-ignore-patterns
# This option is deprecated. Use `CONFIG.quicklink.ignores` in your custom scripts instead.
ignores:


Expand Down
8 changes: 1 addition & 7 deletions layout/_layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{{ partial('_partials/head/head.njk', {}, {cache: theme.cache.enable}) }}
{%- include '_partials/head/head-unique.njk' -%}
<title>{% block title %}{% endblock %}</title>
{{- next_js('load-config.js', true) }}
{{- next_js('config.js') }}
{{- next_inject('head') }}
{{ partial('_third-party/analytics/index.njk', {}, {cache: theme.cache.enable}) }}
<noscript>
Expand Down Expand Up @@ -45,15 +45,9 @@
{{ partial('_third-party/index.njk', {}, {cache: theme.cache.enable}) }}
{{ partial('_third-party/statistics/index.njk', {}, {cache: theme.cache.enable}) }}

{%- if theme.pjax %}
<div class="pjax">
{%- endif %}
{%- include '_third-party/math/index.njk' -%}
{%- include '_third-party/quicklink.njk' -%}

{{- next_inject('bodyEnd') }}
{%- if theme.pjax %}
</div>
{%- endif %}
</body>
</html>
17 changes: 5 additions & 12 deletions layout/_partials/head/head-unique.njk
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@

{# Exports some front-matter variables to Front-End #}
{# https://hexo.io/docs/variables.html #}
<meta name="hexo-config-page" content="{{
{
sidebar: page.sidebar or '',
isHome : is_home(),
isPost : is_post(),
lang : page.lang
} | safedump | escape
}}">
<meta name="hexo-config-calendar" content="{{
theme.calendar | safedump | escape
if page.type === 'schedule' and not page.title
}}">
{{ next_data('page', next_config_unique()) }}

{{ next_data('calendar',
theme.calendar if page.type === 'schedule' and not page.title else '')
}}
2 changes: 1 addition & 1 deletion layout/_partials/head/head.njk
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
<script src="{{ theme.vendors.nprogress_js }}"></script>
{%- endif %}

<meta name="hexo-config" content="{{ next_config() | safedump | escape }}">
{{ next_data('main', next_config()) }}
11 changes: 2 additions & 9 deletions layout/_third-party/analytics/baidu-analytics.njk
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{%- if theme.baidu_analytics %}
<script{{ pjax }}>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?{{ theme.baidu_analytics }}";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
{{ next_js('third-party/analytics/baidu-analytics.js') }}
<script async src="https://hm.baidu.com/hm.js?{{ theme.baidu_analytics }}"></script>
{%- endif %}
27 changes: 2 additions & 25 deletions layout/_third-party/analytics/google-analytics.njk
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
{%- if theme.google_analytics.tracking_id %}
{%- if not theme.google_analytics.only_pageview %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ theme.google_analytics.tracking_id }}"></script>
<script{{ pjax }}>
if (CONFIG.hostname === location.hostname) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ theme.google_analytics.tracking_id }}');
}
</script>
{%- else %}
<script>
function sendPageView() {
if (CONFIG.hostname !== location.hostname) return;
var uid = localStorage.getItem('uid') || (Math.random() + '.' + Math.random());
localStorage.setItem('uid', uid);
navigator.sendBeacon('https://www.google-analytics.com/collect', new URLSearchParams({
v : 1,
tid: '{{ theme.google_analytics.tracking_id }}',
cid: uid,
t : 'pageview',
dp : encodeURIComponent(location.pathname)
}));
}
document.addEventListener('pjax:complete', sendPageView);
sendPageView();
</script>
{%- endif %}
{{ next_data('google_analytics', theme.google_analytics) }}
{{ next_js('third-party/analytics/google-analytics.js') }}
{%- endif %}
8 changes: 3 additions & 5 deletions layout/_third-party/analytics/growingio.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{%- if theme.growingio_analytics %}
<script{{ pjax }}>
!function(e,t,n,g,i){e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},n=t.createElement("script"),tag=t.getElementsByTagName("script")[0],n.async=1,n.src=('https:'==document.location.protocol?'https://':'http://')+g,tag.parentNode.insertBefore(n,tag)}(window,document,"script","assets.growingio.com/2.1/gio.js","gio");
gio('init', '{{ theme.growingio_analytics }}', {});
gio('send');
</script>
<script async src="//assets.growingio.com/2.1/gio.js"></script>
{{ next_data('growingio_analytics', theme.growingio_analytics) }}
{{ next_js('third-party/analytics/growingio.js') }}
{%- endif %}
23 changes: 3 additions & 20 deletions layout/_third-party/chat/chatra.njk
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
{%- if theme.chatra.enable %}
{%- if theme.chatra.embed %}
<script>
window.ChatraSetup = {
mode : 'frame',
injectTo: '{{ theme.chatra.embed }}'
};
</script>
{%- endif %}
<script>
(function(d, w, c) {
w.ChatraID = '{{ theme.chatra.id }}';
var s = d.createElement('script');
w[c] = w[c] || function() {
(w[c].q = w[c].q || []).push(arguments);
};
s.async = {{ theme.chatra.async }};
s.src = 'https://call.chatra.io/chatra.js';
if (d.head) d.head.appendChild(s);
})(document, window, 'Chatra');
</script>
{{ next_data('chatra', theme.chatra) }}
{{ next_js('third-party/chat/chatra.js') }}
<script{{ ' async' if theme.chatra.async }} src="https://call.chatra.io/chatra.js"></script>
{%- endif %}
17 changes: 2 additions & 15 deletions layout/_third-party/comments/changyan.njk
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
{%- if is_home() %}
<script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId={{ theme.changyan.appid }}"></script>
{% elif page.comments %}
<script>
NexT.utils.loadComments('#SOHUCS', () => {
NexT.utils.getScript('https://changyan.sohu.com/upload/changyan.js', () => {
window.changyan.api.config({
appid: '{{ theme.changyan.appid }}',
conf : '{{ theme.changyan.appkey }}'
});
});
});
</script>
<script src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
{%- endif %}
{{ next_data('changyan', theme.changyan) }}
{{ next_js('third-party/comments/changyan.js') }}
41 changes: 5 additions & 36 deletions layout/_third-party/comments/disqus.njk
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
{%- if theme.disqus.count %}
<script>
function loadCount() {
var d = document, s = d.createElement('script');
s.src = 'https://{{ theme.disqus.shortname }}.disqus.com/count.js';
s.id = 'dsq-count-scr';
(d.head || d.body).appendChild(s);
{{ next_data('disqus', theme.disqus, {
i18n: {
disqus: __('disqus')
}
// defer loading until the whole page loading is completed
window.addEventListener('load', loadCount, false);
</script>
{%- endif %}
{%- if page.comments %}
<script>
var disqus_config = function() {
this.page.url = {{ page.permalink | safedump }};
this.page.identifier = {{ page.path | safedump }};
this.page.title = {{ page.title | safedump }};
{% if __('disqus') !== 'disqus' -%}
this.language = '{{ __('disqus') }}';
{% endif -%}
};
NexT.utils.loadComments('#disqus_thread', () => {
if (window.DISQUS) {
DISQUS.reset({
reload: true,
config: disqus_config
});
} else {
var d = document, s = d.createElement('script');
s.src = 'https://{{ theme.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', '' + +new Date());
(d.head || d.body).appendChild(s);
}
});
</script>
{%- endif %}
}) }}
{{ next_js('third-party/comments/disqus.js') }}
20 changes: 4 additions & 16 deletions layout/_third-party/comments/disqusjs.njk
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
{%- if page.comments %}
<link rel="stylesheet" href="{{ theme.vendors.disqusjs_css }}">

<script>
NexT.utils.loadComments('#disqus_thread', () => {
NexT.utils.getScript('{{ theme.vendors.disqusjs_js }}', () => {
window.dsqjs = new DisqusJS({
api : '{{ theme.disqusjs.api }}' || 'https://disqus.com/api/',
apikey : '{{ theme.disqusjs.apikey }}',
shortname : '{{ theme.disqusjs.shortname }}',
url : {{ page.permalink | safedump }},
identifier: {{ page.path | safedump }},
title : {{ page.title | safedump }},
});
}, window.DisqusJS);
});
</script>
{%- endif %}
{{ next_data('disqusjs', theme.disqusjs, {
js: theme.vendors.disqusjs_js
}) }}
{{ next_js('third-party/comments/disqusjs.js') }}
29 changes: 5 additions & 24 deletions layout/_third-party/comments/gitalk.njk
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
{%- if page.comments %}
<link rel="stylesheet" href="{{ theme.vendors.gitalk_css }}">

<script>
NexT.utils.loadComments('.gitalk-container', () => {
NexT.utils.getScript('{{ theme.vendors.gitalk_js }}', () => {
var gitalk = new Gitalk({
clientID : '{{ theme.gitalk.client_id }}',
clientSecret: '{{ theme.gitalk.client_secret }}',
repo : '{{ theme.gitalk.repo }}',
owner : '{{ theme.gitalk.github_id }}',
admin : ['{{ theme.gitalk.admin_user }}'],
id : '{{ gitalk_md5(page.path) }}',
proxy : '{{ theme.gitalk.proxy }}',
{%- if theme.gitalk.language == '' %}
language: window.navigator.language,
{% else %}
language: '{{ theme.gitalk.language }}',
{%- endif %}
distractionFreeMode: {{ theme.gitalk.distraction_free_mode }}
});
gitalk.render(document.querySelector('.gitalk-container'));
}, window.Gitalk);
});
</script>
{%- endif %}
{{ next_data('gitalk', theme.gitalk, {
js: theme.vendors.gitalk_js,
path_md5: gitalk_md5(page.path)
}) }}
{{ next_js('third-party/comments/gitalk.js') }}
12 changes: 2 additions & 10 deletions layout/_third-party/comments/isso.njk
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
{%- if page.comments %}
<script>
NexT.utils.loadComments('#isso-thread', () => {
const script = document.createElement('script');
script.src = '{{ theme.isso }}js/embed.min.js';
script.dataset.isso = '{{ theme.isso }}';
document.querySelector('#isso-thread').appendChild(script);
});
</script>
{%- endif %}
{{ next_data('isso', theme.isso) }}
{{ next_js('third-party/comments/isso.js') }}
18 changes: 1 addition & 17 deletions layout/_third-party/comments/livere.njk
Original file line number Diff line number Diff line change
@@ -1,17 +1 @@
{%- if page.comments %}
<script>
NexT.utils.loadComments('#lv-container', () => {
window.livereOptions = {
refer: {{ page.path | replace(r/index\.html$/, '') | safedump }}
};
(function(d, s) {
var j, e = d.getElementsByTagName(s)[0];
if (typeof LivereTower === 'function') { return; }
j = d.createElement(s);
j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
j.async = true;
e.parentNode.insertBefore(j, e);
})(document, 'script');
});
</script>
{%- endif %}
{{ next_js('third-party/comments/livere.js') }}
16 changes: 2 additions & 14 deletions layout/_third-party/comments/utterances.njk
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
{%- if page.comments %}
<script>
NexT.utils.loadComments('.utterances-container', () => {
const script = document.createElement('script');
script.src = 'https://utteranc.es/client.js';
script.setAttribute('repo', {{ theme.utterances.repo | safedump }});
script.setAttribute('issue-term', {{ theme.utterances.issue_term | safedump }});
script.setAttribute('theme', {{ theme.utterances.theme | safedump }});
script.crossOrigin = 'anonymous';
script.async = true;
document.querySelector('.utterances-container').appendChild(script);
});
</script>
{%- endif %}
{{ next_data('utterances', theme.utterances) }}
{{ next_js('third-party/comments/utterances.js') }}
16 changes: 11 additions & 5 deletions layout/_third-party/math/index.njk
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{%- if theme.math.mathjax.enable or theme.math.katex.enable %}
{%- set enable_math = false %}

{%- set is_index_has_math = false %}

{# At home, check if there has `mathjax: true` post #}
Expand All @@ -11,10 +13,14 @@
{%- endif %}

{%- if theme.math.every_page or is_index_has_math or page.mathjax %}
{%- if theme.math.mathjax.enable %}
{%- include '_third-party/math/mathjax.njk' -%}
{% elif theme.math.katex.enable %}
{%- include '_third-party/math/katex.njk' -%}
{%- endif %}
{%- set enable_math = true %}
{%- endif %}

{{ next_data('enableMath', enable_math) }}

{%- if theme.math.mathjax.enable %}
{%- include '_third-party/math/mathjax.njk' -%}
{% elif theme.math.katex.enable %}
{%- include '_third-party/math/katex.njk' -%}
{%- endif %}
{%- endif %}
5 changes: 4 additions & 1 deletion layout/_third-party/math/katex.njk
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<link rel="stylesheet" href="{{ theme.vendors.katex }}">
{%- if theme.math.katex.copy_tex %}
<script src="{{ theme.vendors.copy_tex_js }}"></script>
<link rel="stylesheet" href="{{ theme.vendors.copy_tex_css }}">
{{ next_data('katex', {
copy_tex_js: theme.vendors.copy_tex_js
}) }}
{{ next_js('third-party/math/katex.js') }}
{%- endif %}
35 changes: 4 additions & 31 deletions layout/_third-party/math/mathjax.njk
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
<script>
if (typeof MathJax === 'undefined') {
window.MathJax = {
tex: {
inlineMath: {'[+]': [['$', '$']]},
tags: '{{ theme.math.mathjax.tags }}'
},
options: {
renderActions: {
insertedScript: [200, () => {
document.querySelectorAll('mjx-container').forEach(node => {
const target = node.parentNode;
if (target.nodeName.toLowerCase() === 'li') {
target.parentNode.classList.add('has-jax');
}
});
}, '', false]
}
}
};
const script = document.createElement('script');
script.src = '{{ theme.vendors.mathjax }}';
script.defer = true;
document.head.appendChild(script);
} else {
MathJax.startup.document.state(0);
MathJax.typesetClear();
MathJax.texReset();
MathJax.typeset();
}
</script>
{{ next_data('mathjax', theme.math.mathjax, {
js: theme.vendors.mathjax
}) }}
{{ next_js('third-party/math/mathjax.js') }}
Loading