stuppid_bot

urllib.js

Apr 15th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * © All rights reserved x9a.ru, 2013
  3.  */
  4. var queryString = new (function() {
  5.     function parseParam(name, value, params) {
  6.         if (name == '') {
  7.             return false;
  8.         }
  9.        
  10.         var pos = name.indexOf('['), parts = [];
  11.        
  12.         if (pos == 0) {
  13.             return false;
  14.         }
  15.        
  16.         parts[0] = pos != -1 ? name.substr(0, pos) : name;
  17.         parts = pos != -1 ? parts.concat( name.substr(++pos).split('][') ) : parts;
  18.        
  19.         if ( parts.length > 1 ) {  
  20.             parts.push( parts.pop().split(']')[0] );
  21.         }
  22.        
  23.         var part, cur = params, i = 0, len = parts.length;
  24.  
  25.         while (1) {
  26.             part = parts[i];
  27.  
  28.             if ( i && /^(|0|[1-9]\d*)$/.test(part) ) {
  29.                 if (cur[''] === undefined) {
  30.                     part = cur[''] = part * 1;
  31.                 }
  32.                 else if (part == '') {
  33.                     part = ++cur[''];
  34.                 }
  35.                 else if (part > cur['']) {
  36.                     cur[''] = part;
  37.                 }
  38.             }        
  39.  
  40.             if (++i == len) {
  41.                 break;
  42.             }
  43.  
  44.             if (cur[part] === undefined) {
  45.                 cur[part] = {};    
  46.             }                  
  47.  
  48.             cur = cur[part];
  49.         }
  50.  
  51.         cur[part] = decodeURIComponent(value);
  52.     }
  53.    
  54.     function clean(obj) {
  55.         delete obj[''];
  56.        
  57.         for (var i in obj) {
  58.             if (typeof obj[i] == 'object') {
  59.                 clean(obj[i]);
  60.             }
  61.         }
  62.     }
  63.    
  64.     this.parse = function(str, sep, eq) {
  65.         if (typeof str != 'string') {
  66.             return str;
  67.         }
  68.        
  69.         eq = eq || '=';
  70.         var parts = str.split(sep || '&'), i = 0, len = parts.length, params = {};
  71.  
  72.         while (i < len) {
  73.             var part = parts[i++].split(eq);
  74.             parseParam(part[0], part[1], params);
  75.         }
  76.        
  77.         clean(params);
  78.         return params;
  79.     }
  80.    
  81.     this.parseQuery = function() {
  82.         return this.parse( location.search.substr(1) );
  83.     }
  84.    
  85.     function completeParams(params, obj, parts) {      
  86.         for (var i in obj) {  
  87.             if (typeof obj[i] == 'object') {
  88.                 parts.push(i);
  89.                 completeParams(params, obj[i], parts);
  90.                 parts.pop();
  91.                 continue;
  92.             }
  93.            
  94.             var temp = parts.concat(i);
  95.             params.push([temp[0] + ( temp.length > 1 ? '[' + temp.slice(1).join('][') + ']' : '' ), obj[i]]);
  96.         }
  97.     }
  98.    
  99.     this.stringify = function(obj, sep, eq) {
  100.         sep = sep || '&';
  101.         eq = eq || '=';
  102.         var params = [];
  103.         completeParams(params, obj, []);
  104.         var i = 0, len = params.length, out = '';
  105.        
  106.         while (1) {
  107.             out += params[i][0] + eq + encodeURIComponent(params[i][1]);
  108.            
  109.             if (++i == len) {
  110.                 break;
  111.             }
  112.            
  113.             out += sep;
  114.         }
  115.        
  116.         return out;
  117.     }
  118. })();
  119.  
  120. var cookies = queryString.parse(document.cookie, /;\s*/);
  121.  
  122. function setCookie(name, value, expires, path, domain, secure, httponly) {
  123.     document.cookie = name + '=' + encodeURIComponent(value) + (expires === undefined ? '' : '; Expires=' + new Date(expires).toUTCString()) + (path ? '; Path=' + path : '')
  124.         + (domain ? '; Domain=' + domain : '') + (secure ? '; Secure' : '') + (httponly ? '; HttpOnly' : '');
  125. }
  126. // setCookie('test', 'Test', new Date().getTime() + 24 * 3600000);
  127.  
  128. function deleteCookie(name, path, domain) {
  129.     setCookie(name, '', 0, path, domain);
  130. }
  131.  
  132.  
  133. var url = {
  134.     parse: function(str) {
  135.         var matches = /^(?:(\S+):\/\/)?(?:([^:\s]*)(?::(\S*))?@)?(?:([^:/\s]*)(?::(\d*))?)([^?\s]*)?(?:\?([^#\s]*))?(?:#(\S*))?$/.exec(str);
  136.         // console.log(matches);
  137.        
  138.         if (matches !== null) {
  139.             var scheme = ['protocol', 'user', 'password', 'host', 'port', 'path', 'query', 'hash'], i = scheme.length, obj = {};
  140.            
  141.             while (i) {
  142.                 obj[ scheme[i - 1] ] = matches[i--];
  143.             }
  144.            
  145.             obj.query = queryString.parse(obj.query);
  146.             return obj;
  147.         }
  148.        
  149.         return false;
  150.     },
  151.    
  152.     stringify: function(obj) {
  153.         return (obj.protocol ? obj.protocol + '://' : '') + (obj.user ? obj.user + (obj.password ? ':' + obj.password : '') + '@' : '') + (obj.host ? obj.host : '')
  154.             + (obj.port ? ':' + obj.port : '')  + (obj.path ? obj.path : '') + (obj.query ? '?' + queryString.stringify(obj.query) : '') + (obj.hash ? '#' + obj.hash : '')
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment