Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can i build a perl application as follows :

Description:
Need a small application that works like this: Say I have my Netscape or IE browser open on a webpage; after opening this application, I would be able to move the cursor over the webpage and see the text contents (maybe a word at time or a sentence at a time)duplicated in a small window near the bottom of the screen. As the the cursor is moved over the contents of the webpage, the contents in the small window changes too, either by the word or by the sentence.

20030720 Edit by Corion: Changed title from 'Perl application'

  • Comment on Perl application to read the current browser page

Replies are listed 'Best First'.
Re: Perl application to read the current browser page
by Bilbo (Pilgrim) on Jul 18, 2003 at 10:56 UTC

    You should probably look at Javascript rather than Perl. Perl programs associated with websites are (almost?) always CGI programs which run on the web server, not on the computer used to view the site, so they know nothing about cursor positions and the like, and only run when the page is loaded. Javascript runs in the browser used to view the site. Bear in mind that quite a few people have javascript turnd off in their browsers, and won't see whatever it is you are trying to do, so make sure that the information is available in some other way.

    Update: after reading BrowserUK's post below I wonder if it was me who mis-interpreted what you are trying to do. Are you trying to write a website which will do this in people's browsers when they look at it or are you trying to write a program for use on your computer which you will use when you look at other people's websites?

      As well as JavaScript you probably want to have a look at these links for DHTML (Dynamic HTML).
      SO do u say that this application can't be done in perl at all.
        I won't say it is impossible.

        I will say that if your Perl is running on a Unix server and you are trying to get that fine of control over a remote Windows machine then one of three things should be true:

        1. Your client-side logic is running on your client's machine (probably in Java or JavaScript).
        2. Your client is running a custom application which gives you more power over them than a browser provides.
        3. You have broken into your client's machine using techniques which their system administrator should not be happy about.
        These are listed in increasing order of difficulty. Perl could be used to do any of them, but is not necessarily the most appropriate language to use. Implementing any of them requires knowledge of how to manipulate the client-side which is highly OT for this site.
Re: Perl application to read the current browser page
by BrowserUk (Patriarch) on Jul 18, 2003 at 13:27 UTC

    Assuming that you are on a Win32 platform (does IE still run on Macs?), this could be done from perl.

    It would not be easy, and it would require either a pretty goood understanding of the Win32 APIs or a lot of research and perseverance.

    The best starting point would be to look at Win32::GuiTest. Look into the source as the POD as displayed on cpan does not mention most of the functionality available within this module. The particular part of the source that is interesting is the @EXPORT_OK section that lists all the APIs that can be exported by the module. It begins.

    @EXPORT_OK = qw( $debug ClientToScreen FindWindowLike GetChildDepth GetChildWindows GetClassName GetCursorPos GetDesktopWindow GetComboText GetComboContents GetListText GetListContents GetParent GetScreenRes GetWindow GetWindowID GetWindowLong GetWindowRect GetWindowText

    Most of these function are undocumented in the pod from what I can see, but if you use an interactive debugger session you can explore the apis listed in the source and get a (brief) usage for them as shown here.

    perl -de1 C:\test>perl -de1 Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(-e:1): 1 DB<1> use Win32::GuiTest qw[:ALL :SW] DB<5> print GetForegroundWindow( 'A totally meaningless parameter' ) Usage: Win32::GuiTest::GetForegroundWindow() at (eval 8)[E:/Perl/lib/p +erl5db.pl:1521] line 2. DB<7> $f = GetForegroundWindow() DB<8> print $f 346162044 DB<10> GetWindowText() Usage: Win32::GuiTest::GetWindowText(hwnd) at (eval 13)[E:/Perl/lib/pe +rl5db.pl:1521] line 2. DB<12> print GetWindowText( $f ) Command Prompt - perl5.6.1 -de1

    If any of this makes sense to you and you wish to pursue this course, get that far and come back with any specific qustions you have and I'll try to help further.

    Good luck.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

        Maybe I am mis-interpreting the OP, but I don't see any mention of "across the network" in his post?

        The impression I got from the post was that the small window was at the bottom of the screen on the same machine as the web browser was running?

        Now, if the OP meant that he wants the text replicated in a small textfield or at the bottom of the browser window then maybe I misinterpreted him... but if thats so, a clearer description would have been nice.

        He also says "Say I have my Netscape or IE browser open on a webpage; after opening this application..." which further reenforces my interpretation that this is an application intended to run on the same machine as the browser window, not something used remotely...but hey, I've been known to misinterpret people before:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      So a year later...

      I see no evidence that "GetComboText" or "GetComboContents" exist at all. Attempting to use either yields
      "Undefined subroutine &main::GetComboText(Contents) called at..."


      Any suggestions would be welcome.

        They do exist. vis.

        P:\test>type 275606.pl #! perl -slw use strict; use Win32::GuiTest qw[ GetComboText GetComboContents ]; eval{ GetComboText() } or print $@; eval{ GetComboContents() } or print $@; P:\test>275606 Usage: Win32::GuiTest::GetComboText(hwnd, index) at P:\test\275606.pl +line 5. Usage: Win32::GuiTest::GetComboContents(hWnd) at P:\test\275606.pl lin +e 6.

        Though the documentation for them seems to be completely absent. Looking through guitest.xs, GetComboText() & GetComboContents() map to a calls to GetTextHelper()

        SV* GetComboText(hwnd, index) HWND hwnd; int index CODE: RETVAL = GetTextHelper(hwnd, index, CB_GETLBTEXTLEN, CB_GETLBT +EXT); OUTPUT: RETVAL SV* GetComboText(hwnd, index) HWND hwnd; int index CODE: RETVAL = GetTextHelper(hwnd, index, CB_GETLBTEXTLEN, CB_GETLBT +EXT); OUTPUT: RETVAL

        Which in turn gets mapped to calls to SendMessage().

        SV* GetTextHelper(HWND hwnd, int index, UINT lenmsg, UINT textmsg) { SV* sv = 0; int len = SendMessage(hwnd, lenmsg, index, 0L); char* text = (char*)safemalloc(len+1); if (text != 0) { SendMessage(hwnd, textmsg, index, (LPARAM)text); sv = newSVpv(text, len); safefree(text); } return sv; }

        So, it would be a case of fightling your way through the MSDN documentation relating to ComboBoxes to work out what and how and why, which the module author must have done to bring you the module in the first place. All of which reenforces my opening comments above:

        It would not be easy, and it would require either a pretty goood understanding of the Win32 APIs or a lot of research and perseverance.

        It all comes down to how desperately you need to do this.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      I need the application to be running on Unix platform . Please let me know if this can be done on Unix platform using Perl . Thankyou !

        My knowledge of Unix is far to old and out-of-date to be of any use with this. Does IE even run on unix platforms?

        In any case, the description you have provided is far from clear. You give a very breif description a replicating bits of text from a browser window into another window as the mouse moves over the former, and you say you want to do this is perl. What is absent from your description is why you are trying to do this.

        I am reminded of a project I was involved in a few years ago. It was a screen reader/speech synthesizer to allow vision impaired users to navigate/read a GUI desktop (the OS/2 Presentation Manager in that case). My contribution was adding the hooks into the guts of the OS to allow an add-on application to track the cursor and access the text beneath it. I can tell you that it required some highly optimised C/assembler code to make this happen in real time, and that was just tracking the cursor and text under it. The speech synthesis was performed by an external dedicated processor. Whilst processors are nearly a hundred times more powerful now, I still think that it is unlikely that it would be usuable if written in a partially interpreted language like perl even now.

        If you provided a fuller explanation of what you are trying to achieve rather than what you think is the way to achieve it, you may well get some good suggestion from the wide range of able and willing unix guys assembled here.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      Hi , I need some more explanation on what u suggested . Perhaps a few more detailed expalnation . Also how can i incorporate Win API in perl on Unix ??? Thankyou !

        You can't. The possibility I spoke of would only be possible if your platform was Win32.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller