Browse Source

feat: support custom js

charlie 4 years ago
parent
commit
0415ba4272

+ 3 - 1
src/main/frontend/components/theme.cljs

@@ -32,7 +32,9 @@
    (fn []
      (ui-handler/add-style-if-exists!)
      (pdf/reset-current-pdf!)
-     (plugin-handler/hook-plugin-app :current-graph-changed {}))
+     (ui-handler/add-style-if-exists!)
+     (plugin-handler/hook-plugin-app :current-graph-changed {})
+     (js/setTimeout #(ui-handler/exec-js-if-exists-&-allowed!) 3000))
    [current-repo])
 
   (rum/use-effect!

+ 9 - 0
src/main/frontend/config.cljs

@@ -298,6 +298,7 @@
 (defonce recycle-dir ".recycle")
 (def config-file "config.edn")
 (def custom-css-file "custom.css")
+(def custom-js-file "custom.js")
 (def metadata-file "metadata.edn")
 (def pages-metadata-file "pages-metadata.edn")
 
@@ -398,6 +399,14 @@
      (get-file-path repo
                     (str app-name "/" custom-css-file)))))
 
+(defn get-custom-js-path
+  ([]
+   (get-custom-js-path (state/get-current-repo)))
+  ([repo]
+   (when repo
+     (get-file-path repo
+                    (str app-name "/" custom-js-file)))))
+
 (defn get-block-hidden-properties
   []
   (get-in @state/state [:config (state/get-current-repo) :block-hidden-properties]))

+ 44 - 1
src/main/frontend/handler/ui.cljs

@@ -3,7 +3,12 @@
             [dommy.core :as dom]
             [frontend.util :as util]
             [frontend.db :as db]
+            [frontend.db.model :as db-model]
+            [frontend.config :as config]
             [frontend.state :as state]
+            [frontend.storage :as storage]
+            [frontend.fs :as fs]
+            [frontend.loader :refer [load]]
             [goog.dom :as gdom]
             [goog.object :as gobj]
             [clojure.string :as string]
@@ -95,11 +100,49 @@
   []
   (when-let [style (or
                     (state/get-custom-css-link)
-                    (db/get-custom-css)
+                    (db-model/get-custom-css)
                     ;; (state/get-custom-css-link)
 )]
     (util/add-style! style)))
 
+(def *js-execed (atom #{}))
+
+(defn exec-js-if-exists-&-allowed!
+  []
+  (when-let [href (or
+                     (state/get-custom-js-link)
+                     (config/get-custom-js-path))]
+    (let [k (str "ls-js-allowed-" href)
+          execed #(swap! *js-execed conj href)
+          execed? (contains? @*js-execed href)
+          ask-allow #(let [r (js/confirm "Found the custom.js file, is it allowed to execute?
+          (If you don't understand the content of this file, it is recommended
+          not to allow execution, which has certain security risks.)")]
+                       (if r
+                         (storage/set k (js/Date.now))
+                         (storage/set k false))
+                       r)
+          allowed! (storage/get k)
+          should-ask? (or (nil? allowed!)
+                          (> (- (js/Date.now) allowed!) 604800))]
+      (when (and (not execed?)
+                 (not= false allowed!))
+        (if (string/starts-with? href "http")
+          (when (or (not should-ask?)
+                    (ask-allow))
+            (load href #(do (js/console.log "[custom js]" href) (execed))))
+          (util/p-handle
+            (fs/read-file "" href)
+            #(when-let [scripts (and % (string/trim %))]
+               (when-not (string/blank? scripts)
+                 (if (or (not should-ask?) (ask-allow))
+                   (try
+                     (do
+                       (js/eval scripts)
+                       (execed))
+                     (catch js/Error e
+                       (js/console.error "[custom js]" e))))))))))))
+
 (defn toggle-wide-mode!
   []
   (let [wide? (state/get-wide-mode?)

+ 4 - 0
src/main/frontend/state.cljs

@@ -252,6 +252,10 @@
   []
   (:custom-css-url (get-config)))
 
+(defn get-custom-js-link
+  []
+  (:custom-js-url (get-config)))
+
 (defn get-default-journal-template
   []
   (when-let [template (get-in (get-config) [:default-templates :journals])]