2010-04-15 5 views
1

이미지를 가리키는 선택한 링크 또는 URL을 이미지 자체로 바꿀 수있는 유비 쿼트 명령을 작성하려고합니다. 그러나 CmdUtils.setSelection() 함수 (here에 설명되어 있음)는 html 태그가 선택 항목에 존재하면 아무 것도하지 않으므로 링크를 바꾸면 쓸모가 없습니다. 일반 텍스트를 선택하면 텍스트가 <img src="text"/> 태그로 대체되어 의도 한대로 정확하게 작동합니다. 누락 된 것이 있습니까? 아니면이 함수가 단순히 html을 대체 할 수 없습니까? 후자의 경우, 내가 할 수있는 기능이나 방법이 있습니까? 다른 조언도 환영합니다!Ubiquity CmdUtils.setSelection이 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)); 
    } 
}); 

답변

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(""); 
    }, 
}); 
관련 문제