Topic: Make mechanize use browser cookies/section

I have a script using mechanize to get some web content but recently the page had some changes and the content I want now can only be read if Im logged in. Is there some way to make mechanize use the section that is already open on my browser instead of having to make the loggin from the script?

Re: Make mechanize use browser cookies/section

I was doing exactly this for a couple of years. I used this code to get the cookies from Safari:

cookie = Appscript::app("Safari").documents.get.collect {|d| if d and (durl = d.URL.get) and durl.match("http:.*") and durl.match(domain.to_s) then d.do_JavaScript("document.cookie") end}.compact

(You set 'domain' to a URI for the website you want the cookies for.)

Then I added them to Mechanize like this:

agent.cookie_jar.add(domainURI, cookies)

Except that about a month ago, it suddenly failed. Mechanize's cookie parser is completely broken, and I have no idea why. I didn't change any of my code at all.