删除google广告代码
太耗资源了!
This commit is contained in:
1 parent
1a86c10250
commit
1801c87cde
1 file changed
+103
-13
+103
-13
@@ -30,13 +30,19 @@ interface Props {
|
|||||||
|
|
||||||
let { title, banner, description, lang, setOGTypeArticle } = Astro.props;
|
let { title, banner, description, lang, setOGTypeArticle } = Astro.props;
|
||||||
|
|
||||||
|
// apply a class to the body element to decide the height of the banner, only used for initial page load
|
||||||
|
// Swup can update the body for each page visit, but it's after the page transition, causing a delay for banner height change
|
||||||
|
// so use Swup hooks instead to change the height immediately when a link is clicked
|
||||||
const isHomePage = pathsEqual(Astro.url.pathname, url("/"));
|
const isHomePage = pathsEqual(Astro.url.pathname, url("/"));
|
||||||
|
|
||||||
|
// defines global css variables
|
||||||
|
// why doing this in Layout instead of GlobalStyles: https://github.com/withastro/astro/issues/6728#issuecomment-1502203757
|
||||||
const configHue = siteConfig.themeColor.hue;
|
const configHue = siteConfig.themeColor.hue;
|
||||||
if (!banner || typeof banner !== "string" || banner.trim() === "") {
|
if (!banner || typeof banner !== "string" || banner.trim() === "") {
|
||||||
banner = siteConfig.banner.src;
|
banner = siteConfig.banner.src;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO don't use post cover as banner for now
|
||||||
banner = siteConfig.banner.src;
|
banner = siteConfig.banner.src;
|
||||||
|
|
||||||
const enableBanner = siteConfig.banner.enable;
|
const enableBanner = siteConfig.banner.enable;
|
||||||
@@ -51,6 +57,7 @@ if (title) {
|
|||||||
const favicons: Favicon[] =
|
const favicons: Favicon[] =
|
||||||
siteConfig.favicon.length > 0 ? siteConfig.favicon : defaultFavicons;
|
siteConfig.favicon.length > 0 ? siteConfig.favicon : defaultFavicons;
|
||||||
|
|
||||||
|
// const siteLang = siteConfig.lang.replace('_', '-')
|
||||||
if (!lang) {
|
if (!lang) {
|
||||||
lang = `${siteConfig.lang}`;
|
lang = `${siteConfig.lang}`;
|
||||||
}
|
}
|
||||||
@@ -102,7 +109,9 @@ const bannerOffset =
|
|||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
<!-- Set the theme before the page is rendered to avoid a flash -->
|
||||||
<script is:inline define:vars={{DEFAULT_THEME, LIGHT_MODE, DARK_MODE, AUTO_MODE, BANNER_HEIGHT_EXTEND, PAGE_WIDTH, configHue}}>
|
<script is:inline define:vars={{DEFAULT_THEME, LIGHT_MODE, DARK_MODE, AUTO_MODE, BANNER_HEIGHT_EXTEND, PAGE_WIDTH, configHue}}>
|
||||||
|
// Load the theme from local storage
|
||||||
const theme = localStorage.getItem('theme') || DEFAULT_THEME;
|
const theme = localStorage.getItem('theme') || DEFAULT_THEME;
|
||||||
switch (theme) {
|
switch (theme) {
|
||||||
case LIGHT_MODE:
|
case LIGHT_MODE:
|
||||||
@@ -119,9 +128,11 @@ const bannerOffset =
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load the hue from local storage
|
||||||
const hue = localStorage.getItem('hue') || configHue;
|
const hue = localStorage.getItem('hue') || configHue;
|
||||||
document.documentElement.style.setProperty('--hue', hue);
|
document.documentElement.style.setProperty('--hue', hue);
|
||||||
|
|
||||||
|
// calculate the --banner-height-extend, which needs to be a multiple of 4 to avoid blurry text
|
||||||
let offset = Math.floor(window.innerHeight * (BANNER_HEIGHT_EXTEND / 100));
|
let offset = Math.floor(window.innerHeight * (BANNER_HEIGHT_EXTEND / 100));
|
||||||
offset = offset - offset % 4;
|
offset = offset - offset % 4;
|
||||||
document.documentElement.style.setProperty('--banner-height-extend', `${offset}px`);
|
document.documentElement.style.setProperty('--banner-height-extend', `${offset}px`);
|
||||||
@@ -129,10 +140,12 @@ const bannerOffset =
|
|||||||
<style define:vars={{
|
<style define:vars={{
|
||||||
configHue,
|
configHue,
|
||||||
'page-width': `${PAGE_WIDTH}rem`,
|
'page-width': `${PAGE_WIDTH}rem`,
|
||||||
}}></style>
|
}}></style> <!-- defines global css variables. This will be applied to <html> <body> and some other elements idk why -->
|
||||||
|
|
||||||
|
<!-- Umami Analytics -->
|
||||||
<script defer src="https://umami.mckero.com/script.js" data-website-id="a4ba198a-25a0-4631-84e1-e3b62e418c87"></script>
|
<script defer src="https://umami.mckero.com/script.js" data-website-id="a4ba198a-25a0-4631-84e1-e3b62e418c87"></script>
|
||||||
|
|
||||||
|
<!-- Google tag (gtag.js) -->
|
||||||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-G9W2J9J4HV"></script>
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-G9W2J9J4HV"></script>
|
||||||
<script>
|
<script>
|
||||||
window.dataLayer = window.dataLayer || [];
|
window.dataLayer = window.dataLayer || [];
|
||||||
@@ -141,9 +154,6 @@ const bannerOffset =
|
|||||||
gtag('config', 'G-G9W2J9J4HV');
|
gtag('config', 'G-G9W2J9J4HV');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script defer src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9638627737028409"
|
|
||||||
crossorigin="anonymous"></script>
|
|
||||||
|
|
||||||
<link rel="alternate" type="application/rss+xml" title={profileConfig.name} href={`${Astro.site}rss.xml`}/>
|
<link rel="alternate" type="application/rss+xml" title={profileConfig.name} href={`${Astro.site}rss.xml`}/>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
@@ -153,6 +163,7 @@ const bannerOffset =
|
|||||||
<ConfigCarrier></ConfigCarrier>
|
<ConfigCarrier></ConfigCarrier>
|
||||||
<slot />
|
<slot />
|
||||||
|
|
||||||
|
<!-- increase the page height during page transition to prevent the scrolling animation from jumping -->
|
||||||
<div id="page-height-extend" class="hidden h-[300vh]"></div>
|
<div id="page-height-extend" class="hidden h-[300vh]"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -196,6 +207,9 @@ const bannerOffset =
|
|||||||
import 'overlayscrollbars/overlayscrollbars.css';
|
import 'overlayscrollbars/overlayscrollbars.css';
|
||||||
import {
|
import {
|
||||||
OverlayScrollbars,
|
OverlayScrollbars,
|
||||||
|
// ScrollbarsHidingPlugin,
|
||||||
|
// SizeObserverPlugin,
|
||||||
|
// ClickScrollPlugin
|
||||||
} from 'overlayscrollbars';
|
} from 'overlayscrollbars';
|
||||||
import {getHue, getStoredTheme, setHue, setTheme} from "../utils/setting-utils";
|
import {getHue, getStoredTheme, setHue, setTheme} from "../utils/setting-utils";
|
||||||
import {pathsEqual, url} from "../utils/url-utils";
|
import {pathsEqual, url} from "../utils/url-utils";
|
||||||
@@ -207,13 +221,57 @@ import {
|
|||||||
} from "../constants/constants";
|
} from "../constants/constants";
|
||||||
import { siteConfig } from '../config';
|
import { siteConfig } from '../config';
|
||||||
|
|
||||||
|
/* Preload fonts */
|
||||||
|
// (async function() {
|
||||||
|
// try {
|
||||||
|
// await Promise.all([
|
||||||
|
// document.fonts.load("400 1em Roboto"),
|
||||||
|
// document.fonts.load("700 1em Roboto"),
|
||||||
|
// ]);
|
||||||
|
// document.body.classList.remove("hidden");
|
||||||
|
// } catch (error) {
|
||||||
|
// console.log("Failed to load fonts:", error);
|
||||||
|
// }
|
||||||
|
// })();
|
||||||
|
|
||||||
|
/* TODO This is a temporary solution for style flicker issue when the transition is activated */
|
||||||
|
/* issue link: https://github.com/withastro/astro/issues/8711, the solution get from here too */
|
||||||
|
/* update: fixed in Astro 3.2.4 */
|
||||||
|
/*
|
||||||
|
function disableAnimation() {
|
||||||
|
const css = document.createElement('style')
|
||||||
|
css.appendChild(
|
||||||
|
document.createTextNode(
|
||||||
|
`*{
|
||||||
|
-webkit-transition:none!important;
|
||||||
|
-moz-transition:none!important;
|
||||||
|
-o-transition:none!important;
|
||||||
|
-ms-transition:none!important;
|
||||||
|
transition:none!important
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
document.head.appendChild(css)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
// Force restyle
|
||||||
|
;(() => window.getComputedStyle(document.body))()
|
||||||
|
|
||||||
|
// Wait for next tick before removing
|
||||||
|
setTimeout(() => {
|
||||||
|
document.head.removeChild(css)
|
||||||
|
}, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
const bannerEnabled = !!document.getElementById('banner-wrapper')
|
const bannerEnabled = !!document.getElementById('banner-wrapper')
|
||||||
|
|
||||||
function setClickOutsideToClose(panel: string, ignores: string[]) {
|
function setClickOutsideToClose(panel: string, ignores: string[]) {
|
||||||
document.addEventListener("click", event => {
|
document.addEventListener("click", event => {
|
||||||
let panelDom = document.getElementById(panel);
|
let panelDom = document.getElementById(panel);
|
||||||
let tDom = event.target;
|
let tDom = event.target;
|
||||||
if (!(tDom instanceof Node)) return;
|
if (!(tDom instanceof Node)) return; // Ensure the event target is an HTML Node
|
||||||
for (let ig of ignores) {
|
for (let ig of ignores) {
|
||||||
let ie = document.getElementById(ig)
|
let ie = document.getElementById(ig)
|
||||||
if (ie == tDom || (ie?.contains(tDom))) {
|
if (ie == tDom || (ie?.contains(tDom))) {
|
||||||
@@ -241,10 +299,12 @@ function initCustomScrollbar() {
|
|||||||
const bodyElement = document.querySelector('body');
|
const bodyElement = document.querySelector('body');
|
||||||
if (!bodyElement) return;
|
if (!bodyElement) return;
|
||||||
OverlayScrollbars(
|
OverlayScrollbars(
|
||||||
|
// docs say that a initialization to the body element would affect native functionality like window.scrollTo
|
||||||
|
// but just leave it here for now
|
||||||
{
|
{
|
||||||
target: bodyElement,
|
target: bodyElement,
|
||||||
cancel: {
|
cancel: {
|
||||||
nativeScrollbarsOverlaid: true,
|
nativeScrollbarsOverlaid: true, // don't initialize the overlay scrollbar if there is a native one
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
scrollbars: {
|
scrollbars: {
|
||||||
@@ -312,25 +372,48 @@ function showBanner() {
|
|||||||
banner.classList.remove('opacity-0', 'scale-105');
|
banner.classList.remove('opacity-0', 'scale-105');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 专用于创建loadComment的事件,即通知评论组件进行一次加载
|
||||||
|
|
||||||
function initCommentComponent() {
|
function initCommentComponent() {
|
||||||
|
|
||||||
const event = new Event("loadComment");
|
const event = new Event("loadComment");
|
||||||
|
|
||||||
document.dispatchEvent(event);
|
document.dispatchEvent(event);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// twikoo评论
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
// disableAnimation()() // TODO
|
||||||
loadTheme();
|
loadTheme();
|
||||||
loadHue();
|
loadHue();
|
||||||
initCustomScrollbar();
|
initCustomScrollbar();
|
||||||
showBanner();
|
showBanner();
|
||||||
initCommentComponent();
|
initCommentComponent(); // 调用initCommentComponent()函数,twikoo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Load settings when entering the site */
|
||||||
init();
|
init();
|
||||||
|
|
||||||
const setup = () => {
|
const setup = () => {
|
||||||
|
// TODO: temp solution to change the height of the banner
|
||||||
|
/*
|
||||||
|
window.swup.hooks.on('animation:out:start', () => {
|
||||||
|
const path = window.location.pathname
|
||||||
|
const body = document.querySelector('body')
|
||||||
|
if (path[path.length - 1] === '/' && !body.classList.contains('is-home')) {
|
||||||
|
body.classList.add('is-home')
|
||||||
|
} else if (path[path.length - 1] !== '/' && body.classList.contains('is-home')) {
|
||||||
|
body.classList.remove('is-home')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
*/
|
||||||
window.swup.hooks.on('link:click', () => {
|
window.swup.hooks.on('link:click', () => {
|
||||||
|
// Remove the delay for the first time page load
|
||||||
document.documentElement.style.setProperty('--content-delay', '0ms')
|
document.documentElement.style.setProperty('--content-delay', '0ms')
|
||||||
|
|
||||||
|
// prevent elements from overlapping the navbar
|
||||||
if (!bannerEnabled) {
|
if (!bannerEnabled) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -343,13 +426,17 @@ const setup = () => {
|
|||||||
navbar.classList.add('navbar-hidden')
|
navbar.classList.add('navbar-hidden')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// window.swup.hooks.on('content:replace', initCustomScrollbar)
|
||||||
|
|
||||||
window.swup.hooks.on("content:replace", () => {
|
window.swup.hooks.on("content:replace", () => {
|
||||||
|
|
||||||
initCustomScrollbar();
|
initCustomScrollbar();
|
||||||
initCommentComponent();
|
|
||||||
|
initCommentComponent(); // 添加代码调用
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.swup.hooks.on('visit:start', (visit: {to: {url: string}}) => {
|
window.swup.hooks.on('visit:start', (visit: {to: {url: string}}) => {
|
||||||
|
// change banner height immediately when a link is clicked
|
||||||
const bodyElement = document.querySelector('body')
|
const bodyElement = document.querySelector('body')
|
||||||
if (pathsEqual(visit.to.url, url('/'))) {
|
if (pathsEqual(visit.to.url, url('/'))) {
|
||||||
bodyElement!.classList.add('lg:is-home');
|
bodyElement!.classList.add('lg:is-home');
|
||||||
@@ -357,24 +444,25 @@ const setup = () => {
|
|||||||
bodyElement!.classList.remove('lg:is-home');
|
bodyElement!.classList.remove('lg:is-home');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// increase the page height during page transition to prevent the scrolling animation from jumping
|
||||||
const heightExtend = document.getElementById('page-height-extend')
|
const heightExtend = document.getElementById('page-height-extend')
|
||||||
if (heightExtend) {
|
if (heightExtend) {
|
||||||
heightExtend.classList.remove('hidden')
|
heightExtend.classList.remove('hidden')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hide the TOC while scrolling back to top
|
||||||
let toc = document.getElementById('toc-wrapper');
|
let toc = document.getElementById('toc-wrapper');
|
||||||
if (toc) {
|
if (toc) {
|
||||||
toc.classList.add('toc-not-ready')
|
toc.classList.add('toc-not-ready')
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
window.swup.hooks.on('page:view', () => {
|
window.swup.hooks.on('page:view', () => {
|
||||||
|
// hide the temp high element when the transition is done
|
||||||
const heightExtend = document.getElementById('page-height-extend')
|
const heightExtend = document.getElementById('page-height-extend')
|
||||||
if (heightExtend) {
|
if (heightExtend) {
|
||||||
heightExtend.classList.remove('hidden')
|
heightExtend.classList.remove('hidden')
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
window.swup.hooks.on('visit:end', (_visit: {to: {url: string}}) => {
|
window.swup.hooks.on('visit:end', (_visit: {to: {url: string}}) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const heightExtend = document.getElementById('page-height-extend')
|
const heightExtend = document.getElementById('page-height-extend')
|
||||||
@@ -382,6 +470,7 @@ const setup = () => {
|
|||||||
heightExtend.classList.add('hidden')
|
heightExtend.classList.add('hidden')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Just make the transition looks better
|
||||||
const toc = document.getElementById('toc-wrapper');
|
const toc = document.getElementById('toc-wrapper');
|
||||||
if (toc) {
|
if (toc) {
|
||||||
toc.classList.remove('toc-not-ready')
|
toc.classList.remove('toc-not-ready')
|
||||||
@@ -420,7 +509,7 @@ function scrollFunction() {
|
|||||||
if (!bannerEnabled) return
|
if (!bannerEnabled) return
|
||||||
if (navbar) {
|
if (navbar) {
|
||||||
const NAVBAR_HEIGHT = 72
|
const NAVBAR_HEIGHT = 72
|
||||||
const MAIN_PANEL_EXCESS_HEIGHT = MAIN_PANEL_OVERLAPS_BANNER_HEIGHT * 16
|
const MAIN_PANEL_EXCESS_HEIGHT = MAIN_PANEL_OVERLAPS_BANNER_HEIGHT * 16 // The height the main panel overlaps the banner
|
||||||
|
|
||||||
let bannerHeight = BANNER_HEIGHT
|
let bannerHeight = BANNER_HEIGHT
|
||||||
if (document.body.classList.contains('lg:is-home') && window.innerWidth >= 1024) {
|
if (document.body.classList.contains('lg:is-home') && window.innerWidth >= 1024) {
|
||||||
@@ -437,6 +526,7 @@ function scrollFunction() {
|
|||||||
window.onscroll = scrollFunction
|
window.onscroll = scrollFunction
|
||||||
|
|
||||||
window.onresize = () => {
|
window.onresize = () => {
|
||||||
|
// calculate the --banner-height-extend, which needs to be a multiple of 4 to avoid blurry text
|
||||||
let offset = Math.floor(window.innerHeight * (BANNER_HEIGHT_EXTEND / 100));
|
let offset = Math.floor(window.innerHeight * (BANNER_HEIGHT_EXTEND / 100));
|
||||||
offset = offset - offset % 4;
|
offset = offset - offset % 4;
|
||||||
document.documentElement.style.setProperty('--banner-height-extend', `${offset}px`);
|
document.documentElement.style.setProperty('--banner-height-extend', `${offset}px`);
|
||||||
@@ -504,4 +594,4 @@ if (window.swup) {
|
|||||||
} else {
|
} else {
|
||||||
document.addEventListener("swup:enable", setup)
|
document.addEventListener("swup:enable", setup)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
Reference in new issue
Block a user