| ---
|
| import { commentConfig } from "@/config/commentConfig";
|
| import { url } from "@/utils/url-utils";
|
|
|
| interface Props {
|
| path: string;
|
| }
|
|
|
| const config = {
|
| ...commentConfig.twikoo,
|
| el: "#tcomment",
|
| path: Astro.props.path,
|
| };
|
| ---
|
|
|
| <div id="tcomment"></div>
|
|
|
| <script is:inline src={url("/assets/js/firefly-twikoo-1.6.44.all.min.js")}></script>
|
| <script is:inline define:vars={{ config }}>
|
|
|
| function getCurrentPath() {
|
| const pathname = window.location.pathname;
|
| return pathname.endsWith("/") && pathname.length > 1
|
| ? pathname.slice(0, -1)
|
| : pathname;
|
| }
|
|
|
|
|
| function createTwikooConfig() {
|
| return {
|
| ...config,
|
| path: getCurrentPath(),
|
| el: "#tcomment",
|
| };
|
| }
|
|
|
|
|
| function initTwikoo() {
|
| if (typeof twikoo !== "undefined") {
|
| const commentEl = document.getElementById("tcomment");
|
| if (commentEl) {
|
| commentEl.innerHTML = "";
|
|
|
| const dynamicConfig = createTwikooConfig();
|
| console.log("[Twikoo] 初始化配置:", dynamicConfig);
|
|
|
| twikoo
|
| .init(dynamicConfig)
|
| .then(() => {
|
| console.log("[Twikoo] 初始化完成");
|
| })
|
| .catch((error) => {
|
| console.error("[Twikoo] 初始化失败:", error);
|
| });
|
| }
|
| } else {
|
|
|
| setTimeout(initTwikoo, 500);
|
| }
|
| }
|
|
|
|
|
| document.addEventListener("DOMContentLoaded", initTwikoo);
|
|
|
|
|
| if (window.swup && window.swup.hooks) {
|
| window.swup.hooks.on("content:replace", function () {
|
| setTimeout(initTwikoo, 200);
|
| });
|
| } else {
|
| document.addEventListener("swup:enable", function () {
|
| if (window.swup && window.swup.hooks) {
|
| window.swup.hooks.on("content:replace", function () {
|
| setTimeout(initTwikoo, 200);
|
| });
|
| }
|
| });
|
| }
|
|
|
|
|
| document.addEventListener("firefly:page:loaded", function () {
|
| const commentEl = document.getElementById("tcomment");
|
| if (commentEl) {
|
| console.log("[Twikoo] 通过自定义事件重新初始化");
|
| initTwikoo();
|
| }
|
| });
|
| </script>
|
| |