from _window import window
import browser
document = window.get_prop('document')
createElement = window.get_prop('document').get_prop('createElement')
for i in range(10):
img = createElement.call(browser.jsstr("img"), this=document)
img.set_prop("src", browser.jsstr("https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png"))
document.get_prop("body").get_prop("appendChild").call(img, this=document.get_prop("body"))
@mireille-raad sure, this seems to work also
from _window import window
import browser
document = window.get_prop('document')
createElement = window.get_prop('document').get_prop('createElement')
errorDiv = window.get_prop('document').get_prop('getElementById').call(browser.jsstr("error"), this=document)
for i in range(10):
img = createElement.call(browser.jsstr("img"), this=document)
img.set_prop("src", browser.jsstr("https://raw.githubusercontent.com/RustPython/RustPython/master/logo.png"))
errorDiv.get_prop("appendChild").call(img, this=errorDiv)it surely could be written shorter.
this is absolutely crazy ;-)
from _window import window
from browser import jsstr
items = document.get_prop("getElementsByTagName").call(jsstr("div"), this=document)
items = [items.get_prop("item").call(jsstr(str(i)), this=items) for i in range(0, int(items.get_prop("length").as_float()))]
print(items)With Pyodide, this obviously more readable version does the same
from js import window
document = window.document
items = document.getElementsByTagName("div")
items = [items.item(i) for i in range(0, int(items.length))]I think I'll might build an easier interface for this in RustPython.
import _window? is it the same like from browser import window?
from _window import window
from browser import jsstr
document = window.get_prop("document")
runBtn = document.get_prop("getElementById").call(jsstr("run-btn"), this=document)
def callback(e):
print(e)
runBtn.get_prop("addEventListener").call("click", callback, this=runBtn)
js_str.get_prop("__proto__").get_prop("__proto__") and get access to the JS Object.prototype, which is a security vulnerability
wasm/lib/src/js_module.rs. Otherwise I could, but I remember it was tricky when I thought about it before, since callbacks passed to JS don't automatically get garbage collected, but there might be some way around that now.
from browser import jsstr, jsclosure, window
document = window.get_prop("document")
runBtn = document.call_method("getElementById", jsstr("run-btn"))
def callback(this, e):
print(e)
jscb = jsclosure(callback)
runBtn.call_method("addEventListener", jsstr("click"), jscb.value)
Ah nice :-)
With your code runBtn.call_method("addEventListener", jsstr("click"), jscb.value) I'm getting error TypeError: EventTarget.addEventListener: At least 2 arguments required, but only 1 passed.
I then tried this code adapted from my draft above
from _window import window
from browser import jsstr, jsclosure
document = window.get_prop("document")
runBtn = document.get_prop("getElementById").call(jsstr("run-btn"), this=document)
def callback(e):
print(e)
jscb = jsclosure(callback)
runBtn.get_prop("addEventListener").call(jsstr("click"), jscb.value, this=runBtn)but when I hit the button, Firefox gets an exception:
Uncaught Error: closure invoked recursively or destroyed already
Ct http://localhost:8080/1.index.js:1
__wbindgen_throw http://localhost:8080/index.js:1
F http://localhost:8080/1.index.js:1
c http://localhost:8080/1.index.js:1
_ http://localhost:8080/1.index.js:1
At http://localhost:8080/1.index.js:1
P http://localhost:8080/1.index.js:1
__wbg_apply_28bd29e919552baf http://localhost:8080/index.js:1
U http://localhost:8080/1.index.js:1
v http://localhost:8080/index.js:1
EventListener.handleEvent* http://localhost:8080/index.js:1
<anonymous> http://localhost:8080/index.js:1
promise callback* http://localhost:8080/index.js:1
s http://localhost:8080/index.js:1
<anonymous> http://localhost:8080/index.js:1
<anonymous> http://localhost:8080/index.js:1
this seems to work for me:
from browser import jsstr, jsclosure, window
document = window.get_prop("document")
runBtn = document.call_method("getElementById", jsstr("console"))
def callback(this, e):
print(e)
jscb = jsclosure(callback)
runBtn.get_prop("addEventListener").call(jsstr("click"), jscb.value, this=runBtn)When I click on the console textarea, it prints JsValue(MouseEvent)
Lib/test/test_with.py? There might be a few tests there you can fix related to executing/compiling Python that should be a good introduction to the codebase
I found some code which could be relevant in vm/src/pyobj.rs and I would like to run the test "cargo run -- -m test test_bool -v" from inside "Lib/test" and have breakpoints inside pyobj.rs so I can get a better feeling of how trying to extending a class with "class C(bool)" will call the pyobj.rs. I believe that if I can set breakpoints there I will be eventually able to understand how to prevent the bool class from being subclassed.
Could you give me a tip on how to put breakpoints inside the vm while running cargo run -- -m test test_bool -v ??
Thanks