bugfixes
[4Free-FSE.git] / src / generic-functions.ts
blob286a86ee7dcd4eb71ad62cc16472f14040d510f7
1 //unassociated functions
2 class Generics{
3         static storageAvailable(storage_type:string):number  {
4                 try {
5                         var storage = window[storage_type];
6                         storage.setItem('x', 'x');
7                         storage.removeItem('x');
8                         return 1;
9                 }
10                 catch(e) {
11                                 return e 
12                 }
13         }
15         //What Browser
16         static detectBrowser():number {
17                 if((navigator.userAgent.indexOf('Opera') || navigator.userAgent.indexOf('OPR')) != -1 )
18                 {
19                         console.log('Opera');
20                         return 0;
21                 }
22                 else if(navigator.userAgent.indexOf('Chrome') != -1 )
23                 {
24                         console.log('Chrome');
25                         return 1;
26                 }
27                 else if(navigator.userAgent.indexOf('Safari') != -1)
28                 {
29                         console.log('Safari');
30                         return 2;
31                 }
32                 else if(navigator.userAgent.indexOf('Firefox') != -1 )
33                 {
34                         console.log('FireFox');
35                         return 3;
36                 }
37                 else if(navigator.userAgent.indexOf('MSIE') != -1 )
38                 {
39                         console.log('IE');
40                         return 4;
41                 }
42                 else
43                 {
44                         console.log('Other');
45                         return -1;
46                 }
47         }
49                 
50         //gets json keys by regex test
51         static getJSONPropertiesByKeyName(JSON_obj:object, regex_string:string):string[]{
52                 var regex:any = new RegExp("^" + regex_string + "$");
53                 var rtnArray:string[] = Array();
54                 for (let key in JSON_obj)
55                         if (regex.test(key))
56                                 rtnArray.push(key);
57                 return rtnArray;
58         }
59         
60         //send alert to 4chanx
61         static alert4ChanX(message:string, type:string, time?:number):void{
62                 var detail:object = {type: type, content: message, lifetime: time};
63                 var event:any = new CustomEvent('CreateNotification', {bubbles: true, detail: detail});
64                 document.dispatchEvent(event);
65         }
66         
67         static getJSON = function(url, callback, extra, ...all_extra) {
68                 var xhr:any = new XMLHttpRequest();
69                 xhr.open('GET', url, true);
70                 xhr.responseType = 'json';
71                 xhr.onload = function() {
72                         var status = xhr.status;
73                         if (status == 200) {
74                                 callback(null, xhr.response, extra, ...all_extra);
75                         } else {
76                                 callback(status);
77                         }
78                 };
79                 xhr.send();
80         };
82