[Greasemonkey] GM & JavaScript/Applet Interaction

Jesse Merriman jessemerriman at warpmail.net
Sat Oct 14 17:42:45 PDT 2006


Hello,

I'm having a problem using Greasemonkey along with an attempt to get
some JavaScript code to interact with a Java applet. When I use a
static page, it works, but not when GM has added in the relevant
parts to another page. Here's the 4 files of code I've boiled things
down to:

======== TestPage.html, the static page that does work ========
<html>
<head><title>Static Test</title></head>
<body>
<script type="text/javascript">
function callApplet() {
    var applet = document.getElementById('TestApplet');
    applet.foo();
}

function addApplet() {
    var appletDiv = document.createElement('div');
    appletDiv.innerHTML =
        '<object id="TestApplet" classid="java:TestApplet.class" ' +
        'codetype="application/java" ' +
        'width="300" height="50" ' +
        'archive="http://www.jessemerriman.com/files/TestApplet/TestApplet.jar" ' +
        '<param name="scriptable" value="true"></param>' +
        '</object>';
    document.body.appendChild(appletDiv);
}

function addForm() {
    var buttonForm = document.createElement('form');
    var buttonInput = document.createElement('input');
    buttonInput.type = 'button';
    buttonInput.value = 'Call applet';
    buttonInput.addEventListener('click', callApplet, true);
    buttonForm.appendChild(buttonInput);
    document.body.appendChild(buttonForm);
}

addApplet();
addForm();
</script>
</body>
</html>

======== Test.user.js, my Greasemonkey script ========
// ==UserScript==
// @name          Test
// @namespace     http://www.jessemerriman.com/
// @description   Testing JavaScript/Applet interaction.
// @include       */MinimalTestPage.html
// ==/UserScript==

function callApplet() {
    var applet = document.getElementById('TestApplet');
    applet.foo();
}

function addApplet() {
    var appletDiv = document.createElement('div');
    appletDiv.innerHTML =
        '<object id="TestApplet" classid="java:TestApplet.class" ' +
        'codetype="application/java" ' +
        'width="300" height="50" ' +
        'archive="http://www.jessemerriman.com/files/TestApplet/TestApplet.jar" ' +
        '<param name="scriptable" value="true"></param>' +
        '</object>';
    document.body.appendChild(appletDiv);
}

function addForm() {
    var buttonForm = document.createElement('form');
    var buttonInput = document.createElement('input');
    buttonInput.type = 'button';
    buttonInput.value = 'Call applet';
    buttonInput.addEventListener('click', callApplet, true);
    buttonForm.appendChild(buttonInput);
    document.body.appendChild(buttonForm);
}

addApplet();
addForm();

======== MinimalTestPage.html, the page I'm testing the GM script on ========
<html>
<head><title>Minimal Test</title></head>
<body></body>
</html>

======== TestApplet.java, my applet ========
import java.applet.Applet;
import java.awt.Graphics;

public class TestApplet extends Applet {
    private String myText;

    public void init() {
        myText = "Start";
    }

    public void foo() {
        myText = "foo() was called!";
        repaint();
    }

    public void paint(Graphics g){
        g.drawString(myText, 10, 10);
    }
}


If I just open up TestPage.html in Firefox, it works as expected:
clicking the button causes the applet text to change (though the
first time it takes a few seconds). However, if I install the above
GM script, and then open up MininmalTestPage.html, clicking the button
does nothing. The error that shows up in the JavaScript Console is
"applet.foo is not a function".

There is a very similar problem with the AdBlock extension, discussed
here:

http://www.aasted.org/adblock/viewtopic.php?t=3094&sid=b2c9dbb39f0cec97a0559ccb4663152a

The problem page is:
  http://web.vscht.cz/bozonm/jmoltest/

The error message in that case is "applet.script in not a function".
With AdBlock disabled, that page will work correctly, but my
stuff still does not. Any ideas?

GM:      0.6.5.20060727
Firefox: 1.5.0.7
Java:    1.5.0_08
OS:      Linux (Gentoo)

--
Jesse Merriman
jessemerriman at warpmail.net
http://www.jessemerriman.com/


More information about the Greasemonkey mailing list