我正在尝试编写一个Ubiquity命令,它允许您将指向图像的选定链接或URL替换为图像本身。但是,如果所选内容中存在任何html标记,CmdUtils.setSelection()函数(文档化的here)似乎不会执行任何操作,这使得它无法替换任何链接。选择纯文本时,它的功能与预期完全相同,即用<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));
}
});发布于 2010-08-22 06:16:32
只要传递了一个有效的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("");
},
});https://stackoverflow.com/questions/2641955
复制相似问题