Skip to content

Commit 072f16b

Browse files
committed
add support to read cookie
1 parent 2c5e2de commit 072f16b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

unbxdSearch.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,48 @@ var unbxdSearchInit = function(jQuery, Handlebars){
18871887
console.log("Unbxd : " + str);
18881888
}
18891889
}
1890+
,decodeAndParse : function(s) {
1891+
if (s.indexOf('"') === 0) {
1892+
// This is a quoted cookie as according to RFC2068, unescape...
1893+
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
1894+
}
1895+
1896+
return this.decodeCookie(s);
1897+
}
1898+
,decodeCookie : function(s) {
1899+
var pluses = /\+/g;
1900+
return decodeURIComponent(s.replace(pluses, ' '));
1901+
}
1902+
,cookie : function (key) {
1903+
// Read
1904+
var cookies = document.cookie.split('; ');
1905+
var result;
1906+
for (var i = 0, l = cookies.length; i < l; i++) {
1907+
var parts = cookies[i].split('=');
1908+
var name = this.decodeCookie(parts.shift());
1909+
var cookie = parts.join('=');
1910+
1911+
if (key && key === name) {
1912+
try{
1913+
result = this.decodeAndParse(cookie);
1914+
break;
1915+
}catch(e){
1916+
this.log(e);
1917+
}
1918+
}
1919+
}
1920+
1921+
return result;
1922+
}
1923+
,readCookie : function(name){
1924+
try{
1925+
return this.cookie('unbxd.' + name);
1926+
}catch(e){
1927+
this.log(e);
1928+
}
1929+
1930+
return undefined;
1931+
}
18901932
});
18911933
};
18921934

0 commit comments

Comments
 (0)