diff options
Diffstat (limited to 'copy.js')
-rw-r--r-- | copy.js | 45 |
1 files changed, 33 insertions, 12 deletions
@@ -117,17 +117,38 @@ function replaceVariable(str){ if (!str) return ''; var win = new XPCNativeWrapper(window.content.window); var sel = '',htmlsel = ''; - if (str.indexOf('%SEL%') >= 0 || str.indexOf('%HTMLSEL%') >= 0){ - sel = win.getSelection().rangeCount()>0? win.getSelection().getRangeAt(0): ''; - } - if (str.indexOf('%HTMLSEL%') >= 0){ - var serializer = new XMLSerializer(); - htmlsel = serializer.serializeToString(sel.cloneContents()); - } - return str.replace(/%TITLE%/g,buffer.title) - .replace(/%URL%/g,buffer.URL) - .replace(/%SEL%/g,sel.toString()) - .replace(/%HTMLSEL%/g,htmlsel); + var selection = win.getSelection(); + function replacer(value){ //{{{ + switch(value){ + case '%TITLE%': + return buffer.title; + case '%URL%': + return buffer.URL; + case '%SEL%': + if (sel) + return sel; + else if (selection.rangeCount < 1) + return ''; + + for (var i=0, c=selection.rangeCount; i<c; i++){ + sel += selection.getRangeAt(i).toString(); + } + return sel; + case '%HTMLSEL%': + if (htmlsel) + return sel; + else if (selection.rangeCount < 1) + return ''; + + var serializer = new XMLSerializer(); + for (var i=0, c=selection.rangeCount; i<c; i++){ + htmlsel += serializer.serializeToString(selection.getRangeAt(i).cloneContents()); + } + return htmlsel; + } + return ''; + } //}}} + return str.replace(/%(TITLE|URL|SEL|HTMLSEL)%/g, replacer); } var exCopyManager = { @@ -168,7 +189,7 @@ var exCopyManager = { } } else { if (!arg) arg = liberator.globalVariables.copy_templates[0]; - var template = getCopyTemplate(arg) || arg; + var template = getCopyTemplate(arg) || {value: arg}; if (typeof template.custom == 'function'){ copyString = template.custom.call(this, template.value); } else if (template.custom instanceof Array){ |