K
K
kevus2018-06-16 21:27:48
JavaScript
kevus, 2018-06-16 21:27:48

What does this piece of code (JS) do?

(function(){var a=function(){this.exit=this.b;this.close=this.close;this.delayCloseButton=this.a};a.prototype.b=function(){window.console&&window.console.log("Exit API: Close requested.")};a.prototype.a=function(e){e=Math.min(e,5);window.console&&window.console.log("Exit API: Close Button will not appear for "+e+" seconds.")};var b=new a,c=["ExitApi"],d=this;
c[0]in d||!d.execScript||d.execScript("var "+c[0]);for(var f;c.length&&(f=c.shift());)c.length||void 0===b?d=d[f]&&d[f]!==Object.prototype[f]?d[f]:d[f]={}:d[f]=b;}).call(this);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavlo Ponomarenko, 2018-06-16
@kevus

The question is far from simple. If we run it through jsbeautifier.org and remove the noise, then the following code remains:

(function(){

function ExitApi () {};
  
ExitApi.prototype.exit = function() {
   console.log("Exit API: Close requested.")
};

ExitApi.prototype.delayCloseButton = function(e) {
   if (e < 5) e = 5;

   console.log("Exit API: Close Button will not appear for " + e + " seconds.")
};

var b = new ExitApi(),
   method = "ExitApi",
   _this = this;

if (!method in _this && _this.execScript) {
  _this.execScript("var " + method);
}

if (!_this[method] || _this[method] === Object.prototype[method]) _this[method] = ExitApi;

}).call(this);

_this in this case is most likely the window object or something similar.
If we leave only the essence, then it is approximately as follows:
(function(){

function Foo () {};
  
Foo.prototype.exit = function() {
   console.log("Exit API: Close requested.")
};

Foo.prototype.delayCloseButton = function(e) {
   if (e < 5) e = 5;

   console.log("Exit API: Close Button will not appear for " + e + " seconds.")
};

this.ExitApi = new Foo();

}).call(this);

That is, it creates an instance that has two methods that exclusively write to the console and do not perform any task. There is no point in this code yet, it looks like they tried to confuse it on purpose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question