4

I have a question about python and javascript. I like to create a file who comminicate with javascript webpage.

First i will create a Python file who will open a local webpage. This example localhost/test.html This page is a themplate page. The code is

<html><head><title>An example application</title></head>
<script>
function myFunction()
{
alert("Hello! I am an alert box!");
}
</script>
    <body>
    <h1 class="content">This is my sample application</h1>
    Put the content here...
    <hr>
    <a href="/exit">Quit</a>
    </body></html>

Whene everything is loaded and displayed the python file will activate the javascript function "myFunction()"

How do I do that? I am running Debian and have installed Python and Chromium.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Wouter van Reeven
  • 517
  • 3
  • 8
  • 16
  • I deleted the webbrowser-control tag, ActiveX is not a realistic outside of Windows. – Quentin Oct 09 '12 at 10:46
  • can you give more background on this question? Why do you need browseer and not V8 binding? – Marat Oct 09 '12 at 13:55
  • Allright: I have a raspberry pi with a NFc reader connection. I have a browser. Chromium in this case. The pythonscript communicate with the NFc reader. I this case it use the python-pyscard library. When i use my NFc card the python script need to access the browser. It will execute a javascipt what i need to use for this webpage – Wouter van Reeven Oct 09 '12 at 14:35

3 Answers3

5

You need a library to allow interaction between the browser and the program.

Options include Selenium and Ghost.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I am using raspberry pi. I really like Selenium because you can run chrome as kiosk mode. But there is no way i could install the chrome web browser. I can use java, but it is to heavy I think to run on a raspberry pi – Wouter van Reeven Oct 09 '12 at 13:34
0

Have you looked at the webbrowser module ? It can be used to display your webpage. It can not call your js function, but perhaps that one can be triggered from the ready() event in the html?

Pengman
  • 708
  • 6
  • 12
  • I is easy to run a webbrowser in debian put i need tot communicate with hardware. That's way the javascript function is really important – Wouter van Reeven Oct 09 '12 at 13:35
  • ok. I though the problem was showing a website and triggering a function, after the python was done doing something. – Pengman Oct 10 '12 at 12:14
0

You could open the website from python using webbroser module with an url parameter containing your javascript code.

in your example

import webbrowser
webbrowser.open('http://localhost/test.html' + '?script=myFunction();')

Then you can read the url paramater in your javascript and execute it via eval

<script>
    window.onload = function () {
        var url = new URL(window.location.href);
        var script = url.searchParams.get("script");
        console.log(script);
        eval(script)
    }    
</script>

Please be aware, that this is only suitable for testing or developing purposes and must not be used in production since using eval like this is dangerous.

TVK
  • 1,042
  • 7
  • 21