Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling Google Chrome via Perl script

I was looking for a way to check for changes in a specific URL provided by the user. I wrote a script that I ran at home that can do that successfully using WWW::Mechanize. The thing is, I need to run it in another computer network that has a group policy that blocks all $mech->get($url) requests (it's not firewall problems, I let Perl through), so I thought I'll try and work around this problem by letting Perl control the web browser.

The computers in the network run Google Chrome and IE8 only and I can't install Firefox due to another policy (thought of using WWW::Mechanize::Firefox). I don't want to invoke IE8 in the script because most of the URLs that will be provided will be of websites that don't work well with it, so that leaves me with Chrome only.

Searching for a module that can do that, I only found AnyEvent::Chromi:

which exposes all of the Chrome Extensions API via a websocket connection.

This doesn't work (the policy probably blocks that websocket as well).

Is there another way to work around this problem/control Chrome from within a Perl script?

like image 880
yoniyes Avatar asked Sep 14 '16 13:09

yoniyes


2 Answers

Is there another way to work around this problem/control Chrome from within a Perl script?

WWW::Mechanize::Chrome

Like WWW::Mechanize, this module automates web browsing with a Perl object. Fetching and rendering of web pages is delegated to the Chrome (or Chromium) browser by starting an instance of the browser and controlling it with Chrome DevTools.

like image 182
Dr.Avalanche Avatar answered Oct 23 '22 13:10

Dr.Avalanche


You can also do this from scratch without using any CPAN modules. I tested this on Linux (Ubuntu 16.04, Google Chrome version 53) using Unix Domain (UD) sockets. Unfortunately, it seems Windows does not have UD sockets, but it should be possible to do same from Windows using named pipes.

First, make sure google-chrome is running in the background. We will need to create a Chrome App that will communicate with a native host through the native messaging API. The native host can be any script that reads messages from its STDIN and returns replies through its STDOUT. I tested both a Python script and a Perl script.

Now, in order for a standalone Perl script to communicate with the Chrome browser, it will send request through a UD socket (created by the native host) to the native host; the native host will then forward the request to google-chrome through its standard output pipe. Then the Chrome App (written in JavaScript) will receive the request. The Chrome App will use the Chrome JavaScript APIs to get the requested data and return it to the native host. Finally, the native host forwards the result through the socket to the Perl script.

As you can see, there are some details required for setting this up, but I can confirm that it works on my Linux platform. Please let me know if you need more details.

like image 38
Håkon Hægland Avatar answered Oct 23 '22 13:10

Håkon Hægland