document.addEventListener('DOMContentLoaded', function() {
// --- Configuration ---
const POPUP_IMAGE_URL = 'https://cdn11.bigcommerce.com/s-9yct61arm5/images/stencil/original/image-manager/shopee-aff.jpg';
const POPUP_DESTINATION_URL = 'https://s.shopee.co.id/10qRHfkuUS';
const DISPLAY_DELAY_MS = 1000; // 1 second
// --- Prevent running if already processed or on certain admin/api paths ---
if (document.getElementById('customPopupAd')) {
console.log('Custom Popup: Already initialized.');
return;
}
if (window.location.pathname.startsWith('/manage') || window.location.pathname.startsWith('/api')) {
console.log('Custom Popup: Skipping for admin/api pages.');
return;
}
// --- Create and Inject CSS ---
const styles = `
#customPopupAdOverlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
z-index: 99998;
}
#customPopupAd {
display: none;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
z-index: 99999;
padding: 0;
background-color: transparent;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
border-radius: 8px;
}
#customPopupAd img {
display: block;
max-width: 90vw;
max-height: 80vh;
width: auto;
height: auto;
border-radius: 8px;
}
#customPopupAdClose {
position: absolute;
top: -15px;
right: -15px;
width: 40px;
height: 40px;
background-color: white;
color: black;
border: 2px solid black;
border-radius: 50%;
font-size: 28px;
font-weight: bold;
line-height: 38px; /* Adjust for vertical centering of X */
text-align: center;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
z-index: 100000; /* Ensure close button is on top of image */
}
#customPopupAdClose:hover {
background-color: #f0f0f0;
}
`;
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
// --- Create HTML Elements ---
const overlay = document.createElement('div');
overlay.id = 'customPopupAdOverlay';
document.body.appendChild(overlay);
const popupDiv = document.createElement('div');
popupDiv.id = 'customPopupAd';
popupDiv.innerHTML = `
×
`;
document.body.appendChild(popupDiv);
// --- Get References to New Elements ---
const popupElement = document.getElementById('customPopupAd'); // Re-fetch after creation
const overlayElement = document.getElementById('customPopupAdOverlay'); // Re-fetch
const closeButtonElement = document.getElementById('customPopupAdClose');
const adLinkElement = document.getElementById('customPopupAdLink');
let popupTimer;
function showPopup() {
if (popupElement && overlayElement) {
if (sessionStorage.getItem('customPopupClosed') !== 'true') {
popupElement.style.display = 'block';
overlayElement.style.display = 'block';
}
}
}
function closePopup() {
if (popupElement && overlayElement) {
popupElement.style.display = 'none';
overlayElement.style.display = 'none';
sessionStorage.setItem('customPopupClosed', 'true');
}
}
// Show popup after delay
popupTimer = setTimeout(showPopup, DISPLAY_DELAY_MS);
// Close button functionality
if (closeButtonElement) {
closeButtonElement.addEventListener('click', function(event) {
event.stopPropagation();
window.open(POPUP_DESTINATION_URL, '_blank');
closePopup();
clearTimeout(popupTimer);
});
}
// Ad link click (image click)
if (adLinkElement) {
adLinkElement.addEventListener('click', function() {
// Link opens in new tab via target="_blank"
// Close popup after a brief moment to ensure link opens
setTimeout(function() {
closePopup();
}, 100);
clearTimeout(popupTimer);
});
}
// Optional: Close popup if user clicks on the overlay
if (overlayElement) {
overlayElement.addEventListener('click', function() {
closePopup(); // Just close, don't open the link
clearTimeout(popupTimer);
});
}
});