張惠妹金句語錄:「相信愛會得到應有的尊重和該有的平等對待。」
張惠妹金句語錄 3.
「我想要給自己一些自信,那個自信是自己給自己的。」
`;
footer.parentNode.insertBefore(emergencyBuffer, footer);
// 5秒後移除緊急緩衝(如果還存在)
setTimeout(function() {
if (emergencyBuffer.parentNode) {
emergencyBuffer.style.opacity = '0';
setTimeout(function() {
if (emergencyBuffer.parentNode) {
emergencyBuffer.parentNode.removeChild(emergencyBuffer);
}
}, 300);
}
footerEmergencyProtection = false;
}, 5000);
}
}
}
}, 50);
// 瀑布流載入檢測
if (waterfallConfig.enabled && !isLoading) {
var scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
var windowHeight = window.innerHeight;
var documentHeight = document.documentElement.scrollHeight;
var scrollPercentage = (scrollPosition + windowHeight) / documentHeight;
var scrollThreshold = 1 - (waterfallConfig.triggerDistance / documentHeight);
// 檢查載入條件
if (scrollPercentage >= scrollThreshold) {
// 檢查載入限制 - 使用一致的計數方式
var maxLoadedArticles = waterfallConfig.maxArticles + 1; // +1 for original article
var currentPageNumber = loadedArticleIds.length; // 目前要載入的頁數
console.log('瀑布流載入檢查:', {
已載入文章數: loadedArticleIds.length,
最大文章數: maxLoadedArticles,
目前頁數: currentPageNumber,
最大頁數: waterfallConfig.maxPages,
滾動百分比: Math.round(scrollPercentage * 100) + '%'
});
if (loadedArticleIds.length >= maxLoadedArticles) {
console.log('已達瀑布流文章數量上限:', maxLoadedArticles);
return;
} else if (currentPageNumber >= waterfallConfig.maxPages) {
console.log('已達瀑布流頁數上限:', waterfallConfig.maxPages, '目前頁數:', currentPageNumber);
return;
}
// 檢查是否有文章正在載入中,防止重疊
if (!canLoadNextWaterfallArticle()) {
console.log('前一篇文章尚未載入完成,延後觸發瀑布流');
return;
}
isLoading = true;
console.log('觸發瀑布流載入,頁面:', currentPageNumber, '已載入文章:', loadedArticleIds.length);
// 顯示載入指示器
showLastWaterfallLoadingIndicator();
// 延遲載入以改善使用體驗
setTimeout(function() {
getData.getArticle();
}, waterfallConfig.loadingDelay);
}
}
});
// 初始化檢查
console.log('瀑布流 JavaScript 載入完成');
// 檢查 DOM 是否準備就緒
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
console.log('瀑布流:DOMContentLoaded 事件觸發');
var relatedWrapCheck = document.querySelector('div.relatedWrap');
if (relatedWrapCheck) {
console.log('瀑布流:relatedWrap 元素檢查通過');
} else {
console.error('瀑布流:找不到 relatedWrap 元素');
}
});
} else {
console.log('瀑布流:DOM 已經載入完成');
var relatedWrapCheck = document.querySelector('div.relatedWrap');
if (relatedWrapCheck) {
console.log('瀑布流:relatedWrap 元素檢查通過');
} else {
console.error('瀑布流:找不到 relatedWrap 元素');
}
}
// 瀑布流文章載入狀態管理
var waterfallArticleStates = {
loading: new Set(), // 正在載入的文章ID
loaded: new Set() // 已完全載入的文章ID
};
// 標記文章載入完成
function markWaterfallArticleLoaded(articleId) {
waterfallArticleStates.loading.delete(articleId);
waterfallArticleStates.loaded.add(articleId);
console.log('瀑布流文章載入完成:', articleId, '已載入文章數:', waterfallArticleStates.loaded.size);
// 觸發自定義事件通知其他組件
var event = new CustomEvent('waterfallArticleLoaded', {
detail: { articleId: articleId, totalLoaded: waterfallArticleStates.loaded.size }
});
document.dispatchEvent(event);
}
// 檢查是否可以載入下一篇文章
function canLoadNextWaterfallArticle() {
// 如果有文章正在載入,就不能載入下一篇
if (waterfallArticleStates.loading.size > 0) {
console.log('有文章正在載入中,暫緩載入下一篇,載入中:', Array.from(waterfallArticleStates.loading));
return false;
}
return true;
}
var waterfallAdCounters = {};
function getWaterfallAdDivId(adElement, adKey, articleId, adDevice) {
var existingId = adElement.getAttribute('data-ad-div-id');
if (existingId) {
return existingId;
}
var counterKey = adKey + '_' + articleId + '_' + adDevice;
if (!waterfallAdCounters[counterKey]) {
waterfallAdCounters[counterKey] = 1;
} else {
waterfallAdCounters[counterKey] += 1;
}
var generatedId = adKey + '-' + articleId + '-' + adDevice + '-' + waterfallAdCounters[counterKey] + '-' + Math.floor(Math.random() * 1000);
adElement.setAttribute('data-ad-div-id', generatedId);
return generatedId;
}
function initWaterfallAds(articleId, adSlotStatus, container) {
if (!adSlotStatus || typeof googletag === 'undefined') {
return;
}
// 查找容器內的所有廣告元素(包含 ad-key 和 ad-internal)
var adElements = container.querySelectorAll('[ad-key], [ad-internal]');
adElements.forEach(function(adElement) {
if (adElement.getAttribute('data-waterfall-initialized') === '1') {
return;
}
var adKey = adElement.getAttribute('ad-key') || adElement.getAttribute('ad-internal');
var adDevice = adElement.getAttribute('ad-device');
// 如果沒有 ad-device 屬性,根據元素的 class 判斷
if (!adDevice) {
if (adElement.classList.contains('desktop')) {
adDevice = 'desktop';
} else if (adElement.classList.contains('mobile')) {
adDevice = 'mobile';
} else {
// 如果沒有明確的 class,根據螢幕大小判斷
adDevice = window.innerWidth > 768 ? 'desktop' : 'mobile';
}
}
adElement.setAttribute('data-waterfall-device', adDevice);
var adDiv = adElement.querySelector('[id^="div-gpt-ad"]');
// 如果沒有 div-gpt-ad,嘗試查找其他 div 元素
if (!adDiv) {
adDiv = adElement.querySelector('div[id]');
}
if (!adDiv) {
var generatedId = getWaterfallAdDivId(adElement, adKey, articleId, adDevice);
adDiv = document.createElement('div');
adDiv.id = generatedId;
adDiv.style.minWidth = '300px';
adDiv.style.minHeight = '250px';
adDiv.setAttribute('data-generated', '1');
adElement.appendChild(adDiv);
} else {
adElement.setAttribute('data-ad-div-id', adDiv.id);
}
if (!adDiv) {
// 即使沒有 div 容器,對於 content 廣告也要觸發背景圖檢查
if (adKey && adKey.indexOf('content_') === 0) {
if (typeof setupAdMonitoring === 'function') {
setupAdMonitoring(adElement);
}
}
return;
}
var adDivId = adDiv.id;
// 對於所有廣告容器,都觸發背景圖檢查
if (typeof setupAdMonitoring === 'function') {
setupAdMonitoring(adElement);
}
// 取得對應的廣告設定
var adConfig = adSlotStatus[adKey] && adSlotStatus[adKey][adDevice];
if (!adConfig) {
console.log('瀑布流廣告缺少配置:', adKey, adDevice, articleId);
return;
}
adElement.setAttribute('data-waterfall-initialized', '1');
// 廣告顯示邏輯
setTimeout(function() {
showWaterfallAd(adConfig, adDivId, adKey, articleId, adElement, adDevice);
}, 300);
});
}
function showWaterfallAd(ad, adDivId, adKey, articleId, adElement, adDevice) {
if (!ad) {
console.log('廣告配置為空:', adKey);
return;
}
try {
var adAdunit, adAdsize, adCampaign, adIframeSrc = '';
// 處理廣告資源配置
if (Array.isArray(ad.campaignResource)) {
var adRand = Math.floor(Math.random() * ad.campaignResource.length);
adAdunit = ad.campaignResource[adRand].datas.adunitKey;
adAdsize = ad.campaignResource[adRand].datas.dimensions;
adCampaign = ad.campaignResource[adRand].name;
adIframeSrc = ad.campaignResource[adRand].datas?.iframeSrc || '';
console.log('隨機廣告選擇:', adRand);
} else if (ad.campaignResource) {
adAdunit = ad.campaignResource.datas.adunitKey;
adAdsize = ad.campaignResource.datas.dimensions;
adCampaign = ad.campaignResource.name;
adIframeSrc = ad.campaignResource.datas?.iframeSrc || '';
}
// 設定廣告活動資訊
if (adElement) {
if (adCampaign) {
adElement.setAttribute('data-campaign', adCampaign);
}
adElement.setAttribute('data-waterfall-device', adDevice || '');
}
// 初始化 Google Publisher Tag
if (adAdunit && adAdsize && typeof googletag !== 'undefined') {
googletag.cmd.push(function() {
// 檢查是否已經定義過這個廣告位,避免重複定義
var existingSlots = googletag.pubads().getSlots();
var slotExists = existingSlots.some(function(slot) {
return slot.getSlotElementId() === adDivId;
});
if (slotExists) {
console.log('廣告位已存在,重新顯示:', adDivId);
googletag.display(adDivId);
return;
}
// 定義廣告位
var slot = googletag.defineSlot(adAdunit, adAdsize, adDivId);
if (slot) {
slot.addService(googletag.pubads());
// 設定目標定位
googletag.pubads().setTargeting("category", "音樂");
googletag.pubads().setTargeting("juksy_category", "音樂");
googletag.pubads().setTargeting("juksy_subcategory", "音樂");
googletag.pubads().setTargeting("article_id", articleId);
console.log('瀑布流廣告位已定義:', adDivId, adAdunit);
// 延遲顯示廣告
setTimeout(function() {
googletag.display(adDivId);
console.log('瀑布流廣告已顯示:', adDivId, adAdunit, adAdsize);
// 廣告載入後重新計算容器高度
setTimeout(function() {
recalculateWaterfallContainerHeight(articleId);
}, 1000); // 等待廣告完全載入
}, 300);
}
});
} else if (adIframeSrc) {
var fallbackFrame = document.getElementById(adDivId + '-iframe') || document.createElement('iframe');
fallbackFrame.id = adDivId + '-iframe';
fallbackFrame.src = adIframeSrc;
fallbackFrame.width = '100%';
fallbackFrame.style.border = '0';
fallbackFrame.style.minHeight = '250px';
fallbackFrame.setAttribute('scrolling', 'no');
fallbackFrame.setAttribute('title', 'advertisement');
var mountTarget = document.getElementById(adDivId) || adElement;
if (mountTarget && !mountTarget.contains(fallbackFrame)) {
mountTarget.innerHTML = '';
mountTarget.appendChild(fallbackFrame);
}
console.log('瀑布流廣告使用 iframe 備援:', adDivId, adIframeSrc);
} else {
console.log('廣告資訊不完整,無法顯示:', adDivId, { adAdunit, adAdsize });
}
} catch (error) {
console.error('瀑布流廣告初始化錯誤:', error, adKey);
}
}
// 重新計算瀑布流容器高度 - 廣告載入後調整
function recalculateWaterfallContainerHeight(articleId) {
// 找到對應的瀑布流容器
var waterfallContainer = document.querySelector('[data-article-id="' + articleId + '"]');
if (!waterfallContainer) {
console.log('找不到瀑布流容器,無法重新計算高度');
return;
}
// 等待DOM更新完成後測量實際高度
requestAnimationFrame(function() {
var actualHeight = waterfallContainer.scrollHeight;
var currentHeight = waterfallContainer.offsetHeight;
console.log('瀑布流容器高度重算:', articleId, '當前:', currentHeight, '實際:', actualHeight);
// 如果實際高度比當前高度大,更新容器高度
if (actualHeight > currentHeight) {
waterfallContainer.style.height = actualHeight + 'px';
waterfallContainer.style.minHeight = actualHeight + 'px';
console.log('瀑布流容器高度已更新:', articleId, actualHeight + 'px');
// 強制重繪確保footer被正確推下
waterfallContainer.offsetHeight;
// 通知其他元素容器高度已變更
var event = new CustomEvent('waterfallHeightChanged', {
detail: { articleId: articleId, newHeight: actualHeight }
});
document.dispatchEvent(event);
} else {
console.log('瀑布流容器高度無需調整:', articleId);
}
});
}
// 在 virtual_pageview 事件中修改標題處理邏輯
function processWaterfallTitle(title) {
// 確保標題格式統一
if (title.endsWith(' - JUKSY 街星')) {
return title; // 已經有正確格式
} else if (title.endsWith('JUKSY 街星')) {
return title.replace(/JUKSY 街星$/, ' - JUKSY 街星'); // 補上 " - "
} else {
return title + ' - JUKSY 街星'; // 完全沒有後綴
}
}