Running Interactive Scripts

browser.engineering
6 min read
easy
Article URL: https://browser.engineering/scripts.html Comments URL: https://news.ycombinator.com/item?id=28554134 Points: 1 # Comments: 0
Running Interactive Scripts Twitter · Blog · Patreon · Discussions

The first web applications were like last chapter's guest book, with the server generating new web pages for every user action. But in the early 2000s, JavaScript-enhanced web applications, which can update pages dynamically and respond immediately to user actions, took their place. Let's add support for this key web application technology to our toy browser.

Installing DukPy

Actually writing a JavaScript interpreter is beyond the scope of this book,But check out a book on programming language implementation if it sounds interesting! so this chapter uses the dukpy library for executing JavaScript.

DukPy wraps a JavaScript interpreter called Duktape. The most famous JavaScript interpreters are those used in browsers: TraceMonkey (Firefox), JavaScriptCore (Safari), and V8 (Chrome). Unlike those implementations, which are extremely fast but also extremely complex, Duktape aims to be simple and extensible, and is usually embedded inside a larger C or C++ project.For example, in a video game the high-speed graphics code is usually written in C or C++ , but the actual plot of the game is usually written in a simpler language.

Like other JavaScript engines, DukPy not only executes JavaScript code, but also allows JavaScript code to call exported Python functions. We'll be using this feature to allow JavaScript code to modify the web page it's running on.

The first step to using DukPy is installing it. On most machines, including on Windows, macOS, and Linux systems, you should be able to do this with:

pip3 install dukpy

Depending on your computer, the pip3 command might be called pip , or you might use easy_install instead. You may also need to install pip3 . If you do your Python programming through an IDE, you may need to use your IDE's package installer. If nothing else works, you can build from source. If you're following along in something other than Python, you might need to skip this…
Read full article