// 根据窗口宽度设置广告栏高度
let adBarHeight = window?.innerWidth <= 768 ? 48 : 64;
const adBarHeightPX = `${adBarHeight}px`;
// 当DOM加载完成时执行
document.addEventListener("DOMContentLoaded", function () {
// 获取当前页面路径
const currentPath = window.location.pathname;
// 判断是否为广告页面
const isAdPage = currentPath.endsWith("/ad.html");
// 获取URL对象
const url = new URL(window.location.href);
// 分割路径
const pathSegments = url.pathname.split("/");
// 确定语言
const language = pathSegments.includes("zh")
? "zh"
: pathSegments.includes("en")
? "en"
: "zh";
if (isAdPage) {
// 如果是广告页面,设置导航背景色并获取广告计划和介绍
setNavBackgroundColor();
fetchAdBanner(language);
fetchAdPlan(language);
fetchAdIntro(language);
manageAnimate();
handleDialog();
} else {
// 如果不是广告页面,插入广告样式表并获取广告栏
insertAdStylesheet();
fetchAdBar(language);
}
});
// 初始化广告栏
function initializeAdBar() {
displayAdBar();
adjustNavPosition("down");
// 添加关闭广告栏的事件监听器
const closeAdBarButton = document.getElementById("ad-close");
closeAdBarButton.addEventListener("click", () => {
hideAdBar();
adjustNavPosition("up");
});
}
// 显示广告栏
function displayAdBar() {
const adWrapper = document.getElementById("ad");
if (!adWrapper) return;
adWrapper.style.height = adBarHeightPX;
adWrapper.style.display = "block";
// 调整头部元素的边距
const headerEl = document.getElementsByTagName("header")[0];
if (!headerEl) return;
headerEl.style.marginTop = adBarHeightPX;
}
// 隐藏广告栏
function hideAdBar() {
const adWrapper = document.getElementById("ad");
if (!adWrapper) return;
adWrapper.style.height = "0px";
adWrapper.style.display = "none";
// 重置头部元素的边距
const headerEl = document.getElementsByTagName("header")[0];
if (!headerEl) return;
headerEl.style.marginTop = "0px";
}
// 调整导航栏位置
function adjustNavPosition(direction) {
const navWrapper = document.getElementsByClassName("nav")[0];
navWrapper.style.top = direction === "down" ? adBarHeightPX : "0";
}
// 获取广告栏数据
function fetchAdBar(language) {
const apiUrl = `https://cms.hitosea.com/api/doo-task-ad-bar?locale=${language}&populate[0]=background`;
fetchData(apiUrl)
.then(({ data: { attributes } }) => {
updateAdBar(attributes);
})
.catch(handleError);
}
// 获取广告banner数据
function fetchAdBanner(language) {
const query = window.Qs.stringify(
{
locale: language,
populate: {
title: {
populate: "*",
},
description: {
populate: "*",
},
background: {
populate: "*",
},
underline: {
populate: "*",
},
signUpButton: {
populate: "*",
},
selfHostButton: {
populate: "*",
},
localizations: {
populate: "*",
},
},
},
{
encodeValuesOnly: true,
}
);
const apiUrl = `https://cms.hitosea.com/api/doo-task-ad-banner?${query}`;
fetchData(apiUrl).then(handleAdBannerResponse).catch(handleError);
}
// 获取广告计划数据
function fetchAdPlan(language) {
const query = window.Qs.stringify({
locale: language,
populate: {
plans: {
populate: {
price: {
populate: "*",
},
button: {
populate: "*",
},
features: {
populate: "*",
},
},
},
},
});
const apiUrl = `https://cms.hitosea.com/api/doo-task-ad-plan?${query}`;
fetchData(apiUrl).then(handleAdPlanResponse).catch(handleError);
}
// 获取广告介绍数据
function fetchAdIntro(language) {
const query = window.Qs.stringify({
locale: language,
populate: {
intros: {
populate: {
bar: {
populate: "*",
},
cover: {
populate: "*",
},
title: {
populate: "*",
},
description: {
populate: "*",
},
},
},
},
});
const apiUrl = `https://cms.hitosea.com/api/doo-task-ad-intro?${query}`;
fetchData(apiUrl).then(handleAdIntroResponse).catch(handleError);
}
// 通用数据获取函数
function fetchData(url) {
return fetch(url).then((response) => response.json());
}
function getMediaUrl(media) {
if (!media?.data?.attributes?.url) {
return "";
}
return `https://cms.hitosea.com${media.data.attributes.url}`;
}
function getStyle(style) {
if (!style) return null;
return Object.keys(style)
.map((key) => `${key}: ${style[key]}`)
.join("; ");
}
// 处理广告banner响应
function handleAdBannerResponse(response) {
// 在此实现广告banner处理逻辑
try {
const {
data: {
attributes: {
title,
description,
background,
underline,
signUpButton,
selfHostButton,
},
},
} = response;
handleAdBannerTitle(title);
handleAdBannerDescription(description);
handleAdBannerBackground(background);
handleAdBannerUnderline(underline);
handleAdBannerSignUpButton(signUpButton);
handleAdBannerSelfHostButton(selfHostButton);
} catch (error) {
console.error("处理广告banner响应时出错:", error);
}
}
function handleAdBannerTitle(title) {
const titleText = {};
if (Array.isArray(title)) {
title.forEach((item) => {
titleText[item.key] = { text: item.text, style: item.style };
});
}
const titlePart1El = document.getElementById("ad-banner-title-part1");
const titlePart2El = document.getElementById("ad-banner-title-part2");
const titlePart3El = document.getElementById("ad-banner-title-part3");
if (titlePart1El && titleText["part1"]) {
titlePart1El.textContent = titleText["part1"].text;
titlePart1El.style = getStyle(titleText["part1"].style);
}
if (titlePart2El && titleText["part2"]) {
titlePart2El.textContent = titleText["part2"].text;
titlePart2El.style = getStyle(titleText["part2"].style);
}
if (titlePart3El && titleText["part3"]) {
titlePart3El.textContent = titleText["part3"].text;
titlePart3El.style = getStyle(titleText["part3"].style);
}
}
function handleAdBannerDescription(description) {
const descriptionText = {
text: description.text,
style: description.style,
};
const descriptionEl = document.getElementById("ad-banner-description");
if (descriptionEl && descriptionText.text) {
descriptionEl.textContent = descriptionText.text;
descriptionEl.style = getStyle(descriptionText.style);
}
}
function handleAdBannerBackground(background) {
const backgroundUrl = getMediaUrl(background);
const adBannerEl = document.getElementById("ad-banner");
if (adBannerEl && backgroundUrl) {
adBannerEl.style.backgroundImage = `url(${backgroundUrl})`;
}
}
function handleAdBannerUnderline(underline) {
const underlineUrl = getMediaUrl(underline);
const adBannerTitleUnderlineEl = document.getElementById(
"ad-banner-title-underline"
);
if (adBannerTitleUnderlineEl && underlineUrl) {
adBannerTitleUnderlineEl.innerHTML = ``;
}
}
function handleAdBannerSignUpButton({
theme,
style,
link: { label, href, target, slug },
}) {
const signUpButtonEl = document.getElementById("ad-banner-sign-up-button");
if (signUpButtonEl) {
signUpButtonEl.innerHTML = `
`;
signUpButtonEl.style = getStyle(style);
}
}
function handleAdBannerSelfHostButton({
theme,
style,
link: { label, href, target, slug },
}) {
const selfHostButtonEl = document.getElementById(
"ad-banner-self-host-button"
);
if (selfHostButtonEl) {
if (href === "#") {
selfHostButtonEl.innerHTML = `
`;
function showDialog(e) {
e.preventDefault();
e.stopPropagation();
const dialogEl = document.querySelector(".ad-dialog");
if (!dialogEl) return;
dialogEl.classList.add("show");
lockBodyScroll(true);
handleDialogAnimate(true)
}
selfHostButtonEl.addEventListener("click", showDialog);
} else {
selfHostButtonEl.innerHTML = `
`;
}
selfHostButtonEl.style = getStyle(style);
}
}
// 处理广告计划响应
function handleAdPlanResponse(response) {
// 在此实现广告计划处理逻辑
try {
const {
data: {
attributes: { title, description, plans },
},
} = response;
handleAdPlanTitle(title);
handleAdPlanDescription(description);
handleAdPlanPlans(plans);
} catch (error) {
console.error("处理广告计划响应时出错:", error);
}
}
function handleAdPlanTitle(title) {
const planTitleEl = document.getElementById("ad-plan-title");
if (planTitleEl && title) {
planTitleEl.textContent = title;
}
}
function handleAdPlanDescription(description) {
const planDescriptionEl = document.getElementById("ad-plan-description");
if (planDescriptionEl && description) {
planDescriptionEl.textContent = description;
}
}
async function handleAdPlanPlans(plans) {
const planContentEl = document.getElementById("ad-plan-content");
if (planContentEl && Array.isArray(plans)) {
const prevPlanItems = planContentEl.querySelectorAll(".plan-item");
for (const prevPlanItem of prevPlanItems) {
prevPlanItem.classList.add(
"animate__animated",
"animate__backOutDown"
);
prevPlanItem.addEventListener(
"animationend",
() => {
prevPlanItem.remove();
},
{ once: true }
);
}
plans.sort((a, b) => a.priority - b.priority);
for (const plan of plans) {
const planItemEl = document.createElement("div");
planItemEl.className = `plan-item ${plan.activated ? "active" : ""
}`;
planItemEl.innerHTML = `
${plan.tag ? `
最新活动