|
17 | 17 | </div> |
18 | 18 | </footer> |
19 | 19 |
|
20 | | -<!-- jQuery --> |
| 20 | +<!-- Critical JavaScript - Load Immediately --> |
21 | 21 | <script src="{{ "/js/jquery.min.js " | prepend: site.baseurl }}"></script> |
22 | 22 |
|
23 | | -<!-- Bootstrap Core JavaScript --> |
24 | | -<!-- Currently, only navbar scroll-down effect at desktop still depends on this --> |
25 | | -<script src="{{ "/js/bootstrap.min.js " | prepend: site.baseurl }}"></script> |
26 | | - |
27 | | -<!-- Custom Theme JavaScript --> |
28 | | -<script src="{{ "/js/jason-blog.min.js " | prepend: site.baseurl }}"></script> |
29 | | - |
30 | | -<!-- Simple Jekyll Search --> |
31 | | -<script src="{{ "/js/simple-jekyll-search.min.js" | prepend: site.baseurl }}"></script> |
| 23 | +<!-- Non-Critical JavaScript - Load Asynchronously --> |
| 24 | +<script> |
| 25 | + // Async load non-critical scripts |
| 26 | + function loadScript(src, callback) { |
| 27 | + var script = document.createElement('script'); |
| 28 | + script.src = src; |
| 29 | + script.async = true; |
| 30 | + if (callback) script.onload = callback; |
| 31 | + document.head.appendChild(script); |
| 32 | + } |
| 33 | + |
| 34 | + // Load scripts after page load |
| 35 | + window.addEventListener('load', function() { |
| 36 | + loadScript('{{ "/js/bootstrap.min.js " | prepend: site.baseurl }}'); |
| 37 | + loadScript('{{ "/js/jason-blog.min.js " | prepend: site.baseurl }}'); |
| 38 | + loadScript('{{ "/js/simple-jekyll-search.min.js" | prepend: site.baseurl }}'); |
| 39 | + |
| 40 | + // Initialize all features when page loads |
| 41 | + initLazyLoading(); |
| 42 | + initReadingProgress(); |
| 43 | + initBackToTop(); |
| 44 | + initSocialShare(); |
| 45 | + }); |
| 46 | + |
| 47 | + // Reading Progress Bar |
| 48 | + function initReadingProgress() { |
| 49 | + const progressBar = document.getElementById('reading-progress'); |
| 50 | + if (!progressBar) return; |
| 51 | + |
| 52 | + function updateProgress() { |
| 53 | + const scrollTop = window.pageYOffset || document.documentElement.scrollTop; |
| 54 | + const scrollHeight = document.documentElement.scrollHeight - window.innerHeight; |
| 55 | + const progress = (scrollTop / scrollHeight) * 100; |
| 56 | + progressBar.style.width = Math.min(progress, 100) + '%'; |
| 57 | + } |
| 58 | + |
| 59 | + window.addEventListener('scroll', updateProgress); |
| 60 | + window.addEventListener('resize', updateProgress); |
| 61 | + updateProgress(); // Initial call |
| 62 | + } |
| 63 | + |
| 64 | + // Back to Top functionality |
| 65 | + function initBackToTop() { |
| 66 | + const backToTopBtn = document.getElementById('back-to-top'); |
| 67 | + if (!backToTopBtn) return; |
| 68 | + |
| 69 | + window.addEventListener('scroll', function() { |
| 70 | + if (window.pageYOffset > 300) { |
| 71 | + backToTopBtn.classList.add('show'); |
| 72 | + } else { |
| 73 | + backToTopBtn.classList.remove('show'); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + function scrollToTop() { |
| 79 | + window.scrollTo({ |
| 80 | + top: 0, |
| 81 | + behavior: 'smooth' |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // Floating Menu functionality |
| 86 | + function toggleFloatingMenu() { |
| 87 | + const menu = document.getElementById('floating-menu'); |
| 88 | + menu.classList.toggle('active'); |
| 89 | + } |
| 90 | + |
| 91 | + function openSearch() { |
| 92 | + const searchPage = document.querySelector('.search-page'); |
| 93 | + if (searchPage) { |
| 94 | + searchPage.classList.add('show'); |
| 95 | + const searchInput = document.getElementById('search-input'); |
| 96 | + if (searchInput) { |
| 97 | + setTimeout(() => searchInput.focus(), 300); |
| 98 | + } |
| 99 | + } |
| 100 | + toggleFloatingMenu(); |
| 101 | + } |
| 102 | + |
| 103 | + function toggleDarkMode() { |
| 104 | + document.body.classList.toggle('dark-mode'); |
| 105 | + localStorage.setItem('darkMode', document.body.classList.contains('dark-mode')); |
| 106 | + toggleFloatingMenu(); |
| 107 | + } |
| 108 | + |
| 109 | + function printPage() { |
| 110 | + window.print(); |
| 111 | + toggleFloatingMenu(); |
| 112 | + } |
| 113 | + |
| 114 | + // Social Share functionality |
| 115 | + function initSocialShare() { |
| 116 | + // Close search when clicking outside |
| 117 | + document.addEventListener('click', function(e) { |
| 118 | + const searchPage = document.querySelector('.search-page'); |
| 119 | + const floatingMenu = document.getElementById('floating-menu'); |
| 120 | + |
| 121 | + if (searchPage && searchPage.classList.contains('show') && |
| 122 | + !e.target.closest('.search-main') && !e.target.closest('.search-icon-close')) { |
| 123 | + searchPage.classList.remove('show'); |
| 124 | + } |
| 125 | + |
| 126 | + if (floatingMenu && floatingMenu.classList.contains('active') && |
| 127 | + !e.target.closest('.floating-menu')) { |
| 128 | + floatingMenu.classList.remove('active'); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + // Close search with close button |
| 133 | + const closeBtn = document.querySelector('.search-icon-close'); |
| 134 | + if (closeBtn) { |
| 135 | + closeBtn.addEventListener('click', function() { |
| 136 | + document.querySelector('.search-page').classList.remove('show'); |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + function shareToTwitter() { |
| 142 | + const url = encodeURIComponent(window.location.href); |
| 143 | + const text = encodeURIComponent(document.title); |
| 144 | + window.open(`https://twitter.com/intent/tweet?url=${url}&text=${text}`, '_blank', 'width=600,height=400'); |
| 145 | + } |
| 146 | + |
| 147 | + function shareToFacebook() { |
| 148 | + const url = encodeURIComponent(window.location.href); |
| 149 | + window.open(`https://www.facebook.com/sharer/sharer.php?u=${url}`, '_blank', 'width=600,height=400'); |
| 150 | + } |
| 151 | + |
| 152 | + function shareToLinkedIn() { |
| 153 | + const url = encodeURIComponent(window.location.href); |
| 154 | + const title = encodeURIComponent(document.title); |
| 155 | + window.open(`https://www.linkedin.com/sharing/share-offsite/?url=${url}&title=${title}`, '_blank', 'width=600,height=400'); |
| 156 | + } |
| 157 | + |
| 158 | + function shareToWechat() { |
| 159 | + // Copy URL to clipboard for WeChat sharing |
| 160 | + navigator.clipboard.writeText(window.location.href).then(function() { |
| 161 | + alert('链接已复制到剪贴板,请在微信中粘贴分享!'); |
| 162 | + }).catch(function() { |
| 163 | + prompt('请复制以下链接在微信中分享:', window.location.href); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + function shareToWeibo() { |
| 168 | + const url = encodeURIComponent(window.location.href); |
| 169 | + const title = encodeURIComponent(document.title); |
| 170 | + window.open(`https://service.weibo.com/share/share.php?url=${url}&title=${title}`, '_blank', 'width=600,height=400'); |
| 171 | + } |
| 172 | + |
| 173 | + // Lazy loading implementation |
| 174 | + function initLazyLoading() { |
| 175 | + const images = document.querySelectorAll('img[data-src]'); |
| 176 | + const imageObserver = new IntersectionObserver((entries, observer) => { |
| 177 | + entries.forEach(entry => { |
| 178 | + if (entry.isIntersecting) { |
| 179 | + const img = entry.target; |
| 180 | + img.src = img.dataset.src; |
| 181 | + img.classList.remove('lazy'); |
| 182 | + imageObserver.unobserve(img); |
| 183 | + } |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + images.forEach(img => imageObserver.observe(img)); |
| 188 | + } |
| 189 | +</script> |
32 | 190 |
|
33 | 191 | <!-- Service Worker --> |
34 | 192 | {% if site.service-worker %} |
|
112 | 270 | </script> |
113 | 271 |
|
114 | 272 |
|
115 | | -<!-- Google Analytics --> |
| 273 | +<!-- Google Analytics (GA4) --> |
116 | 274 | {% if site.ga_track_id %} |
| 275 | +<!-- Google tag (gtag.js) --> |
| 276 | +<script async src="https://www.googletagmanager.com/gtag/js?id={{ site.ga_track_id }}"></script> |
117 | 277 | <script> |
118 | | - // dynamic User by Jason |
119 | | - var _gaId = '{{ site.ga_track_id }}'; |
120 | | - var _gaDomain = '{{ site.ga_domain }}'; |
121 | | - |
122 | | - // Originial |
123 | | - (function (i, s, o, g, r, a, m) { |
124 | | - i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { |
125 | | - (i[r].q = i[r].q || []).push(arguments) |
126 | | - }, i[r].l = 1 * new Date(); a = s.createElement(o), |
127 | | - m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) |
128 | | - })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); |
129 | | - |
130 | | - ga('create', _gaId, _gaDomain); |
131 | | - ga('send', 'pageview'); |
| 278 | + window.dataLayer = window.dataLayer || []; |
| 279 | + function gtag(){dataLayer.push(arguments);} |
| 280 | + gtag('js', new Date()); |
| 281 | + |
| 282 | + gtag('config', '{{ site.ga_track_id }}', { |
| 283 | + 'page_title': '{% if page.title %}{{ page.title }}{% else %}{{ site.title }}{% endif %}', |
| 284 | + 'page_location': '{{ site.url }}{{ page.url }}' |
| 285 | + }); |
132 | 286 | </script> |
133 | 287 | {% endif %} |
134 | 288 |
|
|
0 commit comments