gpt4 book ai didi

javascript - Ubiquity CmdUtils.setSelection 不会取代 HTML

转载 作者:行者123 更新时间:2023-11-28 02:55:59 25 4
gpt4 key购买 nike

我正在尝试编写一个 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));
}
});

最佳答案

只要传递有效的 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("");
},
});

关于javascript - Ubiquity CmdUtils.setSelection 不会取代 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2641955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com