AutoHotkey Community

It is currently May 26th, 2012, 9:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Remotely control Firefox
PostPosted: July 29th, 2009, 1:01 am 
Offline

Joined: November 6th, 2005, 6:43 pm
Posts: 72
Here's an example of remotely controlling Firefox. This method lets you control just about anything. This requires the MozRepl Firefox extension to be installed. This script also uses the WS2 library.

Code:
javascript =
( Comments
   // change the proxy
   var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
   prefs.setCharPref("network.proxy.http", "localhost");
   prefs.setIntPref("network.proxy.http_port", 8080);
   prefs.setIntPref("network.proxy.type", 1);      // change it to "Manual Proxy Configuration", in case it wasn't already

   // make the current tab go to a different URL
   content.location.href = "http://www.google.com/search?hl=en&q=recursion";
)


OnExit, CleanUp
Socket := WS2_Connect("localhost:4242")      ; the default port that MozRepl listens on
if ((Socket<=0) || (StrLen(__WSA_ErrMsg)) || (StrLen(Socket)=0)) {
   MsgBox,16,%A_ScriptName%: Error,% __WSA_ErrMsg
   ExitApp
}
WS2_SendData(Socket, javascript)
Sleep,10   ; if we close the socket too soon, the data won't actually get sent  (why?)
WS2_Disconnect(Socket)
ExitApp


CleanUp:
   WS2_CleanUp()
   ExitApp
   return


Last edited by interiot on July 31st, 2009, 8:34 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 3:53 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Nice!. Do you know the FF.au3 library that also uses MozRepl, see
http://english.documentation.ff-au3.tho ... illert.de/ would be
a great starting point for a AHK library (will keep fingers crossed!) 8)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 4:13 pm 
Offline

Joined: November 6th, 2005, 6:43 pm
Posts: 72
Most of what FF.au3 does can be done by sending it the right Javascript string, FF.au3 just provides nice wrappers that generate the correct Javascript. If you want to know what the correct javascript to send is, you can just look inside the specific FF.au3 implementation. (eg. this is the javascript that clears all cookies)

That said, FF.au3 does provide one thing the above script doesn't: It provides a single simple read/write function call that lets you read back the return value of a command you send to MozRepl. That is, right now, my code is a "write-only" interface, you can't read values back (without doing a lot of manual parsing). If someone needs to do a lot of AutoHotkey => MozRepl work, you'd probably want to write a function similar to FFcmd() right away, and have all your other calls go through that.

However, 80% of what you'd want to do with MozRepl is possible with a write-only interface, so this example script should work for many tasks.


Last edited by interiot on July 29th, 2009, 4:24 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 4:24 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
I'm just going to pretend I understood everything you just said :-) (and in the meantime keep my fingers crossed hoping someone who actually does will provide noob friendly AHK functions), but I'll read what you wrote a few times more and maybe it will sink in :wink:

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 4:32 pm 
Offline

Joined: November 6th, 2005, 6:43 pm
Posts: 72
The "write only" commands involve only sending data from AutoHotkey to Firefox. The "read" commands also involve sending data from Firefox back to AutoHotkey.

Examples of "write-only" commands: (my script can do these easily)
  • change the URL of the current tab
  • change the proxy setting
  • change the homepage
  • click on the third link on the current page
Examples of reading values back: (my script can't currently do these, but someone could add this functionality with a little more work)
  • get the URL of the current tab, assign it to an AutoHotkey variable
  • get the current proxy setting, assign it to an AutoHotkey variable
  • get the URL of the current homepage
  • return the URL of the third link on the current page


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 7:58 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
Very nice! Also thanks for the link to the Mozilla controller :D

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 31st, 2009, 8:45 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Excellent!
Quote:
W32 library.
I think you mean WS2. ;)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 31st, 2009, 9:07 am 
Offline

Joined: September 23rd, 2008, 3:37 am
Posts: 49
very very good - thank you.
also a set of AutoHotkey "single simple read/write function calls" would be useful for everyone! But how has the knowledge to do it?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 31st, 2009, 12:58 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
It's not pretty, but it seems to work:
Code:
MsgBox % mozrepl("content.document.title + '\n' + content.location.href")
WS2_CleanUp()
ExitApp

mozrepl(js, url="localhost:4242")
{
    if (s := WS2_Connect(url)) = -1
        return
    WS2_SendData(s, js)  ,  VarSetCapacity(b, 4096)
    Loop
        if (1 > (n := DllCall("ws2_32\recv", "uint", s, "str", b, "int", VarSetCapacity(b), "int", 0))
            || RegExMatch(t .= SubStr(b, 1, n), "s)(?<=repl> ).*?(?=`n?repl> )", r))
            break
    return r  ,  WS2_Disconnect(s)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 9th, 2009, 2:03 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Rudimentary attempt to start a FF MozRepl lib
Code:
; http://www.autohotkey.com/forum/topic46974.html
#Include winsock2.ahk

SetTitleMatchMode 2
WinActivate Firefox
FF_Connect()
FF_OpenUrl("http://www.autohotkey.com/")
FF_AddTab("http://www.autohotkey.com/forum/")
Sleep 1500
FF_CloseTab() ; close current tab
Sleep 1500
FF_DuplicateTab()
FF_DisConnect()
ExitApp

OnExit, CleanUp
CleanUp:
   WS2_CleanUp()
   ExitApp
   Return

   
FF_Connect(){
Global Socket, FF_Connected
FF_Connected = False
Socket := WS2_Connect("localhost:4242")      ; the default port that MozRepl listens on
if ((Socket<=0) || (StrLen(__WSA_ErrMsg)) || (StrLen(Socket)=0)) {
   MsgBox,16,%A_ScriptName%: Error,% __WSA_ErrMsg
   ExitApp
}
FF_Connected = True
Return
}

FF_DisConnect(){
Global Socket
If FF_Connected = False
   Return
WS2_Disconnect(Socket)
}

FF_OpenUrl(url){
Global Socket
If FF_Connected = False
   Return
javascript =
( Comments
   // make the current tab go to a different URL
   content.location.href = "%url%";
)
WS2_SendData(Socket, javascript)
Sleep,10   ; if we close the socket too soon, the data won't actually get sent  (why?)
}

FF_AddTab(url){
Global Socket
If FF_Connected = False
   Return
javascript =
( Comments
   // make the current tab go to a different URL
   gBrowser.selectedTab = gBrowser.addTab("%url%")
   
)
WS2_SendData(Socket, javascript)
Sleep,10   ; if we close the socket too soon, the data won't actually get sent  (why?)
}

FF_CloseTab(){
Global Socket
If FF_Connected = False
   Return
javascript =
( Comments
   // make the current tab go to a different URL
   gBrowser.removeCurrentTab()
)
WS2_SendData(Socket, javascript)
Sleep,10   ; if we close the socket too soon, the data won't actually get sent  (why?)
}

FF_DuplicateTab(){
Global Socket
If FF_Connected = False
   Return
javascript =
( Comments
   // make the current tab go to a different URL
   gBrowser.duplicateTab(gBrowser.mCurrentTab)
)
WS2_SendData(Socket, javascript)
Sleep,10   ; if we close the socket too soon, the data won't actually get sent  (why?)
}

FF_Connect()
FF_DisConnect()
FF_OpenUrl(url)
FF_AddTab(url)
FF_CloseTab()
FF_DuplicateTab()

Very very 0.00001 version with no "intelligence" whatsoever and requires winsock2 to be included

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 2:18 am 
Offline

Joined: November 8th, 2009, 2:46 am
Posts: 234
Location: Canberra Oz
[quote="hugov"]Rudimentary attempt to start a FF MozRepl lib[code];

If you have multiple FF windows can you control which window it connects to?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 1:20 am 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
:shock:
Lexikos & hugov once again get the GodMode award!

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 9:31 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Michael@oz wrote:
If you have multiple FF windows can you control which window it connects to?
I dunno, I can only assume so but don't know how.

@TLM: I fear not, the above took me quite a while to figure out all the JS and won't develop it further because if you look at the AU3 version there is a lot more going on that what I've done and have no clue on how to proceed.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 4th, 2010, 7:46 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
Sorry I meant to say Sarah Silverman getting changed is GhodMode (ie - ass flash :shock: )

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 24th, 2010, 11:57 pm 
You guys have some great information here! I'm pretty new to mozrepl, and I was wondering how I could view firefox downloads ([Ctr]-j) in mozrepl?


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group