Skip to content

Commit fcf83fe

Browse files
committed
Merge pull request #70 from spiralx/master
Cookie string parsing fixes
2 parents c8816d1 + df1b7f3 commit fcf83fe

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

snippets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@
7474
},
7575
{
7676
"name": "viewcookies",
77-
"content": "// viewcookies.js\n// https://github.com/bgrins/devtools-snippets\n// Shows all cookies stored in document.cookies in a console.table\n\n(function() {\n window.viewCookies = function() {\n var rawCookies = document.cookie.split(';'), cookies = [];\n rawCookies.forEach(function(cookie) {\n var parsedCookie = cookie.split('='), cookieData = {};\n cookies.push({\n 'key': parsedCookie.shift(),\n 'value': decodeURIComponent(parsedCookie.join('='))\n });\n });\n console.table(cookies);\n };\n})();\n\nwindow.viewCookies();\n"
77+
"content": "// viewcookies.js\n// https://github.com/bgrins/devtools-snippets\n// Shows all cookies stored in document.cookies in a console.table\n\n(function() {\n 'use strict';\n\n window.viewCookies = function() {\n if (document.cookie) {\n const cookies = document.cookie\n .split(/; ?/)\n .map(s => {\n const [ , key, value ] = s.match(/^(.*?)=(.*)$/);\n return {\n key,\n value: decodeURIComponent(value)\n };\n });\n\n console.table(cookies);\n }\n else {\n console.warn('document.cookie is empty!');\n }\n };\n})();\n\nwindow.viewCookies();\n"
7878
},
7979
{
8080
"name": "wrapelement",
8181
"content": "// wrapelement.js\n// https://github.com/bgrins/devtools-snippets\n// Wrap a given element in a given type of element\n// wrapElement('.foo', 'h1');\n// wrapElement(document.querySelector('#bar'), 'div');\n//\n// LICENSE: [MIT](http://gkatsev.mit-license.org)\n\n(function() {\n window.wrapElement = function(el, whatToWrapIn) {\n var newParent = document.createElement(whatToWrapIn),\n oldParent,\n nextSibling;\n\n if (typeof el === 'string') {\n el = document.querySelector(el);\n }\n\n oldParent = el.parentNode;\n nextSibling = el.nextSibling;\n newParent.appendChild(el);\n if (nextSibling) {\n oldParent.insertBefore(newParent, nextSibling);\n } else {\n oldParent.appendChild(newParent);\n }\n }\n\n})();\n"
8282
}
8383
]
84-
}
84+
}

snippets/viewcookies/viewcookies.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
// Shows all cookies stored in document.cookies in a console.table
44

55
(function() {
6+
'use strict';
7+
68
window.viewCookies = function() {
7-
var rawCookies = document.cookie.split(';'), cookies = [];
8-
rawCookies.forEach(function(cookie) {
9-
var parsedCookie = cookie.split('='), cookieData = {};
10-
cookies.push({
11-
'key': parsedCookie.shift(),
12-
'value': decodeURIComponent(parsedCookie.join('='))
13-
});
14-
});
15-
console.table(cookies);
9+
if (document.cookie) {
10+
const cookies = document.cookie
11+
.split(/; ?/)
12+
.map(s => {
13+
const [ , key, value ] = s.match(/^(.*?)=(.*)$/);
14+
return {
15+
key,
16+
value: decodeURIComponent(value)
17+
};
18+
});
19+
20+
console.table(cookies);
21+
}
22+
else {
23+
console.warn('document.cookie is empty!');
24+
}
1625
};
1726
})();
1827

0 commit comments

Comments
 (0)