Javascript string decoding logic translated to Python

Question:

The following Javascript code will decode string 91ebf9e9f7a8a2a1a5d1a0a3a7bff2fefc
into an intelligible value:

!function() {
    "use strict";
    function e(e) {
        try {
            if ("undefined" == typeof console)
                return;
            "error"in console ? console.error(e) : console.log(e)
        } catch (e) {}
    }
    function t(e) {
        return d.innerHTML = '<a href="' + e.replace(/"/g, "&quot;") + '"></a>',
        d.childNodes[0].getAttribute("href") || ""
    }
    function r(e, t) {
        var r = e.substr(t, 2);
        return parseInt(r, 16)
    }
    function n(n, c) {
        for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
            var l = r(n, i) ^ a;
            o += String.fromCharCode(l)
        }
        try {
            o = decodeURIComponent(escape(o))
        } catch (u) {
            e(u)
        }
        return t(o)
    }
    function c(t) {
        for (var r = t.querySelectorAll("a"), c = 0; c < r.length; c++)
            try {
                var o = r[c]
                  , a = o.href.indexOf(l);
                a > -1 && (o.href = "mailto:" + n(o.href, a + l.length))
            } catch (i) {
                e(i)
            }
    }
    function o(t) {
        for (var r = t.querySelectorAll(u), c = 0; c < r.length; c++)
            try {
                var o = r[c]
                  , a = o.parentNode
                  , i = o.getAttribute(f);
                if (i) {
                    var l = n(i, 0)
                      , d = document.createTextNode(l);
                    a.replaceChild(d, o)
                }
            } catch (h) {
                e(h)
            }
    }
    function a(t) {
        for (var r = t.querySelectorAll("template"), n = 0; n < r.length; n++)
            try {
                i(r[n].content)
            } catch (c) {
                e(c)
            }
    }
    function i(t) {
        try {
            c(t),
            o(t),
            a(t)
        } catch (r) {
            e(r)
        }
    }
    var l = "/cdn-cgi/l/email-protection#"
      , u = ".__cf_email__"
      , f = "data-cfemail"
      , d = document.createElement("div");
    i(document),
    function() {
        var e = document.currentScript || document.scripts[document.scripts.length - 1];
        e.parentNode.removeChild(e)
    }()
}();

Can anyone explain the actual logic of it, to be able to write it into Python? Or come up with an JS2PY solution of running the code against python-selected values?

Asked By: Barry the Platipus

||

Answers:

You can use js2py module to rewrite automatically the Js code to Python:

import js2py

js_script = """
  function decode(email) {
      function r(e, t) {
        var r = e.substr(t, 2);
        return parseInt(r, 16);
      }

      function n(n, c) {
        for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
          var l = r(n, i) ^ a;
          o += String.fromCharCode(l);
        }
        return o;
      }

    var l = "/cdn-cgi/l/email-protection#";
    return n(email, email.indexOf(l) + l.length);
  }
"""

decoder = js2py.eval_js(js_script)
email = decoder(
    "https://journals.sagepub.com/cdn-cgi/l/email-protection#7c0614041a454f4c483c4d4e4a521f1311431f1f414d454c454c494b4a4d3c0d0d521f1311"
)
print(email)

Prints your email.

Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.