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

I'm just hacking a little HTML demo where I want my browser to reload the page after running my script.

Back in the time on *u*x it was very easy to send remote commands to netscape/firefox to open an URL.

But now I have to work on Win where the -remote option doesn't work and FF has shut down some interfaces, which results in opening many new tabs or warning that FF is already running.

My search revealed that Win32::OLE should do the trick of sending an F5 to the browser, alas I could only find examples in VBS and dunno how to translate them to Win32::OLE.

see https://stackoverflow.com/questions/4680109/how-to-reload-refresh-a-web-page-without-leaving-my-web-development-ide

I'm at the point where hacking the dev environment costs me more than the main project.

Could someone please point me to a working example to send a reload to FF and possible other browsers?

DISCLAIMER: I have the feeling we discussed this before, but a search for lanx and OLE was not successfull.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Reload Browser Tab on Windows
by Corion (Patriarch) on Dec 21, 2020 at 20:57 UTC

    It's easy to automate in two ways:

    The cross-browser way would be to serve the HTML through a local webserver and inject a small websocket script into it that calls back to the server and leans when the HTML or CSS get changed. See Mojolicious::Plugin::AutoReload or my App::Mojo::AssetReloader.

    The Chromium/Chrome/Edge-only way would be with WWW::Mechanize::Chrome and Filesys::Notify::Simple, reloading the page whenever it needs to be reloaded. This has the slim advantage of not modifying the HTML page.

      yeah, sure. I also hacked a JS-snippet 15+ years ago that did a reload based on the change time of a local file. :)

      But a little help with Win32::OLE would be nice. I'm also interested to move windows to have my IDE and Browsers side by side, depending on screen resolution.

      Though looks like I can do it with powershell.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        WWW::Mechanize::Chrome can move and resize the browser window as well:

        $mech->target->send_message('Browser.setWindowBounds', windowId => $window_info->{windowId}, bounds => { 'height' => 1600, 'top' => 0, 'width' => 2560, 'left' => 0, }, )->get;
Re: Reload Browser Tab on Windows
by pryrt (Abbot) on Dec 21, 2020 at 21:31 UTC
    Instead of Win32::OLE, have you considered Win32::GuiTest -- it is a module specifically designed for remote-running Windows apps: clicking buttons, activating menus, typing keys.

    Otherwise, taking a look at the code from the first answer to the SO post you linked,

    Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.AppActivate("Firefox") WshShell.SendKeys "{F5}" WshShell.AppActivate("TextPad")

    I think that the CreateObject would be equivalent to the Win32::OLE->new('WScript.Shell'), and that the methods should translate across... but I haven't tried.

    I'll try:

    perl -MWin32::OLE -e "$obj = Win32::OLE->new('WScript.Shell') or die $ +!; $obj->AppActivate('Chrome') or die $!; $obj->SendKeys('{F5}');"

    Yep, that refreshed the current page in Chrome for me.

    Hopefully, that gives you enough to get started with.

      Since I recommended Win32::GuiTest, I should have given equivalent example using that module, too:

      C:\usr\local\share>perl -MWin32::GuiTest=":FUNC" -le "my ($chrome) = F +indWindowLike(0, 'Google Chrome$'); print $chrome; print GetWindowTex +t($chrome); SetForegroundWindow($chrome);SendKeys('{F5}')"

      That also refreshes the current Chrome window for me.

      > Instead of Win32::OLE, have you considered Win32::GuiTest -- it is a module specifically designed for remote-running Windows apps: clicking buttons, activating menus, typing keys.

      Many thanks, I forgot about it, especially FindWindowLike will help me identifying the right instance of FF.

      :)

      > Otherwise, taking a look at the code from the first answer to the SO post you linked,

      I have to admit, that's what I'm using meanwhile via Powershell.

      Less dependencies! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Reload Browser Tab on Windows
by haukex (Archbishop) on Dec 21, 2020 at 20:55 UTC
    I'm just hacking a little HTML demo where I want my browser to reload the page after running my script.

    What kind of a script? Can't you use JS's window.location.reload();?

      > What kind of a script?

      Sorry Script= Perl Program

      This is not JS-Monks after all! ;-)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Sorry Script= Perl Program
        This is not JS-Monks after all! ;-)

        Sure, I just thought you might be playing with WebPerl ;-) I'm a little unclear on your architecture: would I be correct in guessing that you've got a Perl script that's generating static HTML, and that's why you want to reload the browser from the Perl script instead of from inside the HTML? (And even if that's the case, why not just whip up a quick server that serves the HTML on a local address? That way you could solve this using JS / Websockets / etc.)