Can anyone explain what this code does?

Discuss other programming languages besides AutoHotkey
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Can anyone explain what this code does?

Post by afe » 06 Oct 2019, 11:33

Hello,

I want to describe it with ahk, but the JS grammar is really confusing. I have to ask for help.
I want to start with n(), but I got stuck here at the beginning.

Code: Select all

	var n = function() {
		var r;
		var s = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (r = document.cookie.match(s)) {
			return unescape(r[2])
		} else {
			return null
		}
	};

n("TEST");
Assume that the value of document.cookie is TEST=xxx;OTHER=x .

Although f(){} can be called with f(x), I don't know who is the "TEST" passed to here, and what is the name in s?

I guess, the intention is to match
/(^| )TEST=([^;]*)(;|$)/
, but the results of the Chrome console verification are different from my guess.



The following is the complete JS code.

Code: Select all

var f = function() {
	var j = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~!@#¥%……&";
	var q = String.fromCharCode;
	var k = function(s) {
		if (s.length < 2) {
			var r = s.charCodeAt(0);
				return r < 128 ? s : r < 2048 ? (q(192 | (r >>> 6)) + q(128 | (r & 63))) : (q(224 | ((r >>> 12) & 15)) + q(128 | ((r >>> 6) & 63)) + q(128 | (r &63)))
		} else {
			var r = 65536 + (s.charCodeAt(0) - 55296) * 1024 + (s.charCodeAt(1) - 56320);
			return (q(240 | ((r >>> 18) & 7)) + q(128 | ((r >>> 12) & 63)) + q(128 | ((r >>> 6) & 63)) + q(128 | (r & 63)))
		}
	};
	var l = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
	var p = function(r) {
		return (r + "" + Math.random()).replace(l, k)
	};
	var h = function(u) {
		var t = [0, 2, 1][u.length % 3];
		var r = u.charCodeAt(0) << 16 | ((u.length > 1 ? u.charCodeAt(1) : 0) << 8) | ((u.length > 2 ? u.charCodeAt(2) : 0));
		var s = [j.charAt(r >>> 18), j.charAt((r >>> 12) & 63), t >= 2 ? "=" : j.charAt((r >>> 6) & 63), t >= 1 ? "=" : j.charAt(r & 63)];
		return s.join("")
	};
	var o = function(r) {
		return r.replace(/[\s\S]{1,3}/g, h)
	};
	var g = function(r) {
		return o(p(new Date().getTime()))
	};
	var m = function(r, s) {
		return !s ? g(String(r)) : g(String(r)).replace(/[+\/]/g, function(t) {
			return t === "+" ? "-" : "_"
		}).replace(/=/g, "")
	};
	var n = function() {
		var r;
		var s = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (r = document.cookie.match(s)) {
			return unescape(r[2])
		} else {
			return null
		}
	};
	return m(n("TEST"))
};
Last edited by afe on 08 Oct 2019, 08:34, edited 1 time in total.

User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Can anyone explain what this code does?

Post by Micromegas » 06 Oct 2019, 13:29

This is not an AHK question. Why don't you ask at the appropriate forum for the language?

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Can anyone explain what this code does?

Post by malcev » 06 Oct 2019, 16:59

If You want to understand how JS works You have to read JS manual.

Code: Select all

outerHtml =
(
<script type="text/javascript">
	var name = "autohotkey";
	var n = function() {
		var r;
		var s = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		alert(s);
		if (r = document.cookie.match(s)) {
			return unescape(r[2])
		} else {
			return null
		}
	};
n("TEST");
</script>
)

oHTML := ComObjCreate("HTMLFile")
oHTML.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">")
oHTML.write(outerHtml)
You can start here:
https://www.w3schools.com/js/

afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: Can anyone explain what this code does?

Post by afe » 08 Oct 2019, 08:42

Thank you! It turns out that this is only part of the code, and name should be a variable.

Post Reply

Return to “Other Programming Languages”