-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
71 lines (61 loc) · 2.34 KB
/
basic-usage.ts
File metadata and controls
71 lines (61 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bun
import { homedir } from 'node:os';
import { join } from 'node:path';
import { parseBinaryCookies } from '../src/index';
/**
* Example: Extract Safari cookies from macOS
*/
async function extractSafariCookies() {
try {
// Safari cookies location on macOS
const cookiesPath = join(
homedir(),
'Library/Cookies/Cookies.binarycookies'
);
console.log(`📁 Parsing: ${cookiesPath}\n`);
// Parse the binary cookies file
const cookies = await parseBinaryCookies(cookiesPath);
console.log(`✅ Found ${cookies.length} cookies\n`);
// Display first 5 cookies
console.log('🔍 First 5 cookies:');
cookies.slice(0, 5).forEach((cookie, i) => {
console.log(` ${i + 1}. ${cookie.name}`);
console.log(` Domain: ${cookie.url}`);
console.log(` Value: ${cookie.value.slice(0, 30)}...`);
console.log(` Expires: ${cookie.expiration.toISOString()}`);
console.log(` Flags: ${cookie.flags} (${getFlagsString(cookie.flags)})`);
console.log();
});
// Filter cookies by domain
const googleCookies = cookies.filter(c => c.url.includes('google.com'));
console.log(`\n🔍 Google cookies: ${googleCookies.length}`);
// Convert to Playwright format
const playwrightCookies = cookies.slice(0, 10).map(c => ({
name: c.name,
value: c.value,
domain: c.url,
path: c.path,
expires: Math.floor(c.expiration.getTime() / 1000),
httpOnly: !!(c.flags & 4),
secure: !!(c.flags & 1),
sameSite: 'Lax' as const
}));
console.log('\n📋 Playwright format (first 10):');
console.log(JSON.stringify(playwrightCookies, null, 2));
} catch (error: any) {
console.error(`❌ Error: ${error.message}`);
console.log('\n💡 Make sure Safari has been run at least once on this system');
}
}
/**
* Convert cookie flags to human-readable string
*/
function getFlagsString(flags: number): string {
if (flags === 0) return 'none';
if (flags === 1) return 'secure';
if (flags === 4) return 'httpOnly';
if (flags === 5) return 'secure + httpOnly';
return `custom (${flags})`;
}
// Run example
extractSafariCookies();