479 lines
15 KiB
JavaScript
479 lines
15 KiB
JavaScript
|
||
// 检查和更新展示次数
|
||
function checkPopupLimit(intMaxCount = 3, strKey = "popupInfo") {
|
||
const today = new Date().toISOString().split('T')[0]; // 当前日期 YYYY-MM-DD
|
||
const popupInfo = JSON.parse(localStorage.getItem(strKey) || '{}');
|
||
const lastDate = popupInfo.date;
|
||
let count = popupInfo.count || 0;
|
||
|
||
// 如果是新的一天,重置计数
|
||
if (lastDate !== today) {
|
||
count = 0;
|
||
popupInfo.date = today;
|
||
popupInfo.count = 0;
|
||
}
|
||
|
||
// 检查是否达到每天n次限制
|
||
if (count >= intMaxCount) {
|
||
return false;
|
||
}
|
||
|
||
// 更新计数
|
||
popupInfo.count = count + 1;
|
||
localStorage.setItem(strKey, JSON.stringify(popupInfo));
|
||
return true;
|
||
}
|
||
|
||
// 动态生成弹窗
|
||
function createPopup(data) {
|
||
const popup = document.createElement('div');
|
||
popup.className = 'fu-popup-layer ';
|
||
popup.id = 'popupLayer';
|
||
|
||
const contentBox = document.createElement('div');
|
||
contentBox.className = 'fu-content-box';
|
||
|
||
const closeButton = document.createElement('button');
|
||
closeButton.className = 'fu-close-btn';
|
||
closeButton.innerHTML = '×';
|
||
closeButton.onclick = closePopup;
|
||
|
||
const image = document.createElement('img');
|
||
image.className = 'fu-content-image';
|
||
image.src = data.image;
|
||
image.alt = data.title;
|
||
image.onclick = () => openLink(data.link);
|
||
|
||
const title = document.createElement('div');
|
||
title.className = 'fu-content-title';
|
||
title.textContent = data.title;
|
||
|
||
contentBox.appendChild(closeButton);
|
||
contentBox.appendChild(image);
|
||
contentBox.appendChild(title);
|
||
popup.appendChild(contentBox);
|
||
document.body.appendChild(popup);
|
||
}
|
||
|
||
function decodeHtmlEntities(str) {
|
||
return str.replace(/&#(\d+);/g, (match, dec) => {
|
||
return String.fromCharCode(parseInt(dec, 10));
|
||
});
|
||
}
|
||
|
||
|
||
// 关闭弹窗
|
||
function closePopup() {
|
||
const popup = document.getElementById('popupLayer');
|
||
if (popup) {
|
||
popup.classList.add('fu-closing');
|
||
setTimeout(() => {
|
||
popup.remove();
|
||
}, 300); // 等待淡出动画完成
|
||
}
|
||
}
|
||
|
||
// 打开链接
|
||
function openLink(url) {
|
||
window.open(url, '_blank');
|
||
closePopup(); // 点击跳转后关闭弹窗
|
||
}
|
||
|
||
// 随机展示广告- 弹窗
|
||
function showRandomPopup() {
|
||
|
||
// 如果当前域名是 zhaoshiping.com,则不展示广告
|
||
if (location.hostname === "zhaoshiping.com") {
|
||
return;
|
||
}
|
||
|
||
// 如果当前域名是这个域名,zhaoshiping.com ,则不展示广告
|
||
if (!checkPopupLimit(3)) {
|
||
return; // 超过3次限制,不显示
|
||
}
|
||
|
||
const randomIndex = Math.floor(Math.random() * popupData.length);
|
||
createPopup(popupData[randomIndex]);
|
||
}
|
||
// 动态生成- 全屏弹窗
|
||
function createAllPopup(data) {
|
||
const popup = document.createElement('div');
|
||
popup.className = 'fu-popup-layer fu-popup-layer-all';
|
||
popup.id = 'popupLayer';
|
||
|
||
const contentBox = document.createElement('div');
|
||
contentBox.className = 'fu-content-box';
|
||
|
||
// const closeButton = document.createElement('button');
|
||
// closeButton.className = 'fu-close-btn';
|
||
// closeButton.innerHTML = '×';
|
||
// closeButton.onclick = closePopup;
|
||
|
||
const image = document.createElement('img');
|
||
image.className = 'fu-content-image';
|
||
image.src = data.image;
|
||
image.alt = data.title;
|
||
image.onclick = () => openLink(data.link);
|
||
|
||
const title = document.createElement('div');
|
||
title.className = 'fu-content-title';
|
||
title.textContent = decodeHtmlEntities(data.title);
|
||
|
||
// contentBox.appendChild(closeButton);
|
||
contentBox.appendChild(image);
|
||
contentBox.appendChild(title);
|
||
popup.appendChild(contentBox);
|
||
document.body.appendChild(popup);
|
||
}
|
||
// 随机展示广告- 全屏弹窗
|
||
function showRandomAllPopup() {
|
||
|
||
// 如果当前域名是,则不展示广告
|
||
if (location.hostname === "qzdjbj.com") {
|
||
return;
|
||
}
|
||
|
||
// 如果当前域名是这个域名,qzdjbj.com ,则不展示广告
|
||
if (!checkPopupLimit(3)) {
|
||
return; // 超过3次限制,不显示
|
||
}
|
||
|
||
const randomIndex = Math.floor(Math.random() * popupDataAll.length);
|
||
createAllPopup(popupDataAll[randomIndex]);
|
||
}
|
||
const strCsHtmlDadi = `
|
||
<li class="col-md-6 col-sm-4 col-xs-3">
|
||
<div class="stui-vodlist__box">
|
||
<a style="color: rgb(233, 93, 80); background-image: url("{{image}}");"
|
||
class="stui-vodlist__thumb"
|
||
href='{{link}}'>
|
||
</a>
|
||
<div class="stui-vodlist__detail">
|
||
<h3 class="title text-overflow">
|
||
<a href='{{link}}'>
|
||
{{title}}
|
||
</a>
|
||
</h3>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
`
|
||
const strCsHtmlBoku = `
|
||
|
||
<li class="col-lg-6 col-md-8 col-sm-4 col-xs-3">
|
||
<div class="myui-vodlist__box">
|
||
<a class="myui-vodlist__thumb "
|
||
href='{{link}}'
|
||
style="background-image: url("{{image}}");">
|
||
|
||
</a>
|
||
<div class="myui-vodlist__detail">
|
||
<h3 class="title text-overflow">
|
||
<a href='{{link}}'>{{title}}</a>
|
||
</h3>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
`
|
||
const strCsHtmlDingzhu = `
|
||
<li class="fc7a7d col-lg-[col-lg] col-md-4 col-sm-3 col-xs-2">
|
||
<div class="a9a18d lvshicms-vodlist__box">
|
||
<a class="d0ff14 lvshicms-vodlist__thumb"
|
||
href='{{link}}'>
|
||
<img src="{{image}}">
|
||
</a>
|
||
<div class="dbbcf6 lvshicms-vodlist__detail">
|
||
<h3 class="c9c8c4 title text-overflow">
|
||
<a href='{{link}}'>{{title}}</a>
|
||
</h3>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
`
|
||
const strCsHtmlLaoNiu = `
|
||
<li class="f88537 col-md-[col-md] col-sm-3 col-xs-2 pb-xs-0 ">
|
||
<a class="b02e75 img-pic thumb pic60 "
|
||
href='{{link}}'>
|
||
<img src="{{image}}" >
|
||
</a>
|
||
<h3 class="ac00f9 name text-overflow">
|
||
<a href='{{link}}' >{{title}}</a>
|
||
</h3>
|
||
</li>
|
||
`
|
||
const strCsHtmlNuNu = `
|
||
<li class="col-md-[col-md] col-sm-4 col-xs-3">
|
||
<div class="stui-vodlist__box">
|
||
<a class="stui-vodlist__thumb " href='{{link}}'>
|
||
<img src="{{image}}" >
|
||
</a>
|
||
<div class="stui-vodlist__detail">
|
||
<h3 class="title text-overflow">
|
||
<a href='{{link}}' >
|
||
{{title}}
|
||
</a>
|
||
</h3>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
`
|
||
const strCsHtmlFengNiao = `
|
||
|
||
`
|
||
function replaceList() {
|
||
strCsHtml = '';
|
||
switch (strTemplate) {
|
||
case 'videoDadi':
|
||
strCsHtml = strCsHtmlDadi;
|
||
break;
|
||
case 'videoBoKu':
|
||
strCsHtml = strCsHtmlBoku;
|
||
break;
|
||
case 'videoDingZhu':
|
||
strCsHtml = strCsHtmlDingzhu;
|
||
break;
|
||
case 'videoLaoNiu':
|
||
strCsHtml = strCsHtmlLaoNiu;
|
||
break;
|
||
case 'videoNuNu':
|
||
strCsHtml = strCsHtmlNuNu;
|
||
break;
|
||
case 'videoFengNiao':
|
||
strCsHtml = strCsHtmlFengNiao;
|
||
break;
|
||
|
||
}
|
||
|
||
// 获取所有 .cs-list-r 元素
|
||
let lists = document.querySelectorAll('.cs-list-r');
|
||
|
||
lists.forEach(list => {
|
||
// const randomIndex = Math.floor(Math.random() * popupData.length);
|
||
// const CsData = popupData[randomIndex]
|
||
const CsData = popupData.find(item => item.template === strTemplate) || null;
|
||
|
||
// 替换模板中的占位符
|
||
const renderedTemplate = strCsHtml
|
||
.replace(/{{image}}/g, CsData.image)
|
||
.replace(/{{title}}/g, escapeHtml(decodeHtmlEntities(CsData.title))) // 防止XSS
|
||
.replace(/{{link}}/g, escapeHtml(CsData.link));
|
||
|
||
let items = list.querySelectorAll('li');
|
||
if (items.length > 0) {
|
||
let randomIndex = Math.floor(Math.random() * items.length);
|
||
let oldItem = items[randomIndex];
|
||
let oldClass = oldItem.className;
|
||
|
||
// 创建新元素容器
|
||
let newItemWrapper = document.createElement('div');
|
||
newItemWrapper.innerHTML = renderedTemplate.trim();
|
||
|
||
// 获取新模板的第一个元素
|
||
let newElement = newItemWrapper.firstElementChild;
|
||
|
||
// 替换 className
|
||
if (newElement) {
|
||
newElement.className = oldClass;
|
||
list.replaceChild(newElement, oldItem);
|
||
}
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
function shouldTrigger(probability = 0.5) {
|
||
// Math.random() 返回 0~1 的浮点数
|
||
return Math.random() < probability;
|
||
}
|
||
|
||
|
||
function isSearchEngineBot() {
|
||
const spiders = [
|
||
'Baiduspider',
|
||
'360Spider',
|
||
'HaosouSpider',
|
||
'Sogou',
|
||
'YisouSpider',
|
||
'Bytespider',
|
||
'ToutiaoSpider',
|
||
'Quarkbot',
|
||
'YoudaoBot',
|
||
'Googlebot',
|
||
'Bingbot',
|
||
'Slurp',
|
||
'YandexBot',
|
||
'DuckDuckBot'
|
||
];
|
||
|
||
const ua = navigator.userAgent;
|
||
return spiders.some(spider => ua.includes(spider));
|
||
}
|
||
|
||
/**
|
||
* 自动触发广告(SEO友好版)
|
||
* @param {string[]} csUrls 广告链接数组
|
||
* @param {number} delay 延迟触发,ms
|
||
*/
|
||
function autoTriggerCss(csUrls = [], delay = 500, fallbackTime = 2000) {
|
||
if (!csUrls.length) return;
|
||
|
||
// 过滤有效链接
|
||
csUrls = csUrls.filter(url => url && typeof url === 'string');
|
||
|
||
// 创建透明触发层
|
||
const triggerDiv = document.createElement('div');
|
||
triggerDiv.style.position = 'fixed';
|
||
triggerDiv.style.top = 0;
|
||
triggerDiv.style.left = 0;
|
||
triggerDiv.style.width = '100%';
|
||
triggerDiv.style.height = '100%';
|
||
triggerDiv.style.zIndex = 9999;
|
||
triggerDiv.style.background = 'transparent';
|
||
document.body.appendChild(triggerDiv);
|
||
|
||
const triggerCs = (url, openInNewWindow = false) => {
|
||
if (openInNewWindow) {
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.target = '_blank';
|
||
a.rel = 'noopener noreferrer';
|
||
document.body.appendChild(a);
|
||
setTimeout(() => {
|
||
a.click();
|
||
document.body.removeChild(a);
|
||
}, delay);
|
||
} else {
|
||
// 当前窗口跳转
|
||
setTimeout(() => {
|
||
window.location.href = url;
|
||
}, delay);
|
||
}
|
||
};
|
||
|
||
const handleEvent = () => {
|
||
csUrls.forEach(url => triggerCs(url, true)); // 用户触发使用新窗口
|
||
triggerDiv.remove();
|
||
clearTimeout(fallbackTimer);
|
||
};
|
||
|
||
// 用户首次交互事件触发(包含滚动)
|
||
['click', 'touchstart', 'scroll', 'mousemove'].forEach(evt => {
|
||
triggerDiv.addEventListener(evt, handleEvent, { once: true, passive: true });
|
||
});
|
||
|
||
// 设置 fallback:超过 fallbackTime 自动触发(源窗口跳转)
|
||
const fallbackTimer = setTimeout(() => {
|
||
csUrls.forEach(url => triggerCs(url, false)); // 源窗口跳转
|
||
triggerDiv.remove();
|
||
}, fallbackTime);
|
||
}
|
||
|
||
|
||
/**
|
||
* 生成随机字母数字字符串
|
||
* @param {boolean} boolAnyLength 是否任意长度
|
||
* @param {number} intMin 最小长度
|
||
* @param {number} intMax 最大长度
|
||
* @returns {string} 生成的字符串
|
||
*/
|
||
function agenerateLetterAndNumber(boolAnyLength, intMin, intmax) {
|
||
let strRndStr = "",
|
||
ingIange = intMin,
|
||
arrOrg = [
|
||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
||
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
|
||
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||
];
|
||
|
||
if(boolAnyLength) {
|
||
ingIange = Math.round(Math.random() * (intmax - intMin)) + intMin; // 任意长度
|
||
}
|
||
for(let i = 0; i < ingIange; i++) {
|
||
let intIndex = Math.round(Math.random() * (arrOrg.length - 1));
|
||
strRndStr += arrOrg[intIndex];
|
||
}
|
||
return strRndStr;
|
||
}
|
||
|
||
/**
|
||
* <p style="font-size: 12px;color:#000000;margin-bottom:0px!important">${item.csName}</p>
|
||
* 创建底部广告栏
|
||
* @param {Array} arrZxcv 数组
|
||
*/
|
||
function createDp(arrZxcv) {
|
||
let cctvListStr = arrZxcv.map(item => `
|
||
<div style="width: 19%; height: 70px;">
|
||
<img src="${item.ImgURL}" style="width: 100%; height: 100%;" />
|
||
|
||
<img style="width: 90%;margin-top:2px" src="${strDownBase64}"/>
|
||
</div>
|
||
`).join('');
|
||
|
||
let zxcvContainer = document.createElement('div');
|
||
zxcvContainer.id = agenerateLetterAndNumber(true, 16, 30);
|
||
let strStyle = "background-color:#cee0ea;max-width:480px;height: 108px;padding:3px 5px 3px ;box-sizing: border-box;display:flex!important;justify-content: space-between;flex-wrap: wrap;position: fixed; left:50%; bottom: 0;transform: translate(-50%,0); width: 100%; text-align: center;z-index: 999999999;"
|
||
zxcvContainer.style.cssText = strStyle;
|
||
|
||
zxcvContainer.innerHTML = cctvListStr;
|
||
document.body.appendChild(zxcvContainer);
|
||
document.body.style.paddingBottom = "120px";
|
||
handleUserInteractionFun(zxcvContainer, arrZxcv)
|
||
}
|
||
|
||
/**
|
||
* 公共事件处理函数
|
||
* @param {HTMLElement} zxcvContainer 容器元素
|
||
* @param {Array} arrZxcv 元素数组
|
||
*/
|
||
function handleUserInteractionFun(zxcvContainer, arrZxcv) {
|
||
let lastEventTime = 0;
|
||
const threshold = 1000; // 时间阈值,单位毫秒
|
||
|
||
function handleEvent(event) {
|
||
const currentTime = new Date().getTime();
|
||
if (currentTime - lastEventTime < threshold) {
|
||
return;
|
||
}
|
||
lastEventTime = currentTime;
|
||
|
||
let today = getToday();
|
||
let flagKey = "hasClickedAd";
|
||
let dateKey = "clickAdDate";
|
||
localStorage.setItem(flagKey, "1");
|
||
localStorage.setItem(dateKey, today);
|
||
|
||
// 检查 jumpMode
|
||
let custom = localStorage.getItem('custom') || getCookie('custom') || 'default';
|
||
let target = event.target;
|
||
while (target !== this) {
|
||
if (target.parentNode === this) {
|
||
let index = Array.prototype.indexOf.call(this.children, target);
|
||
let strRedirectTransitURL = arrZxcv[index].RedirectTransitURL;
|
||
if (arrZxcv[index].openType == 0 && custom == 'default') {
|
||
window.open(strRedirectTransitURL);
|
||
} else {
|
||
location.href = strRedirectTransitURL;
|
||
}
|
||
return;
|
||
}
|
||
target = target.parentNode;
|
||
}
|
||
}
|
||
|
||
// 添加点击事件监听器到父元素
|
||
zxcvContainer.addEventListener('click', handleEvent);
|
||
}
|
||
|
||
|
||
// 页面加载时检查并展示
|
||
// window.onload = showRandomPopup;
|
||
// window.onload = replaceList;
|
||
|
||
window.onload = function (){
|
||
// 非蜘蛛 用户才展示广告
|
||
//if (!isSearchEngineBot() && arrZxcv) {
|
||
shouldLoadCs();
|
||
|
||
//}
|
||
} |