File: stats.js

package info (click to toggle)
wot 20151208-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,684 kB
  • ctags: 828
  • sloc: makefile: 8
file content (318 lines) | stat: -rwxr-xr-x 9,275 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var wot_stats = 
{
    utils: {
        serialize: function(obj) 
        {
            var str = [];
            var length = 0;
            for(var p in obj) {
                if (obj.hasOwnProperty(p)) {
                    length++;
                    str.push(p + "=" + obj[p]);
                }
            }
            return {
                data: str.join("&"),
                length:length
            };
        },

        postRequest: function(url, data, length, callback) 
        {
            try {
                var http = new XMLHttpRequest();
                http.open("POST", url, true);
                http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

                http.onreadystatechange = function() {
                    if (http.readyState == 4) {
                        if (http.status == 200) {
                            if (callback) {
                                callback(true, http.responseText);
                            }
                        }
                        else {
                            if (callback) {
                                callback(false, http.responseText);
                            }
                        }
                    }
                };
                http.send(data);          
            }
            catch(e) {
                console.log("postRequest() - error." + e);
            }
        },

        dictionaryToQueryString: function(dict) 
        {
            var result = '';
            for(key in dict) {
                result += key + '=' + dict[key] + '&';
            }
            return result.slice(0, result.length - 1); 
        },

        createRandomString: function (string_size) 
        {
            var text = "";
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            for (var i = 0; i < string_size; i++)
                text += possible.charAt(Math.floor(Math.random() * possible.length));

            return text;
        },

        RESPONSE_RECEIVED: 4,
        getRequest: function(url, callback) 
        {
            try {
                var xmlhttp = new XMLHttpRequest();
                
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == wot_stats.utils.RESPONSE_RECEIVED) {
                        if (xmlhttp.status == 200) {
                            callback(true,xmlhttp.responseText);
                        }
                        else {
                            callback(false, xmlhttp.responseText);
                        }
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }
            catch(e){
                console.log("getRequest() - error. " +e);
            }
        },

        getCurrentTime: function() 
        {
            return new Date().getTime();
        }
    },

    last_prev: "",
    enabled: false,
    statusKey: "ok",
    urlKey: "url",

    load: function()
    {
        try {
            var settings = this.getMonitoringSettings();
            if (settings != null && settings[this.statusKey] == 1) {
                this.startMonitoring();
            }
            this.fetchSettings();
        }
        catch(e) {
            console.log("load() - error." + e);
        }
    },

    isWebURL: function(url) 
    {
        return url.toLowerCase().indexOf("http") == 0;
    },    

    getInstallTime: function() 
    {
        var stats_installtime = wot_prefs.getChar("stats_installtime", "");
        if (stats_installtime === "") {
            wot_prefs.setChar("stats_installtime", this.utils.getCurrentTime());
        }
        return wot_prefs.getChar("stats_installtime", null);
    },

    setMonitoringSettings: function(settings) 
    {
        if (settings) {
          wot_prefs.setChar("stats_settings", settings);
        }
    },

    getMonitoringSettings: function() 
    {
        var stats_settings = wot_prefs.getChar("stats_settings", "");
        if (stats_settings !== "") {
            try {
                var settingsJson = JSON.parse(stats_settings);
                if (typeof settingsJson[this.statusKey] == "undefined" || settingsJson[this.statusKey] == null) {
                    return null;
                }
                if (typeof settingsJson[this.urlKey] == "undefined" || settingsJson[this.urlKey] == null) {
                    return null;
                }
                return settingsJson;
            }
            catch(e) {
                console.log("getMonitoringSettings() - error." + e);
            }
            return null;
        }
        return null;
    },

    startMonitoring: function() 
    {
        this.enabled = true;
    },

    fetchSettings: function() 
    {
        var url = WOT_STATS.URL;
        var data = {
            "s":WOT_STATS.SID,
            "ins":wot_stats.getInstallTime(),
            "ver":WOT_STATS.VER
        };
        var queryString = this.utils.dictionaryToQueryString(data);
        url = url + "?" + queryString;
        this.utils.getRequest(url, this.onSettingsReceived);
    },

    onSettingsReceived: function(status, response) 
    {
        wot_stats.setMonitoringSettings(response);
        var settings = wot_stats.getMonitoringSettings();
        
        if(settings[wot_stats.statusKey] == 1) {
            wot_stats.startMonitoring();
        }
    },

    getUserId: function() 
    {
        var stats_uid = wot_prefs.getChar("stats_uid", "");
        if (stats_uid === "") {
            wot_prefs.setChar("stats_uid", this.utils.createRandomString(32));  
        }
        return wot_prefs.getChar("stats_uid", null);
    },

    getSession: function() 
    {
        var session = wot_prefs.getChar("stats_sess", "");
        if (session === "") {
            session = this.createSession();
            this.saveSession(session);
        }
        else {
            try {
                if (this.isSessionExpired()) {
                    session = this.createSession();
                    this.saveSession(session); 
                } else {
                    return JSON.parse(session);
                }
            }
            catch(e) {
                session = this.createSession();
                this.saveSession(session);
            }
        }
        return session;
    },

    isSessionExpired: function() 
    {
        var oldSession = wot_prefs.getChar("stats_sess", "");
        var currentTime = this.utils.getCurrentTime();

        if (oldSession !== "") {
            var jsonOldSession = JSON.parse(oldSession);
            var oldSessionTs = jsonOldSession['ts'];

            if (typeof oldSessionTs != "undefined" && oldSessionTs && (currentTime - oldSessionTs) < WOT_STATS.ST) {
                return false;
            }
        }
        return true;
    },

    touchSession: function(prev) 
    {
        var session = this.getSession();
        session['ts'] = this.utils.getCurrentTime();
        if (prev) {
            session['prev'] = encodeURIComponent(prev);
        }
        this.saveSession(session);
    },

    saveSession: function(session) 
    {
        wot_prefs.setChar("stats_sess", JSON.stringify(session)); 
    },

    createSession: function() 
    {
        var session = {
            "id" : wot_stats.utils.createRandomString(32),
            "ts" : wot_stats.utils.getCurrentTime(),
            "prev" : encodeURIComponent("")
        };

        session = JSON.stringify(session);
        session = JSON.parse(session);
        return session;
    },

    loc: function(url, ref) 
    {
        if(this.isWebURL(url)) {
            this.query(url, ref);
        }
    },  

    focus: function(url) 
    {
        if(typeof url == "string" && this.isWebURL(url)) {
            this.last_prev = url;
        }
        this.touchSession();
    },  

    query: function(url, ref) 
    {
        if(!this.enabled) {
            return;
        }
        var settings = this.getMonitoringSettings();
        if (this.last_prev === "") {
            this.last_prev = decodeURIComponent(this.getSession()['prev']);
        }
        data = {
            "s":WOT_STATS.SID,
            "md":21,
            "pid":wot_stats.getUserId(),
            "sess":wot_stats.getSession()['id'],
            "q":encodeURIComponent(url),
            "prev":encodeURIComponent(wot_stats.last_prev),
            "link":0,
            "sub": "ff",
            "tmv": WOT_STATS.VER,
            "hreferer" : encodeURIComponent(ref),
            "ts" : wot_stats.utils.getCurrentTime()
        };

        var requestDataInfo = this.utils.serialize(data);
        var requestData = requestDataInfo.data;
        var requestLength = requestDataInfo.length;

        var encoded = btoa(btoa(requestData));        
        if (encoded != "") {
            var data = "e=" + encodeURIComponent(encoded);
            var statsUrl = settings[this.urlKey] + "/valid";
            this.utils.postRequest(statsUrl, data, requestLength);
        }
        this.last_prev = url;      
        this.touchSession(this.last_prev);
    }
};

wot_modules.push({ name: "wot_stats", obj: wot_stats });