auto-eval
This commit is contained in:
parent
1db46040a3
commit
62b3f1ad5f
26
resources/public/disable_auto_eval.html
Normal file
26
resources/public/disable_auto_eval.html
Normal file
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
<script src="js/scittle.js" type="application/javascript"></script>
|
||||
<script>scittle.core.disable_auto_eval()</script>
|
||||
<!-- to do the same as scittle does by default: -->
|
||||
<!-- <script>scittle.core.eval_string("(.addEventListener js/document \"DOMContentLoaded\" js/scittle.core.eval_script_tags false)")</script> -->
|
||||
<script type="application/x-scittle">
|
||||
(defn my-alert []
|
||||
(js/alert "You clicked!"))
|
||||
|
||||
(set! (.-my_alert js/window) my-alert)
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Scittle</h1>
|
||||
<p>On this page, auto-eval is disabled.</p>
|
||||
<p><button onclick="scittle.core.eval_script_tags()">
|
||||
Click me to evaluate script tags!
|
||||
</button></p>
|
||||
<p><button onclick="my_alert()">
|
||||
Click me to run evaluated function!
|
||||
</button></p>
|
||||
</body>
|
||||
</html>
|
|
@ -15,6 +15,9 @@
|
|||
(fs/copy "resources/public/bookmarklet.html" "gh-pages"
|
||||
{:replace-existing true})
|
||||
|
||||
(fs/copy "resources/public/disable_auto_eval.html" "gh-pages"
|
||||
{:replace-existing true})
|
||||
|
||||
(def style-source-dir (fs/file "resources" "public" "css"))
|
||||
(def style-target-dir (fs/file "gh-pages" "css"))
|
||||
(fs/create-dirs style-target-dir)
|
||||
|
|
|
@ -35,11 +35,11 @@
|
|||
plug-in-name ;; unused for now
|
||||
(swap! ctx sci/merge-opts sci-opts))
|
||||
|
||||
(defn load-contents [script-tags]
|
||||
(defn- eval-script-tags* [script-tags]
|
||||
(when-let [tag (first script-tags)]
|
||||
(if-let [text (not-empty (gobject/get tag "textContent"))]
|
||||
(do (eval-string text)
|
||||
(load-contents (rest script-tags)))
|
||||
(eval-script-tags* (rest script-tags)))
|
||||
(let [src (.getAttribute tag "src")
|
||||
req (js/XMLHttpRequest.)
|
||||
_ (.open req "GET" src true)
|
||||
|
@ -47,11 +47,22 @@
|
|||
(fn [] (this-as this
|
||||
(let [response (gobject/get this "response")]
|
||||
(eval-string response))
|
||||
(load-contents (rest script-tags)))))]
|
||||
(eval-script-tags* (rest script-tags)))))]
|
||||
(.send req)))))
|
||||
|
||||
(defn ^:export eval-script-tags []
|
||||
(let [script-tags (js/document.querySelectorAll "script[type='application/x-scittle']")]
|
||||
(eval-script-tags* script-tags)))
|
||||
|
||||
(def auto-load-disabled? (volatile! false))
|
||||
|
||||
(defn ^:export disable-auto-eval
|
||||
"By default, scittle evaluates script nodes on the DOMContentLoaded
|
||||
event using the eval-script-tags function. This function disables
|
||||
that behavior."
|
||||
[]
|
||||
(vreset! auto-load-disabled? true))
|
||||
|
||||
(js/document.addEventListener
|
||||
"DOMContentLoaded"
|
||||
(fn []
|
||||
(let [script-tags (js/document.querySelectorAll "script[type='application/x-scittle']")]
|
||||
(load-contents script-tags))), false)
|
||||
(fn [] (when-not @auto-load-disabled? (eval-script-tags))), false)
|
||||
|
|
Loading…
Reference in a new issue