Skip to content

Commit 09d00f1

Browse files
authored
Merge pull request #32 from santosh1994/search-headers
set user and device info header in search request
2 parents 8742df6 + a3fe6a7 commit 09d00f1

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

unbxdSearch.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
481481
,searchQueryParam:"q"
482482
,retainbaseParam: false
483483
,baseParams:[]
484+
,requestHeaders: {}
484485
};
485486

486487
jQuery.extend(Unbxd.setSearch.prototype,{
@@ -1159,7 +1160,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11591160

11601161
modifiedCB(data);
11611162
}
1162-
,urlobj = self.url();
1163+
,urlobj = self.url()
1164+
,requestHeaders = jQuery.extend({}, this.getDefaultRequestHeaders(), this.options.requestHeaders);
11631165

11641166
if(doPush){
11651167
var finalquery = this.options.noEncoding ? urlobj.query : this.encode( urlobj.query );
@@ -1185,6 +1187,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11851187
,dataType: "jsonp"
11861188
,jsonp: 'json.wrf'
11871189
,success: cb.bind(self)
1190+
,headers: requestHeaders
11881191
});
11891192
}
11901193
,reset: function(){
@@ -1882,6 +1885,84 @@ var unbxdSearchInit = function(jQuery, Handlebars){
18821885
}
18831886
return t;
18841887
}
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+
}
18851966
});
18861967
};
18871968

0 commit comments

Comments
 (0)