<script>
(function() {
// 1. 弹窗类封装(参考[1,4](@ref)的类封装思想)
class DeviceAlert {
constructor() {
this.alertBox = this.createAlertHTML();
this.countdown = 10;
this.timer = null;
document.body.appendChild(this.alertBox);
}
// 2. 创建弹窗DOM结构(参考[5](@ref)的动态元素创建)
createAlertHTML() {
const alertDiv = document.createElement('div');
alertDiv.id = 'device-alert';
alertDiv.className = 'alert alert-warning';
alertDiv.style.cssText = 'position:fixed;top:1rem;left:50%;transform:translateX(-50%);z-index:9999;display:none;';
alertDiv.innerHTML = `
<div style="text-align:center;margin-bottom:10px;font-size:1rem;">
<i class="far fa-grin-beam-sweat me-1" style="color:#c14848;font-size:1.2rem;"></i>
<strong>请注意:</strong> 本站资源为 MacOS 专用,Windows 电脑下载无法使用!
</div>
<div class="d-flex justify-content-center gap-2">
<button type="button" class="btn btn-danger" id="continue-browse" style="min-width:140px;">继续浏览(10秒)</button>
<button type="button" class="btn btn-secondary" id="no-remind" style="min-width:140px;">不再提醒</button>
</div>
`;
return alertDiv;
}
// 3. 设备检测逻辑(原生JS实现)
detectDevice() {
const isMac = navigator.platform.includes("Mac");
const isMobile = /Mobi|Android/i.test(navigator.userAgent);
return { isMac, isMobile };
}
// 4. 核心逻辑:条件触发弹窗
init() {
const deviceInfo = this.detectDevice();
const isClosed = localStorage.getItem("device_notice_closed") === "true";
if (!deviceInfo.isMac && !deviceInfo.isMobile && !isClosed) {
setTimeout(() => {
this.alertBox.style.display = 'block';
this.startCountdown();
}, 2000);
}
this.bindEvents();
}
// 5. 倒计时控制
startCountdown() {
const continueBtn = document.getElementById('continue-browse');
this.timer = setInterval(() => {
this.countdown--;
continueBtn.textContent = `继续浏览(${this.countdown}秒)`;
if (this.countdown <= 0) {
clearInterval(this.timer);
this.alertBox.style.display = 'none';
}
}, 1000);
}
// 6. 事件绑定(参考[4](@ref)的事件监听方法)
bindEvents() {
document.getElementById('no-remind').addEventListener('click', () => {
localStorage.setItem("device_notice_closed", "true");
clearInterval(this.timer);
this.alertBox.style.display = 'none';
});
document.getElementById('continue-browse').addEventListener('click', () => {
clearInterval(this.timer);
this.alertBox.style.display = 'none';
});
}
}
// 7. 页面加载后初始化
document.addEventListener('DOMContentLoaded', () => {
new DeviceAlert().init();
});
})();
</script>
<script src="https://cdn.bootcdn.net/ajax/libs/limonte-sweetalert2/11.7.27/sweetalert2.all.min.js"></script>
<style>
.swal2-custom-icon img {
width: 200px;
height: 80px;
}
.popup-text {
font-size: 0.8em;
margin-bottom: 0.5em;
}
.popup-text-small {
font-size: 1em;
margin-bottom: 0.5em;
}
</style>
<script>
function showPopup() {
var currentTime = Math.floor(Date.now() / 1000);
var lastVisitTime = parseInt(localStorage.getItem('lastVisitTime')) || 0;
var elapsedTime = currentTime - lastVisitTime;
if (elapsedTime >= 900) {
console.log("显示弹窗");
Swal.fire({
title: '<div class="swal2-custom-icon"><img src="/qiniu_logo.png" class="custom-icon"></div>',
html:
'<div class="popup-text">1.新用户请先看<a href="https://www.xmac.im/faq/">答疑频道</a>|<a href="https://www.xmac.im/17_6.html">解压密码</a>|<a href="https://www.xmac.im/11_23.html">提取码错误解决方法</a></div>' +
'<div class="popup-text">2.资源提供多个下载源链接,偶见失效可用其他下载源</div>' +
'<div class="popup-text">3. 任何问题请提交工单,进行反馈后等待回复即可。。</div>' +
'<div class="popup-text-small">--------------</div>' +
'<div class="popup-text-small">www.xmac.im</div>',
showConfirmButton: true,
confirmButtonText: '我已确认收到此公告'
}).then((result) => {
if (result.isConfirmed) {
localStorage.setItem('lastVisitTime', currentTime.toString());
}
});
} else {
console.log("已访问过弹窗");
}
}
showPopup();
</script>