#lang scheme/base
(require srfi/13
(file "base.ss")
(file "core.ss"))
(define (url-ref)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium history current))))))
(define (title-ref)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getTitle))))))
(define (inner-html-ref selector)
(normalize-html-case
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getInnerHTML) ,selector))))))
(define (inner-html-ref* selector)
(map normalize-html-case
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllInnerHTML) ,selector))))))
(define (js-ref expr)
(respond/expr
(lambda (embed-url)
(js ((function ()
(!dot Delirium (log "jsRef" ,(javascript->string expr)))
(with (!dot Delirium (getWindow))
(return ,expr))))))))
(define (jquery-path-ref selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getJQueryReference) ,selector)))))
(define (jquery-path-ref* selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllJQueryReferences) ,selector)))))
(define (xpath-supported?)
(js-ref (js (!dot Delirium xPathSupported))))
(define (xpath-path-ref selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getXPathReference) ,selector)))))
(define (xpath-path-ref* selector)
(respond/expr
(lambda (embed-url)
(js ((!dot Delirium api getAllXPathReferences) ,selector)))))
(define (normalize-html-case html)
(and html (string-trim-both (regexp-replace* #px"[<]/?[^\\s]+" html string-downcase))))
(provide/contract
[url-ref (-> string?)]
[title-ref (-> string?)]
[inner-html-ref (-> javascript-expression? (or/c string? false/c))]
[inner-html-ref* (-> javascript-expression? (listof string?))]
[js-ref (-> javascript-expression? any)]
[jquery-path-ref (-> javascript-expression? (or/c string? false/c))]
[jquery-path-ref* (-> javascript-expression? (listof string?))]
[xpath-supported? (-> boolean?)]
[xpath-path-ref (-> javascript-expression? (or/c string? false/c))]
[xpath-path-ref* (-> javascript-expression? (listof string?))])