Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How can I tell a browser to refresh from perl?

by Anonymous Monk
on Feb 10, 2017 at 15:50 UTC ( [id://1181684]=perlquestion: print w/replies, xml ) Need Help??

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

I have a perl text that from time to time rewrites a .html file. How can I get the browser to redisplay the file? It can refresh every N seconds, but I'd like it faster and more efficient (browsers flicker upon refresh). I'm doing this:

system("/usr/bin/osascript reload.scpt");
. . . where reload.scpt is a trivial Applescript:
tell application "Google Chrome" reload active tab of window 1 end tell

But it annoys me to have to fork a new process each time. Is there a better way to do it? There used to be interfaces from perl directly to Applescript, but I can't get Mac::OSA::Simple to work these days. Can it be done in some non-applescript way? Or maybe the browser can watch for changes in the file, if I can put it where the browser can get at it?

Replies are listed 'Best First'.
Re: How can I tell a browser to refresh from perl?
by haukex (Archbishop) on Feb 10, 2017 at 16:53 UTC

    Hi Anonymous,

    Is this HTML file on a web server, or are you accessing it via a file:// URL? If it were on a web server, there would be ways to do it with AJAX requests. If not, and this is all just on a single local machine with a file:// URL, then because I haven't worked on a Mac in a while I can't tell you if there's a better way, perhaps a Monk more versed in that can help (have you tried Mac::AppleScript?). But if it works, maybe it doesn't need fixing ;-)

    Regards,
    -- Hauke D

      All on a local machine. And I can't get any perl-to-AppleScript module to work. In any case, Mac::Applescript is no good because it compiles the script every time, and so is less efficient than my current solution! At least Mac::OSA::Simple, if it worked, would execute a precompiled and even preloaded script. I was really hoping for some non-AppleScript solution.
Re: How can I tell a browser to refresh from perl?
by Corion (Patriarch) on Feb 11, 2017 at 10:59 UTC

    You don't tell us which browser you're using. Maybe WWW::Mechanize::Firefox can help, especially the ->reload method.

Re: How can I tell a browser to refresh from perl?
by poj (Abbot) on Feb 10, 2017 at 17:31 UTC

    You could perhaps try adding this meta tag to the html

    <meta http-equiv="refresh" content="2"/>
    poj
      As the third sentence of my original post says, I know that I can have the browser periodically refresh. This is not what I'm looking for. The browser will refresh every N seconds (N=2 in your case) with a flicker, but the file updates only every few minutes. I don't want the browser to poll, I want to tell the browser when it's time.
Re: How can I tell a browser to refresh from perl?
by LanX (Saint) on Feb 11, 2017 at 09:55 UTC
    Here a JS solution:
    setTimeout( function () { window.location.reload(true) }, 60000);

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      As the third sentence of my original post says, I know that I can have the browser periodically refresh. This is not what I'm looking for. The browser will refresh every N seconds (N=60 in your case, meaning a long wait from when the file updates to when we see it) with a flicker, but the file updates only every few minutes. I don't want the browser to poll, I want to tell the browser when it's time.
        > As the third sentence of my original post says,

        And as another sentence said

        >  Is there a better way to do it? 

        JS doesn't spawn a process and doesn't depend on AppleScript or OS or browser at all.

        You can also use JS in a hidden iframe checking in short frequency if something changed, before conditionally reloading the parent frame. (Hence no "flickering")

        But that's becoming OT here, Google is your friend.

        I know newer technologies like WebSockets allow to push content, but IIRC this will need a server keeping the connection alive.

        See http://en.wikipedia.org/wiki/Push_technology for various options

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: How can I tell a browser to refresh from perl?
by Anonymous Monk on Feb 11, 2017 at 10:16 UTC

    Can it be done in some non-applescript way? Or maybe the browser can watch for changes in the file, if I can put it where the browser can get at it?

    see Server-sent events

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1181684]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found