mozilla

Revision 143094 of PyDOM

  • Revision slug: PyDOM
  • Revision title: PyDOM
  • Revision id: 143094
  • Created:
  • Creator: Toddw
  • Is current revision? No
  • Comment 22 words added

Revision Content

Introduction

This is the documentation for the binding of Python and the Mozilla DOM. This allows you to use Python code (almost) anywhere you can use JavaScript.

Globals and namespaces

This is taken from a mail to the pyxpcom list - it should be expanded, but a copy-paste will do for now

Normal module semantics don't exist here. The globals are 'bound' to the global object (i.e., the window), instead of to a module's __dict__ as you expect in the Python world. Note that we don't actually 'import' anything here - the scripts and event handlers do not exist in a Python module.

The intent is to provide similar semantics to JS. For example, when a top-level window has something like:

<window ...
 script="foo = 1"
...

An event handler like:

  <button onclick="print foo"/>

Should be able to reference 'foo' via globals as shown.

Note that you can stick arbitrary values on any DOM object - this is what JS calls 'expandos'. For example, let's say you have XUL similar to pyxultest:

Top-level script code says something like:

button = document.getElementById("some-button")
button.foo = 0

And the button itself might look like:

<button id="some-button" label="click here" 
        onclick="event.target.foo += 1; print 'Foo is now', event.target.foo"/>

Note that (a) we have stuck an arbitrary attribute on a DOM element and (b) in all cases (e.g., event handler and top-level script), the DOM node needs to be explicitly specified - the globals are the window itself. The event handler could also have used getElementById - the point is that both the event handler and top-level script share the same namespace.

Using Python

Ideally, you just tell Mozilla you are using Python, and magically you can use Python instead of JavaScript. Once you set the script type for a node, it is the default for that node and all children. This means that if you set the script-type to Python for the window, Python is the default script type for all child elements in the window.

XUL

Use a script-type attribute on your elements. For example,

 <window script-type="application/x-python" .../>

HTML

<meta http-equiv="Content-Script-Type" content="application/x-python" /> should work (but possibly doesn't - see {{ Bug("354630") }}). But addEventListener within a <script type="application/x-python"> block does.

Almost

Python code can not be secured - so it will only run from trusted sources. This means you can only have Python code in chrome. Content loaded from anywhere other than a chrome:// URL can not host Python. It should be obvious why you don't want to run http://evil.com/something_evil.py in your browser! Inside chrome, you can use Python via either HTML or XUL.

PyXPCOM: Create and use XPCOM components with Python
Python-SpiderMonkey
PyDOM Samples: Sample applications that use PyDOM
PythonExt: A Firefox/XULRunner extension that can be used to install PyDOM

 

Revision Source

<h2 id="Introduction" name="Introduction">Introduction</h2>
<p>This is the documentation for the binding of Python and the Mozilla DOM. This allows you to use Python code (<a href="#Almost">almost</a>) anywhere you can use JavaScript.</p>
<h2 id="Globals_and_namespaces" name="Globals_and_namespaces">Globals and namespaces</h2>
<dl><dd> <em>This is taken from a mail to the pyxpcom list - it should be expanded, but a copy-paste will do for now</em> </dd></dl>
<p>Normal module semantics don't exist here. The globals are 'bound' to the global object (i.e., the <a href="/en/DOM/window" title="en/DOM/window">window</a>), instead of to a module's <code>__dict__</code> as you expect in the Python world. Note that we don't actually 'import' anything here - the scripts and event handlers do not exist in a Python module.</p>
<p>The intent is to provide similar semantics to JS. For example, when a top-level window has something like:</p>
<pre>&lt;window ...
 script="foo = 1"
...
</pre>
<p>An event handler like:</p>
<pre>  &lt;button onclick="print foo"/&gt;
</pre>
<p>Should be able to reference 'foo' via globals as shown.</p>
<p>Note that you can stick arbitrary values on any DOM object - this is what JS calls 'expandos'. For example, let's say you have XUL similar to pyxultest:</p>
<p>Top-level script code says something like:</p>
<pre class="eval">button = document.getElementById("some-button")
button.foo = 0
</pre>
<p>And the button itself might look like:</p>
<pre class="eval">&lt;button id="some-button" label="click here" 
        onclick="event.target.foo += 1; print 'Foo is now', event.target.foo"/&gt;

</pre>
<p>Note that (a) we have stuck an arbitrary attribute on a DOM element and (b) in all cases (e.g., event handler and top-level script), the DOM node needs to be explicitly specified - the globals are the window itself. The event handler could also have used <a href="/en/DOM/document.getElementById" title="en/DOM/document.getElementById">getElementById</a> - the point is that both the event handler and top-level script share the same namespace.</p>
<h2 id="Using_Python" name="Using_Python">Using Python</h2>
<p>Ideally, you just tell Mozilla you are using Python, and magically you can use Python instead of JavaScript. Once you set the <em>script type</em> for a node, it is the default for that node and all children. This means that if you set the script-type to Python for the window, Python is the default script type for all child elements in the window.</p>
<h3 id="XUL" name="XUL">XUL</h3>
<p>Use a <em>script-type</em> attribute on your elements. For example,</p>
<pre class="eval"> &lt;window script-type="application/x-python" .../&gt;
</pre>
<h3 id="HTML" name="HTML">HTML</h3>
<p><code>&lt;meta http-equiv="Content-Script-Type" content="application/x-python" /&gt;</code> should work (but possibly doesn't - see {{ Bug("354630") }}). But <a href="/en/DOM/element.addEventListener" title="en/DOM/element.addEventListener">addEventListener</a> within a <code>&lt;script type="application/x-python"&gt;</code> block does.</p>
<h2 id="Almost" name="Almost">Almost</h2>
<p>Python code can not be secured - so it will only run from trusted sources. This means you can only have Python code in chrome. Content loaded from anywhere other than a <code><a class=" external" href="chrome://" rel="freelink">chrome://</a></code> URL can not host Python. It should be obvious why you don't want to run <span class="nowiki">http://evil.com/something_evil.py</span> in your browser! Inside chrome, you can use Python via either HTML or XUL.</p>
<h2 id="Related_Topics" name="Related_Topics">Related Topics</h2>
<dl><dd> <a href="/en/PyXPCOM" title="en/PyXPCOM">PyXPCOM</a>: Create and use XPCOM components with Python </dd><dd> <a class="external" href="http://code.google.com/p/python-spidermonkey/">Python-SpiderMonkey</a> </dd><dd> <a class="external" href="http://pyxpcomext.mozdev.org/samples.html" title="http://pyxpcomext.mozdev.org/samples.html">PyDOM Samples</a>: Sample applications that use PyDOM </dd><dd> <a class="external" href="http://pyxpcomext.mozdev.org/" title="http://pyxpcomext.mozdev.org/">PythonExt</a>: A Firefox/XULRunner extension that can be used to install PyDOM<br>
</dd><dd> <br>
</dd></dl>
<p> </p>
Revert to this revision