Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.documentElement.classList.remove("no-scripts");
- document.documentElement.classList.add("scripts");
- window.sssg_challenge = function (hashString, difficulty, numerToCalculatePatienceTime) {
- console.log("Starting work.");
- const worker = new Worker("/.sssg/public/worker.js");
- document.getElementById("loader").setAttribute("class", "playing");
- let currentUnixTime = Date.now()
- var deadLine = currentUnixTime + (6e4 - (currentUnixTime % 6e4)) + 6e4 * numerToCalculatePatienceTime;
- var finalSolution = null
- var amountOfCores = navigator.hardwareConcurrency
- var initialPosition = 4503599627370496 * Math.random()
- var attempt_number = initialPosition;
- console.log(`\t${deadLine - currentUnixTime}ms of patience.\n\tSalt is ${hashString}\n\tDifficutly of ${difficulty}\n\tInitial position of ${initialPosition}`);
- for (var u = 0; u < amountOfCores; ++u) worker.postMessage({ attempt: attempt_number, difficulty: difficulty, salt: hashString }), ++attempt_number;
- window.addEventListener("storage", (e) => {
- "sssg_clearance" == e.key &&
- e.newValue &&
- (worker.terminate(),
- "PAUSE" != e.newValue
- ? (console.log(
- `Accepting answer from LocalStorage!\n\t${e.newValue}`
- ),
- window.location.reload())
- : console.log("SessionStorage instructs a pause."));
- });
- worker.addEventListener("message",
- ({ data: { attempt: attempt, solution: solution } }) => {
- var newAttemptNumber = attempt + amountOfCores;
- if (null === solution) {
- var howManyAttempts = newAttemptNumber - initialPosition;
- null === finalSolution && (howManyAttempts % 1e4 == 0 && (console.log(`\tAttempt #${howManyAttempts}`),
- Date.now() > deadLine && (console.log("RAN OUT OF TIME!"), location.reload())),
- worker.postMessage({ attempt: newAttemptNumber, difficulty: difficulty, salt: hashString }));
- } else {
- console.log(`Work concluded after ${Date.now() - currentUnixTime}ms.\n\t${solution}`);
- finalSolution = solution;
- console.log("\tSubmitting to server...");
- let setPageStatus = function (e) {
- "string" == typeof e
- ? (document.getElementById("status").innerHTML = `<p>${e}</p>`)
- : "error" in e
- ? (console.log(
- `Error received in reply to server:\n\t${e.error}`
- ),
- (document.getElementById("status").innerHTML =
- "<p>Received a client error in reply. Please try again.</p>"))
- : (console.log(`Incomprehensible reply:\n\t${e}`),
- (document.getElementById("status").innerHTML = "<p>Received a server error in reply. This issue may be temporary. Try again.</p>")),
- document.getElementById("loader").setAttribute("class", "paused");
- let t = document.querySelectorAll("#loader circle");
- for (let e = 0; e < t.length; ++e) t[e].style = "opacity: 1 !important; fill: #a66b5e !important;";
- };
- let statusSuccess = function (e) {
- window.localStorage.setItem("sssg_clearance", "PAUSE");
- document.getElementById("status").innerHTML = "<p>Work accepted! Validating session...</p>";
- document.getElementById("loader").setAttribute("class", "paused");
- let t = document.querySelectorAll("#loader circle");
- for (let e = 0; e < t.length; ++e) t[e].style = "fill: #6ba65e !important;";
- var XMLHttpReq1 = new XMLHttpRequest();
- XMLHttpReq1.onreadystatechange = function () {
- if (XMLHttpReq1.readyState === 4)
- try {
- var e = JSON.parse(XMLHttpReq1.response);
- "auth" in e ? reloadPage(e) : setPageStatus(e);
- } catch (e) {
- setPageStatus("Server responded with an error."),
- console.log(`Bad Json: ${XMLHttpReq1.response}`),
- console.log(e);
- }
- }
- XMLHttpReq1.open("POST", "/.sssg/api/check", !0);
- XMLHttpReq1.setRequestHeader(
- "Content-type",
- "application/x-www-form-urlencoded"
- );
- XMLHttpReq1.send(`f=${e.auth}`);
- };
- let reloadPage = function (e) {
- window.localStorage.setItem("sssg_clearance", e.auth);
- document.getElementById("status").innerHTML = "<p>All done! Reloading page...</p>";
- document.getElementById("loader").setAttribute("class", "paused");
- let t = document.querySelectorAll("#loader circle");
- for (let e = 0; e < t.length; ++e) t[e].style = "opacity: 1 !important; fill: #6ba65e !important;";
- setTimeout(function () {location.reload();}, 1500);
- };
- var XMLHttpReq2 = new XMLHttpRequest();
- (XMLHttpReq2.onreadystatechange = function () {
- if (4 === XMLHttpReq2.readyState)
- try {
- var e = JSON.parse(XMLHttpReq2.response);
- "auth" in e ? statusSuccess(e) : setPageStatus(e);
- } catch (e) {
- setPageStatus("Server responded with an error."),
- console.log(`Bad Json: ${XMLHttpReq2.response}`),
- console.log(e);
- }
- }),
- XMLHttpReq2.open("POST", "/.sssg/api/answer", !0),
- XMLHttpReq2.setRequestHeader(
- "Content-type",
- "application/x-www-form-urlencoded"
- ),
- XMLHttpReq2.send(`a=${hashString}&b=${attempt}`);
- }
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement