我如何在 ClojureScript 中创建一个可以专注于 window 的 js/Notification?

How can I create a js/Notification in ClojureScript that can focus on the window?

我想创建一个 (chrome) 带有文本的通知,我可以单击通知以转到发出通知的页面。这是我得到的最接近的,请注意它非常不合惯用。我不得不使用 js* (js star),我找不到文档,但它只执行 javascript.

的字符串
(js* "  var notification = new Notification('Here is the title', {
    icon: 'http://path.to/my/icon.png',
    body: 'Some body text',
  });

  notification.onclick = function () {
    console.log('hi there');
    this.close();
    window.focus();
  };")

注意,log 有效,close 有效,但 window.focus() 无效。如果您将此代码的 js 部分复制并粘贴到浏览器的控制台中,它 确实 有效。

我认为正确的答案应该更像下面这样,但是 onclick 对于任何函数都不起作用:

(new js/Notification "Here is the title"
             (clj->js {:body "(click to vist page)"
                       :onclick #(.focus js/window)}))

EDIT:如果您输入以下内容(例如,在 figwheel 中),js/window 肯定有一个 focus 参数。很明显,但我刚刚检查过,但是用 (.focus js/window):

调用时它不起作用
(.keys js/Object js/window)

这甚至无法集中注意力:

(js* "window.focus()")

而且我似乎无法找到附加到 js/Notification 的任何密钥来使 onclick 工作:

(.keys js/Object (new js/Notification "hi" (clj->js {:body "body text"})))

我肯定做错了什么,但是什么?

这是您的原始 JavaScript 代码的 "straight port":

(let [title "Here is the title"
      js-opts (js-obj "icon" "http://path.to/my/icon.png"
                      "body" "Some body text")
      js-notification (js/Notification. title js-opts)]
  (aset js-notification "onclick"
    (fn []
      (this-as js-this
        (.log js/console "hi there")
        (.close js-this)
        (.focus js/window)))))

请注意,我在 JavaScript 引用前加上了 js- 前缀,这是在 ClojureScript 中处理 JavaScript 互操作时的常见约定。