首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ubiquity CmdUtils.setSelection不会取代超文本标记语言

Ubiquity CmdUtils.setSelection不会取代超文本标记语言
EN

Stack Overflow用户
提问于 2010-04-15 08:28:15
回答 1查看 156关注 0票数 1

我正在尝试编写一个Ubiquity命令,它允许您将指向图像的选定链接或URL替换为图像本身。但是,如果所选内容中存在任何html标记,CmdUtils.setSelection()函数(文档化的here)似乎不会执行任何操作,这使得它无法替换任何链接。选择纯文本时,它的功能与预期完全相同,即用<img src="text"/>标签替换文本。我是不是遗漏了什么,或者这个函数不允许我替换html?如果是后者,有没有函数或方法可以让我这样做呢?任何其他建议也是受欢迎的!

代码语言:javascript
复制
 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));
  }
});
EN

回答 1

Stack Overflow用户

发布于 2010-08-22 06:16:32

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

代码语言:javascript
复制
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("");
  },
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2641955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档