56 lines
1.3 KiB
JavaScript
56 lines
1.3 KiB
JavaScript
(function () {
|
|
var root = document.querySelector("[data-eighteen]");
|
|
if (!root) return;
|
|
|
|
var key = "hg_age_ok";
|
|
var enterBtn = root.querySelector("[data-eighteen-enter]");
|
|
var exitBtn = root.querySelector("[data-eighteen-exit]");
|
|
|
|
function alreadyOk() {
|
|
try {
|
|
return window.localStorage.getItem(key) === "1";
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function close() {
|
|
var wasOpen = root.classList.contains("is-open");
|
|
root.classList.remove("is-open");
|
|
document.body.classList.remove("eighteen-locked");
|
|
document.documentElement.classList.add("eighteen-ok");
|
|
if (wasOpen) {
|
|
document.dispatchEvent(new CustomEvent("eighteen:closed"));
|
|
}
|
|
}
|
|
|
|
function open() {
|
|
root.classList.add("is-open");
|
|
document.body.classList.add("eighteen-locked");
|
|
}
|
|
|
|
// 已确认过:不展示
|
|
if (alreadyOk() || document.documentElement.classList.contains("eighteen-ok")) {
|
|
close();
|
|
return;
|
|
}
|
|
|
|
// 首次进入:展示
|
|
open();
|
|
|
|
if (enterBtn) {
|
|
enterBtn.addEventListener("click", function () {
|
|
try {
|
|
window.localStorage.setItem(key, "1");
|
|
} catch (e) {}
|
|
close();
|
|
});
|
|
}
|
|
|
|
if (exitBtn) {
|
|
exitBtn.addEventListener("click", function () {
|
|
window.location.href = "https://www.google.com";
|
|
});
|
|
}
|
|
})();
|