Emacsist RSS --- If never, make it ever: C-h C-h

"titlecantbewhitespaceohthehorror"

7124

这里 查看更多 Emacs 相关推荐文章最新 Emacs 圈的动态. 欢迎关注微信公众账号: Emacsist

Hick 叽喳:

    作者原标题太有个性了. 严格意义上说, 前面说 xwidget 合并到 25.1 了并不意味着一定就会在那个版本被支持, 稍微关注了下邮件组, 有些人已经开始试用了, 本文大概就是其一.

xwidget.el has made it into Emacs 25! You might wonder how exactly the interaction between a webkit widget and Emacs works. The answer to this is simple, while lower-level tasks require bindings to the library exposing webkit, higher-level tasks are solved in JavaScript. This is why xwidget-webkit-execute-script is used throughout the sources, it binds webkit_view_execute_script. Now, what’s weird about that one is that while it has an argument for the code to evaluate, there is no mechanism to retrieve the return value at all. This oversight has been fixed in a later version of webkit by introducingwebkit_web_view_run_javascript for kicking off and webkit_web_view_run_javascript_finish for finalizing and retrieving the return value. Given this information, how the heck doesxwidget.el solve that problem with the earlier version of the library?

That’s how:

(defun xwidget-webkit-execute-script-rv (xw script &optional default)
  "Same as 'xwidget-webkit-execute-script' but but with return value.
XW is the webkit instance.  SCRIPT is the script to execute.
DEFAULT is the defaultreturn value."
  ;; Notice the ugly "title" hack.  It is needed because the Webkit
  ;; API at the time of writing didn't support returning values.  This
  ;; is a wrapper for the title hack so it's easy to remove should
  ;; Webkit someday support JS return values or we find some other way
  ;; to access the DOM.

  ;; Reset webkit title.  Not very nice.
  (let* ((emptytag "titlecantbewhitespaceohthehorror")
         title)
    (xwidget-webkit-execute-script xw (format "document.title=\"%s\";"
                                              (or default emptytag)))
    (xwidget-webkit-execute-script xw (format "document.title=%s;" script))
    (setq title (xwidget-webkit-get-title xw))
    (if (equal emptytag title)
        (setq title ""))
    (unless title
      (setq title default))
    title))

The above hack poses a number of questions:

  • Why does the title need to be a non-blank string?
  • Why is the previous title not saved and restored?
  • Why are you forcing me to use an optional default parameter to distinguish a return value serializing to an empty string from the empty string as return value?
  • Why do I need to decode JSON to make sense of the result?
  • The injection is just wrong. If you pass 1+1;2+2 you only get the return value of the first expression, but both are evaluated anyways. Nothing the good old function(){...}()trick couldn’t fix though…
  • Under what other circumstances will this hack break?

Definitely not what I’ve expected the title to be used for when you’re not looking.

原文出处: Vasilij Schneidermann
原文地址: http://emacshorrors.com/posts/titlecantbewhitespaceohthehorror.html
原文时间: 2016-01-23 06:48
本文地址: http://emacsist.com/10694
整理时间: 2016-01-23 15:03

本文由 Hick 整理,转载请保留以上信息;
COPYLEFTThe articles on this site come from Internet, thanks to all the original authors.
      If anything about COPYRIGHT, or LEFT, please contact Emacsist at gmail dot com .