1

我正在尝试编写一个 Ubiquity 命令,该命令允许您用该图像本身替换指向图像的选定链接或 URL。但是,如果选择中存在任何 html 标记,则 CmdUtils.setSelection() 函数(在此处记录)似乎什么都不做,这使得它无法用于替换任何链接。选择纯文本时,它的功能完全符合预期,用<img src="text"/>标签替换文本。有什么我遗漏的东西,还是这个功能根本不允许我替换 html?如果是后者,是否有允许我这样做的功能或方法?也欢迎任何其他建议!

 CmdUtils.CreateCommand({
  name: "fetch-image",
  author: {name: "Josh Timmer"},
  license: "GPL",
  description: "Replaces links or URLs pointing to images with the image itself",
  help: "Highlight text or a hyperlink and execute this command",
  takes: {"image URL": /.*/},

  _getImgUrl: function(itemIq) {
     if (itemIq.html.indexOf("<a ",0) < 0)
        return itemIq.text;
     var refPos = itemIq.html.indexOf("href=\"",0);
     if (refPos < 0)
        return "Error, no URL found!";
     var startPos = itemIq.html.indexOf("\"", refPos);
     var endPos = itemIq.html.indexOf("\"", startPos + 1);
     startPos += 1;
     var url = itemIq.html.substring(startPos, endPos);
     return url;
  },

  preview: function(pblock, input) {
     pblock.innerHTML = "Image URL: " + this._getImgUrl(input) + "<br/><br/><img src='" + this._getImgUrl(input) + "'/>";
  },

  execute: function img_insert(input) {
     CmdUtils.setSelection("<img src='" + this._getImgUrl(input) + "'/>");
     displayMessage("Executed: " + this._getImgUrl(input));
  }
});
4

1 回答 1

1

只要传递了有效的 HTML,它就应该可以工作。

CmdUtils.CreateCommand({
  name: "fetch image",
  authors: [{name: "Josh Timmer"}, "satyr"],
  license: "GPL",
  description: "Replaces links pointing to images with the image itself.",
  help: "Highlight text or a hyperlink and execute this command.",
  preview: function fetch_image_preview(pblock) {
    pblock.innerHTML = this._images() || this.previewDefault();
  },
  execute: function fetch_image_execute() {
    CmdUtils.selection = this._images();
  },
  _images: function fetch_image_urls(){
    var srcs = CmdUtils.getSelectedNodes("a");
    if (!srcs.length) srcs = CmdUtils.selection.match(/\bhttps?:\/+\S+/g);
    return [<img src={s}/>.toXMLString() for each (s in srcs)].join("");
  },
});
于 2010-08-21T22:16:31.793 回答