refactor(License): 使用内联脚本替换动态URL更新逻辑

移除原有的动态URL更新函数,改为直接在脚本中设置当前URL,简化实现并提高可靠性
This commit is contained in:
二叉树树
2025-11-29 06:55:36 +08:00
parent f94cc8319e
commit ab7e7f8d6a

View File

@@ -21,9 +21,18 @@ const postUrl = decodeURIComponent(Astro.url.toString());
<div class="transition font-bold text-black/75 dark:text-white/75"> <div class="transition font-bold text-black/75 dark:text-white/75">
{title} {title}
</div> </div>
<a href={postUrl} class="license-url link text-[var(--primary)] opacity-0 transition-opacity">
{postUrl} <a id="current-link" style="display:none;" class="link text-[var(--primary)]"></a>
</a>
<script>
const currentLink = document.getElementById('current-link');
const url = window.location.href;
currentLink.textContent = url; // 显示文本 = 当前 URL
currentLink.href = url; // 点击跳转 = 当前 URL
currentLink.style.display = 'inline'; // 显示出来
</script>
<div class="flex gap-6 mt-2"> <div class="flex gap-6 mt-2">
<div> <div>
<div class="transition text-black/30 dark:text-white/30 text-sm">作者</div> <div class="transition text-black/30 dark:text-white/30 text-sm">作者</div>
@@ -41,16 +50,4 @@ const postUrl = decodeURIComponent(Astro.url.toString());
<Icon name="fa6-brands:creative-commons" class="transition text-[15rem] absolute pointer-events-none right-6 top-1/2 -translate-y-1/2 text-black/5 dark:text-white/5"></Icon> <Icon name="fa6-brands:creative-commons" class="transition text-[15rem] absolute pointer-events-none right-6 top-1/2 -translate-y-1/2 text-black/5 dark:text-white/5"></Icon>
</div> </div>
<script>
function updateLicenseUrl() {
const links = document.querySelectorAll('.license-url');
links.forEach(link => {
const url = decodeURIComponent(window.location.href);
link.setAttribute('href', url);
link.textContent = url;
link.classList.remove('opacity-0');
});
}
updateLicenseUrl();
document.addEventListener('astro:page-load', updateLicenseUrl);
</script>