10 examples of 'puppeteer set cookie' in JavaScript

Every line of 'puppeteer set cookie' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
58_setCookie(cookie, callback) {
59 // phantomJS expects cookie domains to start with a dot
60 let domain = cookie.domain
61 if ((domain.trim().toLowerCase().indexOf("http://") === 0) || (domain.trim().toLowerCase().indexOf("https://") === 0)) {
62 callback(`setCookie: cannot set cookie "${cookie.name}": specify a domain instead of an URL (${domain})`)
63 return
64 }
65 if (domain[0] !== ".") {
66 domain = `.${domain}`
67 }
68 const c = {
69 name: cookie.name,
70 value: cookie.value,
71 domain: domain,
72 path: "/",
73 expires: (Date.now() + (365 * 24 * 60 * 60 * 1000))
74 }
75 if (_.has(cookie, "secure")) {
76 c.secure = cookie.secure
77 }
78 if (_.has(cookie, "httpOnly")) {
79 c.httponly = cookie.httpOnly // yes, lower-case o
80 }
81 try {
82 // try-catch just in case...
83 var ret = phantom.addCookie(c)
84 } catch (e) {
85 callback(`setCookie: failed to add cookie "${cookie.name}": ${e.toString()}`)
86 return
87 }
88 if (ret) {
89 callback(null)
90 } else {
91 callback(`setCookie: failed to add cookie "${cookie.name}"`)
92 }
93}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

41_deleteCookie(name, domain, callback) {
42 // phantomJS completely ignores the domain parameter
43 // and deletes cookies only based on their names
44 try {
45 // try-catch just in case...
46 var ret = phantom.deleteCookie(name)
47 } catch(e) {
48 callback(`deleteCookie: failed to delete cookie "${cookie.name}": ${e.toString()}`)
49 return
50 }
51 if (ret) {
52 callback(null)
53 } else {
54 callback(`deleteCookie: failed to delete cookie "${cookie.name}"`)
55 }
56}
35setCookie( name, value ) {
36 const currentPage = browser.getUrl(),
37 // some cookies might require a valid expiry date.
38 // setting to 1 hour from now.
39 cookieExpiryTime = new Date().getTime() + ( 3600 * 1000 );
40
41 if ( !currentPage.includes( browser.options.baseUrl ) ) {
42 this.open();
43 }
44
45 const cookie = browser.getCookie( name );
46
47 if ( !cookie || cookie.value !== value ) {
48 browser.setCookie( {
49 name: name,
50 value: value,
51 expiry: cookieExpiryTime
52 } );
53
54 browser.refresh();
55 }
56}
3function setCookie (name, value, timeOffset) {
4 var domain = this.options.cookieDomain(),
5 expires = (new Date((new Date()).getTime() + timeOffset)).toUTCString(),
6 cookie = name + '=' + value + '; Expires=' + expires + ';';
7
8 if (domain !== 'localhost') {
9 cookie += ' Path=/; Domain=' + domain + ';';
10 }
11
12 document.cookie = cookie;
13}
323function setCookie(name, value, Hours) {
324 var d = new Date(),
325 offset = 8,
326 utc = d.getTime() + (d.getTimezoneOffset() * 60000),
327 nd = utc + (3600000 * offset),
328 exp = new Date(nd);
329
330 exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
331 document.cookie = name + "=" + decodeURIComponent(value) + ";path=/;expires=" + exp.toGMTString() + ";";
332}
52function setCookie(key, value, exp, path, domain) {
53 if (!(typeof key === 'string' && key.length)) {
54 return; // Key is mandatory
55 }
56 if (typeof value !== 'string') {
57 value = '';
58 } //If value is invalid by default empty string will be set
59 var dt = new Date();
60 if (typeof exp === 'number') {
61 if (exp === Infinity) {
62 dt = new Date('Thu, 31 Dec 2037 00:00:00 GMT');
63 } else {
64 dt.setTime(dt.getTime() + exp * 24 * 60 * 60 * 1000);
65 }
66 }
67 var expires = exp ? '; expires=' + dt.toUTCString() : '',
68 cookiePath = '; path=' + (typeof path === 'string' ? path.trim() : '/'),
69 defaultDomain = window.location.hostname,
70 cookieDomain = '';
71 if (defaultDomain === 'localhost') {
72 // IE does not allow localhost domain
73 if (typeof domain === 'string') {
74 cookieDomain = '; domain=' + domain.trim();
75 }
76 } else {
77 cookieDomain = '; domain=' + (typeof domain === 'string' ? domain.trim() : defaultDomain);
78 }
79
80 var secureCookieFlag = '';
81 if (location.protocol === 'https:') {
82 secureCookieFlag = '; secure';
83 }
84 document.cookie = key + '=' + value + expires + cookieDomain + cookiePath + secureCookieFlag;
85}
149function setCookie(name, value) {
150 document.cookie = name + "=" + escape(value) + "; ";
151}
6setCookie(value: string, params?: any) {
7 this.cookie(this.key, value, params);
8}
315function setCookie(name, value)
316{
317 document.cookie = name + '=' + value + '; Path=/';
318}
172function setCookie(name, value) {
173 document.cookie = name + "=" + value + "; expires=" + new Date(Date.now() + 1000 * 60 * 60 * 24 * 7).toUTCString(); //week
174}

Related snippets