I have this code working in javascript:
(document.querySelectorAll('[rel="next"]'))[0].click()
I am trying to write the same thing in parenscript (a library from the Common Lisp ecossystem). The expressions in my current sketches are being evalued by the REPL under a package called Nyxt availabe here.
CL-USER> (in-package :nyxt)
Nyxt is designed to be an infinitely extensible browser. Thus, the user can change the code and/or create extensions while the program is running. This is live hackability by design.
Here is the code:
(ps:chain document (query-selector-all "[rel=next]")) (click))
Using ps:ps
I can "see" what parenscript is building:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") (click)))
"document.querySelectorAll('[rel=next]').click();"
The above result is close to what I need. However, there is still one thing missing: the subscription of the parenscript array (id. est., "[0]" in Javascript), before the .click()
.
I have some failed attempts.
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") [0] (click)))
I get an extra ".":
"document.querySelectorAll('[rel=next]').[0].click();"
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") ([0]) (click)))
I get an extra "." and unnecessary "()":
"document.querySelectorAll('[rel=next]').[0]().click();"
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]")[0] (click)))
I get an extra ".":
"document.querySelectorAll('[rel=next]').[0].click();"
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]" [0]) (click)))
I get an extra [0]
as another argument:
"document.querySelectorAll('[rel=next]', [0]).click();"
How do I insert it?
Accidentaly, I discovered the answer, the trick was just to pass just 0
without []
:
NYXT> (ps:ps (ps:chain document (query-selector-all "[rel=next]") 0 (click)))
"document.querySelectorAll('[rel=next]')[0].click();"
I think parenscript could have more tutorials and documentation. I have struggled with it. Thus, I am leaving this here to help other Common Lisp newbies like me :)