# Insert web content dynamically (Javascript?)

This year in my field we have a large conference overlapping with the European soccer championship. Luckily my talk does not overlap with any match, but in case it will happen in the future I was wondering if it is possible to include in a beamer presentation some Javascript magic that fetches the live results from the internet and displays them in the bottom bar.

I have researched and seen some examples of embedding JS in TeX, but everything seems to be only functions that do some simple stuff when the document is opened or when a PDF form is submitted. In particular, my questions at this point are:

1. can I run AJAX requests inside a PDF, or are the security restrictions too tight?

2. how do I embed code that gets called every X seconds, or whenever a new slide is presented?

3. which packages would make my life easier in programming this?

-

Package media9 is an option.

The first example displays latest FIFA news in a Flash-based RSS-Reader I found on the web. It is embedded in the footline of every slide in a beamer presentation. It doesn't seem to update the content, though. However, it is reloaded (with updated content) when moving to the next presentation slide. Use next example's approach to force reloading at a given time interval.

\documentclass{beamer}
\usepackage{media9}

\setbeamertemplate{footline}{
\includemedia[
width=\paperwidth,height=0.1\linewidth,
activate=pageopen, deactivate=pageclose,
url
}

\begin{document}
\begin{frame}{Frame title}
Please concentrate on the slide content!
\end{frame}
\end{document}


The second example loads an online chart image. It is displayed by StrobeMediaPlayback.swf shipping with media9. The image is updated in regular intervals of 10 s by repeatedly unloading and loading StrobeMediaPlayback.swf. Needs pdflatex for compilation.

\documentclass{beamer}
\usepackage{media9}

\begin{document}
\begin{frame}{nasdaq}

\includemedia[
label=nasdaq,
width=0.46\linewidth,height=0.2\linewidth,
activate=pageopen,
flashvars={
src=http://content.nasdaq.com/graphs/HOMEIXIC.gif?522249
&controlBarMode=none
},
]{}{StrobeMediaPlayback.swf}%
\pdfannot width 0pt height 0pt depth 0pt {
/Subtype/Widget
/AA <</PO <<  %start updating routine on 'pageopen' event
/S/JavaScript/JS (
try{var ret=app.setInterval('
annotRM["nasdaq"].activated=false;
annotRM["nasdaq"].activated=true;
' , 10000);}catch(e){}
)
>>>>
}

\end{frame}
\end{document}

-

One option is to use Lua to fetch data when the file is compiled. Then, set a batch script to recompile the TeX file every 30 seconds or so, and use a pdf reader that auto-refreshes the pdf file.

Here is an example (in ConTeXt) that fetches the current date and time at a specific location from a webserver, parses the returned XML file, and displays the result.

\enabledirectives[schemes.threshold=10] % Don't cache downloaded file for more than 10 sec

% I use earth tools as an example, which returns data in XML format.
\startluacode
thirddata = thirddata or {}
thirddata.url = "http://www.earthtools.org/timezone-1.1/40.71417/-74.00639"

function thirddata.getcurrenttime()
-- Fetch remote url and store it in a file. ConTeXt automatically caches the
-- file for a duration given by schemes.threshold

-- Read the file for data

-- Parse XML data.to find localtime
local time = xml.text(xmldata, "xml://timezone/localtime")
print("DEBIG:", time)

return time
end
\stopluacode

\def\getCurrentTime{\ctxlua{context(thirddata.getcurrenttime())}}

\starttext
The current time is New York is \getCurrentTime.

\stoptext


Of course, if you are running a batch script to compile the file, you could as well get the data using any programming language of your choice, but where is the fun in that.

-