Make a bookmarklet in Python

This is the first post I made a bookmarklet that works with code written in Python using Brython The second half is just code golf

What is Brython

http://qiita.com/ryo_grid/items/5e34220ed48f4580126d This was very helpful.

There is demand

(Maybe) No

I wrote it for the time being

bookmarklet


javascript:
(function() {
  var py = document.createElement('script');
  py.type = 'text/python';
  py.src = 'https://example/script.py';
  document.body.appendChild(py);
  var br = document.createElement('script');
  br.src = 'https://example/brython.js';
  br.onload = function() {
    brython();
  };
  document.body.appendChild(br);
})();

/script.py


from browser import alert

alert("Hello World!")

The mechanism is simple, just before </ body>

<script type="text/python" src="https://example/script.py"></script>
<script src="https://example/brython.js"></script>

Is just adding After loading brython.js, callbrython ()using the onload attribute to run the text / python script in the page.

Cross-domain problem

Brython uses Ajax to load any external scripts Therefore, there is a fatal problem that bookmarklets do not work except on the server page where the script is placed. So I solved it by adding ʻAccess-Control-Allow-Origin: *` to the HTTP header

shorten

It's a bookmarklet, so it should be shorter (?)

Omit the declaration and change the variable name to one character

Also put the document in a variable

javascript:
(function(d,p,b) {
  p = d.createElement('script');
  p.type = 'text/python';
  p.src = 'https://example/script.py';
  d.body.appendChild(p);
  b = d.createElement('script');
  b.src = 'https://example/brython.js';
  b.onload = function() {
    brython();
  };
  d.body.appendChild(b);
})(document);

Recursion

Call with ʻarguments.callee` because there is no function name (If you give a function name, a space will be inserted and it is inappropriate as a bookmarklet) Use ternary operator without if statement

javascript:
(function d(d,u,c,e) {
  e = d.createElement('script');
  e.src = u + (c ? 'brython.js' : 'script.py');
  c ? (arguments.callee(d,u), e.onload = function() {
    brython();
  }) : (e.type = 'text/python');
  d.body.appendChild(e);
})(document,'https://example/',1);

Remove spaces, line breaks

javascript:(function d(d,u,c,e) {e=d.createElement('script');e.src=u+(c?'brython.js':'script.py');c?(arguments.callee(d,u),e.onload=function(){brython();}):(e.type='text/python');d.body.appendChild(e);})(document,'https://example/',1);

330 characters => 235 characters I think it's still a long time, but my technology seems to be the limit

It's over, have a good Python life!

Recommended Posts

Make a bookmarklet in Python
Let's make a combination calculation in Python
Make a copy of the list in Python
Make a rock-paper-scissors game in one line (python)
Make a joyplot-like plot of R in python
Take a screenshot in Python
Create a function in Python
Don't make test.py in Python!
Make a fortune with Python
Make Opencv available in Python
Make python segfault in 2 lines
Draw a heart in Python
Try to make a Python module in C language
Make a simple Slackbot with interactive button in python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
Make python segfault in one line
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Let's make a GUI with python.
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Make standard output non-blocking in Python
python / Make a dict from a list.
[Python] Make the function a lambda function
Make a recommender system with python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Make python segfault in three lines
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Let's make a graph with python! !!
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Make a curtain generator in Blender
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Make a table of multiplication of each element in a spreadsheet (Python)
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
I tried to make a stopwatch using tkinter in python
I want to make input () a nice complement in python
Generate a first class collection in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Solve ABC175 A, B, C in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Use print in a Python2 lambda expression
Let's make a shiritori game with Python
Make ASCII art GIF animation in Python
Do a non-recursive Euler Tour in Python
Meta-analysis in Python