Template:18PlusDisclaimer: Difference between revisions
Template page
More actions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
<templatestyles src="Template:18PlusDisclaimer/styles.css" /> | <templatestyles src="Template:18PlusDisclaimer/styles.css" /> | ||
{{#tag:html| | {{#tag:html| | ||
<div id="ageDisclaimerOverlay" class="age-overlay" | <div id="ageDisclaimerOverlay" class="age-overlay" role="dialog" aria-modal="true" aria-labelledby="ageDisclaimerTitle" aria-describedby="ageDisclaimerDesc"> | ||
<div class="age-blur"></div> | <div class="age-blur" aria-hidden="true"></div> | ||
<div class="age-popup | <div class="age-popup" tabindex="-1"> | ||
<h2>You must be 18+ to view this content</h2> | <h2 id="ageDisclaimerTitle">You must be 18+ to view this content</h2> | ||
<p>This page may contain content intended for mature audiences only.<br>Please confirm that you are 18 years of age or older.</p> | <p id="ageDisclaimerDesc"> | ||
This page may contain content intended for mature audiences only.<br> | |||
Please confirm that you are 18 years of age or older. | |||
</p> | |||
<div class="btn-group"> | <div class="btn-group"> | ||
<button id="acceptDisclaimer">I am 18 or older</button> | <button id="acceptDisclaimer" type="button">I am 18 or older</button> | ||
<button id="declineDisclaimer">Leave this page</button> | <button id="declineDisclaimer" type="button">Leave this page</button> | ||
</div> | </div> | ||
</div> | </div> | ||
</div> | </div> | ||
< | <script> | ||
(function() { | |||
(function( | // cookie helpers | ||
// | function setCookie(name, value, days) { | ||
const expires = new Date(Date.now() + days * 864e5).toUTCString(); | |||
document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + expires + "; path=/"; | |||
} | |||
function getCookie(name) { | |||
const match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)')); | |||
return match ? decodeURIComponent(match[1]) : null; | |||
} | |||
document.addEventListener("DOMContentLoaded", function() { | |||
const overlay = document.getElementById("ageDisclaimerOverlay"); | |||
const popup = overlay.querySelector(".age-popup"); | |||
const body = document.body; | |||
const accept = document.getElementById("acceptDisclaimer"); | |||
const decline = document.getElementById("declineDisclaimer"); | |||
// show | // show overlay if not yet confirmed | ||
if (! | if (!getCookie("ageConfirmed")) { | ||
overlay. | overlay.classList.add("active"); | ||
body.style.overflow = "hidden"; | |||
popup.focus(); | |||
} | } | ||
accept.addEventListener("click", function() { | |||
setCookie("ageConfirmed", "true", 30); | |||
overlay.classList.remove("active"); | |||
overlay. | body.style.overflow = ""; | ||
}); | }); | ||
decline.addEventListener("click", function() { | |||
window.location.href = | // redirect to Main_Page via mw.config | ||
const url = mw.config.get("wgServer") + mw.config.get("wgArticlePath").replace("$1","Main_Page"); | |||
window.location.href = url; | |||
}); | |||
// keep focus inside popup | |||
document.addEventListener("focusin", function(e) { | |||
if (overlay.classList.contains("active") && !overlay.contains(e.target)) { | |||
e.stopPropagation(); | |||
popup.focus(); | |||
} | |||
}); | }); | ||
}); | }); | ||
})( | })(); | ||
</script> | |||
}} | |||
Revision as of 11:07, 19 April 2025