Skip to content

Instantly share code, notes, and snippets.

@nordineb
Created January 28, 2025 17:09
Show Gist options
  • Save nordineb/1c38f95eb948236cf188b281c9be1d7d to your computer and use it in GitHub Desktop.
Save nordineb/1c38f95eb948236cf188b281c9be1d7d to your computer and use it in GitHub Desktop.
import cookies

export cookies with https://github.com/hrdl-github/cookies-txt

import cookies with

function importCookies(cookieFileContent) {
    // Split the content into lines
    const lines = cookieFileContent.split('\n');
    
    // Filter out comments and empty lines
    const cookieLines = lines.filter(line => 
        line.trim() && !line.startsWith('#')
    );

    // Process each cookie line
    cookieLines.forEach(line => {
        const parts = line.split('\t');
        if (parts.length >= 6) {
            const [domain, , path, secure, , name, value] = parts;
            
            // Create cookie string
            const cookieString = `${name}=${value}; path=${path}; domain=${domain}${secure.toLowerCase() === 'true' ? '; secure' : ''}`;
            
            try {
                document.cookie = cookieString;
                console.log(`Attempting to set cookie: ${cookieString}`);
            } catch (e) {
                console.error(`Failed to set cookie: ${name}`, e);
            }
        }
    });
}

// Your cookie file content
const cookieFileContent = `# Netscape HTTP Cookie File
# https://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.

`;

importCookies(cookieFileContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment