diff options
author | drry | 2008-04-23 15:44:07 +0000 |
---|---|---|
committer | drry | 2008-04-23 15:44:07 +0000 |
commit | d4e266a8b1d3e32533e87719c44678a84710b5af (patch) | |
tree | 22eeab216fdf1b8d4b4bd0bd1586ab9f909dea94 | |
parent | 1dfde42b4c3ef33b844a4bbf0f9acd53905e86fe (diff) | |
download | vimperator-plugins-d4e266a8b1d3e32533e87719c44678a84710b5af.tar.bz2 |
lang/javascript/vimperator-plugins/trunk/copy.js:
* `Array` オブジェクトに対する `for each` 文を回避しました。
* インデントの混在を統一しました。
* ほか。
git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@10226 d0d07461-0603-4401-acd4-de1884942a52
-rw-r--r-- | copy.js | 158 |
1 files changed, 78 insertions, 80 deletions
@@ -12,7 +12,7 @@ * :copy {copyString} -> copy the argument replaced some certain string * :copy! {expr} -> evaluate the argument and copy the result * - * ex) + * e.g.) * :copy %TITLE% -> copied the title of the current page * :copy title -> same as `:copy %TITLE%' by default * :copy! liberator.version -> copy the value of liberator.version @@ -35,22 +35,22 @@ * The copy_templates is a string variable which can set on * vimperatorrc as following. * - * let copy_templates = "[ {label:'titleAndURL', value:'%TITLE%\n%URL%'}, {label:'title', value:'%TITLE%'} ]" + * let copy_templates = "[{ label: 'titleAndURL', value: '%TITLE%\n%URL%' }, { label: 'title', value: '%TITLE%' }]" * * or your can set it using inline JavaScript. * * javascript <<EOM - * liberator.globalVariables.copy_templates = uneval([ + * liberator.globalVariables.copy_templates = [ * { label: 'titleAndURL', value: '%TITLE%\n%URL%' }, * { label: 'title', value: '%TITLE%' }, * { label: 'anchor', value: '<a href="%URL%">%TITLE%</a>' }, * { label: 'selanchor', value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' }, - * { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' }, - * ]); + * { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' } + * ]; * EOM */ (function(){ -if (!liberator.globalVariables.copy_templates) { +if (!liberator.globalVariables.copy_templates){ liberator.globalVariables.copy_templates = [ { label: 'titleAndURL', value: '%TITLE%\n%URL%' }, { label: 'title', value: '%TITLE%' }, @@ -62,84 +62,82 @@ if (!liberator.globalVariables.copy_templates) { // used when argument is none //const defaultValue = templates[0].label; liberator.commands.addUserCommand(['copy'],'Copy to clipboard', - function(arg, special){ - var copyString = ''; - var isError = false; - if (special && arg){ - try { - copyString = window.eval('with(liberator){' + arg + '}'); - switch (typeof copyString){ - case 'object': - copyString = copyString === null ? 'null' : copyString.toSource(); - break; - case 'function': - copyString = copyString.toString(); - break; - case 'number': - case 'boolean': - copyString = '' + copyString; - break; - case 'undefined': - copyString = 'undefined'; - break; - } - } catch(e){ - isError = true; - copyString = e.toString(); - } - } else { - if (!arg){ arg = liberator.globalVariables.copy_templates[0].label; } - var str = getCopyTemplate(arg) || arg; - copyString = replaceVariable(str); - } - liberator.copyToClipboard(copyString); - if (isError){ - liberator.echoerr('CopiedErrorString: `' + copyString + "'"); - } else { - liberator.echo('CopiedString: `' + liberator.util.escapeHTML(copyString) + "'"); - } - },{ - completer: function(filter, special){ - if (special){ - return liberator.completion.javascript(filter); - } - var templates = liberator.globalVariables.copy_templates.map(function(template){ - return [template.label, template.value]; - }); - if (!filter){ return [0,templates]; } - var candidates = []; - templates.forEach(function(template){ - if (template[0].toLowerCase().indexOf(filter.toLowerCase()) == 0){ - candidates.push(template); - } - }); - return [0, candidates]; - } - } + function(arg, special){ + var copyString = ''; + var isError = false; + if (special && arg){ + try { + copyString = window.eval('with(liberator){' + arg + '}'); + switch (typeof copyString){ + case 'object': + copyString = copyString === null ? 'null' : copyString.toSource(); + break; + case 'function': + copyString = copyString.toString(); + break; + case 'number': + case 'boolean': + copyString = '' + copyString; + break; + case 'undefined': + copyString = 'undefined'; + break; + } + } catch (e){ + isError = true; + copyString = e.toString(); + } + } else { + if (!arg){ arg = liberator.globalVariables.copy_templates[0].label; } + var str = getCopyTemplate(arg) || arg; + copyString = replaceVariable(str); + } + liberator.copyToClipboard(copyString); + if (isError){ + liberator.echoerr('CopiedErrorString: `' + copyString + "'"); + } else { + liberator.echo('CopiedString: `' + liberator.util.escapeHTML(copyString) + "'"); + } + },{ + completer: function(filter, special){ + if (special){ + return liberator.completion.javascript(filter); + } + var templates = liberator.globalVariables.copy_templates.map(function(template) + [template.label, template.value] + ); + if (!filter){ return [0,templates]; } + var candidates = []; + templates.forEach(function(template){ + if (template[0].toLowerCase().indexOf(filter.toLowerCase()) == 0){ + candidates.push(template); + } + }); + return [0, candidates]; + } + } ); function getCopyTemplate(label){ - for each(var template in liberator.globalVariables.copy_templates){ - if ( template.label == label ){ - return template.value; - } - } - return null; + var ret = null; + liberator.globalVariables.copy_templates.some(function(template) + template.label == label ? (ret = template.value) && true : false); + return ret; } 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().getRangeAt(0); - } - if (str.indexOf('%HTMLSEL%') >= 0){ - var serializer = new XMLSerializer(); - htmlsel = serializer.serializeToString(sel.cloneContents()); - } - return str.replace(/%TITLE%/g,liberator.buffer.title) - .replace(/%URL%/g,liberator.buffer.URL) - .replace(/%SEL%/g,sel.toString()) - .replace(/%HTMLSEL%/g,htmlsel); + 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().getRangeAt(0); + } + if (str.indexOf('%HTMLSEL%') >= 0){ + var serializer = new XMLSerializer(); + htmlsel = serializer.serializeToString(sel.cloneContents()); + } + return str.replace(/%TITLE%/g,liberator.buffer.title) + .replace(/%URL%/g,liberator.buffer.URL) + .replace(/%SEL%/g,sel.toString()) + .replace(/%HTMLSEL%/g,htmlsel); } })(); |