@@ -481,6 +481,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
481
481
, searchQueryParam :"q"
482
482
, retainbaseParam : false
483
483
, baseParams :[ ]
484
+ , requestHeaders : { }
484
485
} ;
485
486
486
487
jQuery . extend ( Unbxd . setSearch . prototype , {
@@ -1159,7 +1160,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1159
1160
1160
1161
modifiedCB ( data ) ;
1161
1162
}
1162
- , urlobj = self . url ( ) ;
1163
+ , urlobj = self . url ( )
1164
+ , requestHeaders = jQuery . extend ( { } , this . getDefaultRequestHeaders ( ) , this . options . requestHeaders ) ;
1163
1165
1164
1166
if ( doPush ) {
1165
1167
var finalquery = this . options . noEncoding ? urlobj . query : this . encode ( urlobj . query ) ;
@@ -1185,6 +1187,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1185
1187
, dataType : "jsonp"
1186
1188
, jsonp : 'json.wrf'
1187
1189
, success : cb . bind ( self )
1190
+ , headers : requestHeaders
1188
1191
} ) ;
1189
1192
}
1190
1193
, reset : function ( ) {
@@ -1882,6 +1885,84 @@ var unbxdSearchInit = function(jQuery, Handlebars){
1882
1885
}
1883
1886
return t ;
1884
1887
}
1888
+ , log : function ( str ) {
1889
+ if ( this . readCookie ( "debug" ) === '1' ) {
1890
+ console . log ( "Unbxd : " + str ) ;
1891
+ }
1892
+ }
1893
+ , decodeAndParse : function ( s ) {
1894
+ if ( s . indexOf ( '"' ) === 0 ) {
1895
+ // This is a quoted cookie as according to RFC2068, unescape...
1896
+ s = s . slice ( 1 , - 1 ) . replace ( / \\ " / g, '"' ) . replace ( / \\ \\ / g, '\\' ) ;
1897
+ }
1898
+
1899
+ return this . decodeCookie ( s ) ;
1900
+ }
1901
+ , decodeCookie : function ( s ) {
1902
+ var pluses = / \+ / g;
1903
+ return decodeURIComponent ( s . replace ( pluses , ' ' ) ) ;
1904
+ }
1905
+ , cookie : function ( key ) {
1906
+ // Read
1907
+ var cookies = document . cookie . split ( '; ' ) ;
1908
+ var result ;
1909
+ for ( var i = 0 , l = cookies . length ; i < l ; i ++ ) {
1910
+ var parts = cookies [ i ] . split ( '=' ) ;
1911
+ var name = this . decodeCookie ( parts . shift ( ) ) ;
1912
+ var cookie = parts . join ( '=' ) ;
1913
+
1914
+ if ( key && key === name ) {
1915
+ try {
1916
+ result = this . decodeAndParse ( cookie ) ;
1917
+ break ;
1918
+ } catch ( e ) {
1919
+ this . log ( e ) ;
1920
+ }
1921
+ }
1922
+ }
1923
+
1924
+ return result ;
1925
+ }
1926
+ , readCookie : function ( name ) {
1927
+ try {
1928
+ return this . cookie ( 'unbxd.' + name ) ;
1929
+ } catch ( e ) {
1930
+ this . log ( e ) ;
1931
+ }
1932
+
1933
+ return undefined ;
1934
+ }
1935
+ , getDeviceInfo : function ( ) {
1936
+ var smallDeviceMaxWidth = 768 ,
1937
+ mediumDeviceMaxWidth = 992 ;
1938
+ if ( window . outerWidth < smallDeviceMaxWidth ) {
1939
+ return "Mobile" ;
1940
+ } else if ( window . outerWidth < mediumDeviceMaxWidth ) {
1941
+ return "Tablet" ;
1942
+ } else {
1943
+ return "Desktop" ;
1944
+ }
1945
+ }
1946
+ , getUserType : function ( ) {
1947
+ return this . readCookie ( 'visit' ) === "repeat" ? "repeat" : "new" ;
1948
+ }
1949
+ , getUserId : function ( ) {
1950
+ return this . readCookie ( 'userId' ) ;
1951
+ }
1952
+ , getDefaultRequestHeaders : function ( ) {
1953
+ var self = this ,
1954
+ userId = this . getUserId ( ) ,
1955
+ defaultRequestHeaders = {
1956
+ "Device-Type" : self . getDeviceInfo ( )
1957
+ , "Unbxd-Url" : document . URL
1958
+ , "Unbxd-Referrer" : document . referrer
1959
+ , "User-Type" : self . getUserType ( )
1960
+ } ;
1961
+ if ( userId ) {
1962
+ defaultRequestHeaders [ "User-Id" ] = userId ;
1963
+ }
1964
+ return defaultRequestHeaders ;
1965
+ }
1885
1966
} ) ;
1886
1967
} ;
1887
1968
0 commit comments