|
1 | | -// javascript proof of work to prevent headless browsers while allowing 'good scrapers' like ones that just analyze the HTML |
2 | | - |
3 | | -(function () { |
4 | | - const difficulty = 16; |
5 | | - const timeoutMs = 30000; |
6 | | - let nonce = 0; |
7 | | - |
8 | | - async function sha256(input) { |
9 | | - const encoder = new TextEncoder(); |
10 | | - const data = encoder.encode(input); |
11 | | - const hashBuffer = await crypto.subtle.digest("SHA-256", data); |
12 | | - return Array.from(new Uint8Array(hashBuffer)) |
13 | | - .map(b => b.toString(16).padStart(2, '0')) |
14 | | - .join(''); |
15 | | - } |
16 | | - |
17 | | - async function doProofOfWork(deadline) { |
18 | | - const prefix = "0".repeat(difficulty / 4); |
19 | | - while (performance.now() < deadline) { |
20 | | - const hash = await sha256(nonce.toString()); |
21 | | - if (hash.startsWith(prefix)) return nonce; |
22 | | - nonce++; |
23 | | - if (nonce % 10000 === 0) await new Promise(r => setTimeout(r, 1)); |
24 | | - } |
25 | | - return null; |
26 | | - } |
27 | | - |
28 | | - (async function () { |
29 | | - const deadline = performance.now() + timeoutMs; |
30 | | - const statusDiv = document.getElementById("pow-status"); |
31 | | - |
32 | | - const allElements = Array.from(document.body.children); |
33 | | - const hiddenElements = allElements.filter(el => el !== statusDiv); |
34 | | - hiddenElements.forEach(el => el.style.display = "none"); |
35 | | - |
36 | | - const updateTimerAndHash = async () => { |
37 | | - const timeLeft = Math.max(0, deadline - performance.now()); |
38 | | - const currentHash = await sha256(nonce.toString()); |
39 | | - |
40 | | - statusDiv.innerHTML = ` |
41 | | - Checking if you are not a robot... |
42 | | - <br> |
43 | | - Time remaining: ${timeLeft.toFixed(0)}ms |
44 | | - <br> |
45 | | - Current Hash: ${currentHash} |
46 | | - <br> |
47 | | - Attempts: ${nonce} |
48 | | - <br> |
49 | | - Difficulty: ${difficulty} |
50 | | - `; |
51 | | - }; |
52 | | - |
53 | | - |
54 | | - // check if the user is a human by checking if the cookie is set |
55 | | - if (document.cookie.includes("isHuman=true")) { |
56 | | - hiddenElements.forEach(el => el.style.display = ""); |
57 | | - statusDiv.textContent = ""; |
58 | | - return; |
59 | | - } |
60 | | - |
61 | | - updateTimerAndHash(); |
62 | | - const interval = setInterval(updateTimerAndHash, 100); |
63 | | - |
64 | | - const solvedNonce = await doProofOfWork(deadline); |
65 | | - |
66 | | - clearInterval(interval); |
67 | | - |
68 | | - if (solvedNonce !== null) { |
69 | | - hiddenElements.forEach(el => el.style.display = ""); |
70 | | - statusDiv.textContent = ""; |
71 | | - // store a cookie to remember the user is human |
72 | | - document.cookie = "isHuman=true; path=/"; |
73 | | - } else { |
74 | | - statusDiv.textContent = "Access denied. If you are a human, please try again later."; |
75 | | - hiddenElements.forEach(el => el.innerHTML = ""); |
76 | | - // redirect to example.com |
77 | | - window.location.href = "https://example.com"; |
78 | | - } |
79 | | - })(); |
80 | | -})(); |
| 1 | +console.log("Hello world"); |
0 commit comments