Interacting with the current web page

Pete Fison
PythonIQ

--

This is the third article in a series which explores how PyScript can be used to create sophisticated Chrome Extensions using Python… yes, Python!

Part 1 explains how to create a skeleton Chrome Extension, and Part 2 explains how PyScript can talk with other JavaScript functions and objects, as well as controlling Chrome itself (e.g. interacting with Bookmarks and Tabs).

Unfortunately one of the actions you’re most likely to need turns out to be fairly tricky, and as far as I know, only achievable with the help of a little boiler-plate code in JavaScript. This article sets out a template you can use to interact with the DOM of the current web page, and continue to build the rest of your Extension using pure Python.

The simplicity of this template belies the number of failed attempts that preceded it… and sadly the official Chrome tutorials took me down more rat-holes than I’d care to admit. I hope this article saves you that pain, and acts as a spring-board to building exciting Chrome extensions in Python. If it does — please let us all know how you got on by leaving a comment at the end?

You can find the full code for this template here: https://github.com/PFython/ChromeAWSOM3

What the Extension Does

Our example extension this time creates a yellow Popup with a button labelled “Say Hello”. Clicking on it…

--

--

Pete Fison
PythonIQ

Experienced Python developer with a special interest in web scraping, data wrangling, ETL, NLP, and AI Prompt Engineering. Former IT Director & Video Producer.