Brython execution?

215 views
Skip to first unread message

Edward Elliott

unread,
Dec 26, 2020, 2:21:17 PM12/26/20
to brython
How does brython execute code?  My limited understanding is the following:
  1. brython.js scans for <script type='text/python'> tags in DOM
  2. compiles found scripts to js code
  3. calls eval (code) on the javascript 

Where "code" in the above example is just a string.  I believe so, but not positive.

I ask because chrome extensions are moving from version 2 to 3 some time soon.  In version 3, extensions are not allowed to execute arbitrary strings of js.  All js code must reside in a file inside the extension.  Which means no dynamic (runtime) code generation and hence no brython inside extensions. :(

It's not clear when chrome will stop running version 2 extensions.  Probably some time in 2021.

Most of you don't use brython in a browser extension.  But you may use greasemonkey or tampermonkey, which rely on the same behavior.  Regardless, it's sad to see some brython environments taken away by fiat in a browser monoculture.

I'll explore potential workarounds.  One option is to compile the brython code to js code outside the browser, then save the js code in a file inside the extension.  This solution would require two things:
  1. Brython allows output of compiled js
  2. The entire environment must also be compiled to js, including any python modules or brython-specific packages used.
Not sure if either of those is possible, from a quick look at the docs.  Anyone have experience with saving js code generated by brython?

Kiko

unread,
Dec 26, 2020, 3:38:57 PM12/26/20
to bry...@googlegroups.com
2020-12-26 20:21 GMT+01:00, Edward Elliott <ese...@gmail.com>:
> How does brython execute code? My limited understanding is the following:
>
> 1. brython.js scans for <script type='text/python'> tags in DOM
> 2. compiles found scripts to js code
> 3. calls eval (code) on the javascript
>
>
> Where "code" in the above example is just a string. I believe so, but not
> positive.

Have a look here:
https://github.com/brython-dev/brython/wiki/How-Brython-works, to
better understand what is happening behind the curtains.

>
> I ask because chrome extensions are moving from version 2 to 3 some time
> soon. In version 3, extensions are not allowed to execute arbitrary strings
>
> <https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings>
> of
> js. All js code must reside in a file inside the extension. Which means
> no dynamic (runtime) code generation and hence no brython inside
> extensions. :(

You can include brython.js and brython stdlib and you can create your
own lib and wrap it to be included within the extension (here:
https://github.com/brython-dev/brython/tree/master/scripts, you have
scripts to do so). I think it would be possible to run brython with
manifest 3 but I'm not using chrome and maybe I'm not the most
adequate person to talk about chrome :-(

>
> It's not clear when chrome will stop running version 2 extensions.
> Probably some time in 2021.
>
> Most of you don't use brython in a browser extension. But you may use
> greasemonkey or tampermonkey, which rely on the same behavior. Regardless,
>
> it's sad to see some brython environments taken away by fiat in a browser
> monoculture.
>
> I'll explore potential workarounds. One option is to compile the brython
> code to js code outside the browser, then save the js code in a file inside
>
> the extension. This solution would require two things:
>
> 1. Brython allows output of compiled js
> 2. The entire environment must also be compiled to js, including any
> python modules or brython-specific packages used.
>
> Not sure if either of those is possible, from a quick look at the docs
> <https://brython.info/static_doc/en/modele.html>. Anyone have experience
> with saving js code generated by brython?
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/98f194fe-4f59-48cd-a814-928bfba4eeban%40googlegroups.com.
>

Edward Elliott

unread,
Dec 27, 2020, 2:43:09 PM12/27/20
to bry...@googlegroups.com
Hi kiko, thanks so much!  My guess is also that some workaround will be possible... but perhaps that's wishful thinking. ;)  I'll work on it and see what happens.


Joao S. O. Bueno

unread,
Dec 27, 2020, 4:41:30 PM12/27/20
to bry...@googlegroups.com
On Sun, 27 Dec 2020 at 16:43, Edward Elliott <ese...@gmail.com> wrote:
Hi kiko, thanks so much!  My guess is also that some workaround will be possible... but perhaps that's wishful thinking. ;)  I'll work on it and see what happens.


As I see it, if arbitrary eval is indeed restricted, the way to go will be to pre-generate the javascript into the extension - which will
also restrict runtime Python code generation (including eval) - but some restricted forms would work.

That, and loading external resources into the extension at runtime.


Coming to that: do you currently use Brthon to create any browser extension? 
What setup do you use?

 

Edward Elliott

unread,
Dec 27, 2020, 6:17:17 PM12/27/20
to brython
Hi Joao, 
Yes that's true about eval.  All uses would be restricted.

Which raises a good question: does any brython code other than the compiler / interpreter use eval?  I.e. does the implementation of any brython function or module use eval in its own code?  That would be another problem.

Brython Browser Extensions

As for running brython-based browser extensions, the answer is yes I have for several years.  First I used tampermonkey to load brython, but it was too complicated and fragile.  Kept getting unpredictable execution errors.

This year I moved to creating a browser extension from scratch.  It was tricky, but eventually I got it thanks to Chrome API docs and trial and error.  Haven't tested other browsers but the same code should work.  Most major browsers support the Chrome API.  I also tried the Mozilla promises API but didn't get very far.

Why would one want to run brython in a browser extension?  Several reasons:
  1. Ability to run brython on any website, without access to the server
  2. Access to global browser state, such as settings, context menus, and UI elements
  3. Access to internal data & features such as bookmarks, history, unlimited local storage, downloads, tab & window management, etc
  4. The ability to do all this with clean python instead of nasty javascriptses (we hates it, precious)

Basic Setup

Running brython on pages inside the extension is similar to a regular html page.  Only difference is you can't execute code directly in the html.  So "<body onload="brython()">" won't work.  Instead you use a helper js file to start brython on DOMContentLoaded.

If you want to run brython on other websites (i.e. "content scripts" in chrome API lingo) then you just need a javascript shim to insert your brython scripts in the DOM before you invoke brython.  With that, you can run custom brython scripts on any website you want.  Rather handy.

I posted an example to the issue tracker a while back.  You can download the zip file and follow the short instructions to get a minimal brython extension running.  From there, just use the Chrome API docs (or whatever browser API you prefer).  It's a bit of a learning curve but gets easier once you pickup a few concepts.  Rule #1: you can't access anything directly... everything requires message passing and callbacks.

Hope this helps.
Reply all
Reply to author
Forward
0 new messages