From 62b3f1ad5ffc9a1a322fb8f99fec2f45bf862dae Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 27 May 2021 09:55:01 +0200 Subject: [PATCH] auto-eval --- resources/public/disable_auto_eval.html | 26 +++++++++++++++++++++++++ script/release.clj | 3 +++ src/scittle/core.cljs | 23 ++++++++++++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 resources/public/disable_auto_eval.html diff --git a/resources/public/disable_auto_eval.html b/resources/public/disable_auto_eval.html new file mode 100644 index 0000000..34ed838 --- /dev/null +++ b/resources/public/disable_auto_eval.html @@ -0,0 +1,26 @@ + + + + + + + + + + + +

Scittle

+

On this page, auto-eval is disabled.

+

+

+ + diff --git a/script/release.clj b/script/release.clj index 70fb099..b67d873 100755 --- a/script/release.clj +++ b/script/release.clj @@ -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) diff --git a/src/scittle/core.cljs b/src/scittle/core.cljs index 5bb0e35..94f28e8 100644 --- a/src/scittle/core.cljs +++ b/src/scittle/core.cljs @@ -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)