-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeImprint.ts
95 lines (82 loc) · 3.28 KB
/
NativeImprint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { captchaData } from './captchaData';
import { solveCaptcha } from './captchaLogic';
import { AddressInfo } from './AddressInfo';
import "./Imprint.css"
// some simple helpers for creating elements
const br = () => document.createElement("br");
const div = (className: string) => {
const div = document.createElement("div");
div.className = className;
return div;
}
/**
* Creates an imprint element.
*
* @returns A HTML element that can be appended to the document.
*/
export function createImprintElement(language: "deDe" | "enUs" = "enUs"): HTMLDivElement {
let decryptedData: AddressInfo | null = null;
const onDataUpdate: (() => void)[] = [];
function censoredElement(key: keyof AddressInfo, placeholder: string) {
const censoredElement = document.createElement("span");
const updateElement = () => {
if (decryptedData === null) {
censoredElement.className = "imprint-censored";
censoredElement.innerText = placeholder;
}
if (decryptedData !== null) {
censoredElement.className = "";
censoredElement.innerText = decryptedData[key];
}
};
updateElement();
onDataUpdate.push(updateElement)
return censoredElement;
}
const wrapper = div("imprint-columns");
const address = div("imprint-address");
wrapper.appendChild(address);
address.append(
"Verantwortlicher nach §5 TMG:", br(),
"Georg Wicke-Arndt", br(),
censoredElement("address1", "Beispielstraße 123"), br(),
censoredElement("address2", "12345 Neustadt"), br(),
"Tel.: ", censoredElement("tel", "0123 456789"), br(),
"E-Mail: ", censoredElement("email", "georg@example.com"), br(),
);
const captchaElement = div("imprint-captcha");
wrapper.appendChild(captchaElement);
captchaElement.innerHTML = {
enUs: "<h3>Are you a robot?</h3>"
+ "To prevent scraping, the address data is encrypted. "
+ "Please answer the following question to reveal it.",
deDe: "<h3>Bist du ein Roboter?</h3>"
+ "Um Scraping zu verhindern sind die Adressdaten verschlüsselt. "
+ "Bitte beantworte die folgende Frage, um sie aufzudecken."
}[language];
captchaElement.append(br());
const question = document.createElement("i")
question.innerText = captchaData.question[language];
captchaElement.append(question, br());
const answer = document.createElement("input");
answer.type = "text";
answer.placeholder = "Type answer here";
answer.oninput = e => trySolve(answer.value);
captchaElement.append(answer);
let currentAnswer = "";
let activePromise: Promise<any> | null = null;
const trySolve = (answer: string) => {
currentAnswer = answer;
if (!activePromise) {
activePromise = solveCaptcha(captchaData, answer).then(revealed => {
activePromise = null;
decryptedData = revealed !== null ? JSON.parse(revealed) : null;
if (revealed === null && currentAnswer !== answer)
trySolve(currentAnswer);
else
onDataUpdate.forEach(x => x());
});
}
};
return wrapper;
}