Pass cookie from script to firefox


I had a requirement to get firefox to open a logged in page in firefox. I used it for automated opening of some logged in pages. But there is another use for it. It can be used for opening mail or any other site that needs to be logged in. The only issue, this method works on sites that relies on cookies only. This is fine as almost all websites run using cookies. So what the script does is, it has the username and passwords. It logs into your account and gets the cookie and passes the cookie to your browser. So when we open the page again in browser it already has valid cookie and there is no need to login. You will go thought to the logged in page and thats it. You just need to run the script and you can make the script to take any number of sites. Currently I have configured only to automate for Yahoo! . It can be easily extended to add any more sites.

Just run the script after configuring your username ,password, profile folder, profile folder path. And just keep in mind to run it when there is no active firefox runnning. And thats it. Here is script:

use strict;
use warnings;

use WWW::Mechanize;
use Crypt::SSLeay; # required for https://
use HTML::Entities;

my %ref;

$ref{user} = 'username';# change this to your username
$ref{pass} = 'password';# change this to your password
$profilespath = '/home/.mozilla/firefox/'; #change to your profile folder path
my $status;
$status = login(%ref);
if ($status) {
	print "Logged in successfullyn";
} else {
	print "Some problem has happened.n";
}

sub login
{
	my ($ref) = (@_);
	my $agentstr = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6';
	my $agent = WWW::Mechanize->new(autocheck => 1, agent => $agentstr);
	my $mail_server = "http://mail.yahoo.com";
	my $user;
	my $pass;
	my $cookie_file;
	my $time;
	my $value;
	my $profilename='';#insert your profile folder name
	$user = $ref->{user};
       	$cookie_file = "./yahoo_cookie_$user.dat";
	$agent->cookie_jar->{ignore_discard} = 1;
	$agent->get($mail_server);
	if ( ($agent->content =~ /https://login.yahoo.com/config/login/)  and
		($agent->content =~ /login_form/)) {
		$agent->form_name("login_form");
		unless ($agent->content =~ /$user/) {
			$agent->field("login", $user);
		}
		$agent->field("passwd", $ref->{pass});
		$agent->submit();
		my $mail_url;
		if ($agent->content =~ /url=([^"]+)/s) {
			$mail_url = $1;
		}
		if (!$mail_url) {
			return 0; # Cannot login
		}
		$agent->cookie_jar->save($cookie_file);
		open FILE, $cookie_file or die $!;
		my $output=`sqlite3 $profilespath$profilename/cookies.sqlite "delete from moz_cookies where name in ('Y','T');"`;
		while(<FILE>)
		{
		 if($_=~/Set-Cookie3: Y=/)
		 {
		 	print $_;
		 	$value=$_;
		 	$value=~ s/Set-Cookie3: Y="(.*)"; path=.*/$1/;
		 	chomp($value);
		 	$time=time()+9*24*60*60;
		 	my $output=`sqlite3 $profilespath$profilename/cookies.sqlite "insert into moz_cookies VALUES(NULL,'Y','$value','.yahoo.com','/',$time,1263117769112180,0,0);"`;
		 	print $output;
		 }
		 if($_=~/Set-Cookie3: T=/)
		 {
		 	print $_;
		 	my $value=$_;
		 	$value=~ s/Set-Cookie3: T="(.*)"; path=.*/$1/;
		 	chomp($value);
		 	$time=time()+9*24*60*60;
		 	my $output=`sqlite3 $profilespath$profilename/cookies.sqlite "insert into moz_cookies VALUES(NULL,'T','$value','.yahoo.com','/',$time,1263117769112180,0,0);"`;
		 	print $output;
		 }
		}
	}
	return 1;
}

Try it out and tell me how it goes.


, , , , , , , , ,

  1. #1 by Pritesh on April 29, 2011 - 11:59 pm

    what language is this script written in? how do I call it from C# code?

(will not be published)


  1. Pass cookie from script to firefox for Yahoo Mail | Firefox Blog
  2. Latest Yahoomail Auctions | wwwyahoo_yahoomail_video_women