%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/lib/ |
| Current File : //home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/lib/devtoolsGuard.ts |
/**
* Anti-Inspect (deterrent) — menghalangi jalur paling umum untuk membuka
* DevTools: klik kanan, F12, shortcut Ctrl+Shift+I/J/C, Ctrl+U, dan deteksi
* DevTools terbuka via perbedaan ukuran jendela (khusus desktop).
*
* Saat DevTools terdeteksi di desktop, halaman langsung ditutupi overlay
* penuh yang memblokir SEMUA interaksi (klik, scroll, keypress) sampai
* DevTools ditutup.
*
* Catatan:
* - Ini adalah PENCEGAH (deterrent), bukan keamanan absolut — DevTools
* tetap bisa dibuka lewat menu browser atau alat eksternal.
* - Di perangkat sentuh (telepon/tablet), deteksi ukuran jendela dan blokir
* menu konteks DIMATIKAN. Browser mobile punya address bar dinamis dan
* keyboard virtual yang membuat selisih outer/inner width-height besar
* tanpa ada DevTools — memicu false positive.
*/
function showWarningOverlay() {
if (document.getElementById("devtools-guard-overlay")) return;
const overlay = document.createElement("div");
overlay.id = "devtools-guard-overlay";
overlay.style.cssText = [
"position:fixed",
"inset:0",
"z-index:99999",
"display:flex",
"align-items:center",
"justify-content:center",
"background:rgba(5,5,5,0.9)",
"backdrop-filter:blur(8px)",
"webkitBackdropFilter:blur(8px)",
"cursor:not-allowed",
].join(";");
const box = document.createElement("div");
box.style.cssText = [
"max-width:420px",
"margin:0 24px",
"padding:28px 32px",
"border-radius:20px",
"border:1px solid rgba(255,255,255,0.08)",
"background:#0c0c0f",
"text-align:center",
"box-shadow:0 24px 64px rgba(0,0,0,0.7)",
].join(";");
const icon = document.createElement("div");
icon.textContent = "⚠️";
icon.style.cssText = "font-size:36px;margin-bottom:12px;";
const title = document.createElement("p");
title.textContent = "Alat Pengembang Terdeteksi";
title.style.cssText =
"color:#fff;font-size:16px;font-weight:700;margin-bottom:8px;font-family:Inter,system-ui,sans-serif;";
const desc = document.createElement("p");
desc.textContent =
"Membuka inspection console untuk memanipulasi atau mengekstrak data dilarang. Tutup alat pengembang untuk melanjutkan.";
desc.style.cssText =
"color:#94a3b8;font-size:13px;line-height:1.6;font-family:Inter,system-ui,sans-serif;";
box.appendChild(icon);
box.appendChild(title);
box.appendChild(desc);
overlay.appendChild(box);
document.body.appendChild(overlay);
}
function hideWarningOverlay() {
const overlay = document.getElementById("devtools-guard-overlay");
if (overlay) overlay.remove();
}
function isTouchDevice(): boolean {
if (window.matchMedia?.("(pointer: coarse)").matches) return true;
return navigator.maxTouchPoints > 0;
}
function isDevtoolsOpen(): boolean {
// Cegah false positive di mobile: browser sentuh punya address bar dinamis
// dan keyboard virtual yang mengubah ukuran jendela tanpa DevTools.
if (isTouchDevice()) return false;
const threshold = 160;
return (
window.outerWidth - window.innerWidth > threshold ||
window.outerHeight - window.innerHeight > threshold
);
}
export function installDevtoolsGuard() {
const isMobile = isTouchDevice();
// Blokir klik kanan — hanya di perangkat non-sentuh; di mobile klik panjang
// difungsikan sebagai menu konteks sistem (salin/tempel, buka tautan, dll.).
if (!isMobile) {
document.addEventListener("contextmenu", (e) => e.preventDefault(), true);
}
// Blokir shortcut DevTools & menu sumber
document.addEventListener(
"keydown",
(e) => {
const key = (e.key || "").toLowerCase();
const devtoolsShortcut =
e.key === "F12" ||
(e.ctrlKey && e.shiftKey && (key === "i" || key === "j" || key === "c")) ||
(e.metaKey && e.shiftKey && (key === "i" || key === "j" || key === "c")) ||
(e.ctrlKey && key === "u") ||
(e.metaKey && key === "u");
if (devtoolsShortcut) {
e.preventDefault();
e.stopPropagation();
}
},
true,
);
// Deteksi DevTools terbuka (via ukuran jendela) — khusus desktop.
// Overlay muncul SELAMA DevTools terbuka dan langsung hilang saat ditutup.
if (!isMobile) {
window.setInterval(() => {
if (isDevtoolsOpen()) showWarningOverlay();
else hideWarningOverlay();
}, 500);
}
}