2 <plugin name="copy" version="0.7.6"
3 href="http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/copy.js"
4 summary="copy strings from the template (like CopyURL+)"
5 xmlns="http://vimperator.org/namespaces/liberator">
6 <author email="teramako@gmail.com">teramako</author>
7 <license>MPL 1.1/GPL 2.0/LGPL 2.1</license>
8 <project name="Vimperator" minVersion="2.3"/>
11 <spec>:copy <a>label</a></spec>
13 <p>copy the argument replaced some certain string.</p>
18 <spec>:copy! <a>expr</a></spec>
20 <p>evaluate the argument(javascript code) and copy the result.</p>
24 <tags>copy-keyword</tags>
25 <spec>copy-keyword</spec>
27 <p>replaces following keywords</p>
30 <dd>to the title of the current page</dd>
32 <dd>to the currenet URL</dd>
34 <dd>to the string of selection</dd>
36 <dd>to the html string of selection</dd>
38 <dd>to the hostname of the current location</dd>
40 <dd>to the pathname of the current location</dd>
42 <dd>to the host of the current location</dd>
44 <dd>to the port of the current location</dd>
46 <dd>to the protocol of the current location</dd>
48 <dd>to the search(?...) of the curernt location</dd>
50 <dd>to the hash(anchor #..) of the current location</dd>
55 <tags>copy-template</tags>
56 <spec>copy-template</spec>
58 <p>you can set your own template using inline JavaScript</p>
61 liberator.globalVariables.copy_templates = [
62 { label: 'titleAndURL', value: '%TITLE%\n%URL%' },
63 { label: 'title', value: '%TITLE%', map: ',y' },
64 { label: 'anchor', value: '<a href="%URL%">%TITLE%</a>' },
65 { label: 'selanchor', value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' },
66 { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' }
67 { label: 'ASIN', value: 'copy ASIN code from Amazon', custom: function(){return content.document.getElementById('ASIN').value;} },
73 <dd>template name which is command argument</dd>
75 <dd>copy string. <a>copy-keyword</a> is replaced</dd>
77 <dd>key map <a>lhs</a> (optional)</dd>
80 <a>function</a> or <a>Array</a> (optional)
82 <dt><a>function</a></dt>
83 <dd>execute the function and copy return value, if specified</dd>
86 replace to the <a>value</a> by normal way at first.
87 then replace words matched <a>Array</a>[0] in the repalced string to <a>Array</a>[1].
89 <dt><a>Array</a>[0]</dt>
90 <dd>String or RegExp</dd>
91 <dt><a>Array</a>[1]</dt>
92 <dd>String or Function</dd>
94 see: <link topic="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace">http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace</link>
102 <tags>copy-option</tags>
103 <spec>copy-option</spec>
105 <code><ex>liberator.globalVariables.copy_use_wedata = false; // false by default</ex></code>
106 <p>true に設定すると wedata からテンプレートを読込みます。</p>
107 <code><ex>liberator.globalVariables.copy_wedata_include_custom = true; // false by default</ex></code>
108 <p>custom が設定された wedata を読込みます。
109 SandBox でなく、window.eval を利用してオブジェクトする為、
110 セキュリティ上の理由で初期設定は false になっています。
111 true に設定する場合は、動作を理解したうえ自己責任でご利用ください。</p>
112 <code><ex>liberator.globalVariables.copy_wedata_exclude_labels = ['pathtraqnormalize', ];</ex></code>
113 <p>wedata から読込まない label のリストを定義します。</p>
120 <description>enable to copy strings from a template (like CopyURL+)</description>
121 <description lang="ja">テンプレートから文字列のコピーを可能にします(CopyURL+みたいなもの)</description>
122 <minVersion>2.0pre</minVersion>
123 <maxVersion>2.0pre</maxVersion>
124 <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/copy.js</updateURL>
125 <author mail="teramako@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/teramako/">teramako</author>
126 <license>MPL 1.1/GPL 2.0/LGPL 2.1</license>
127 <version>0.7.5</version>
130 liberator.plugins.exCopy = (function(){
131 var excludeLabelsMap = {};
132 var copy_templates = [];
133 if (!liberator.globalVariables.copy_templates){
134 liberator.globalVariables.copy_templates = [
135 { label: 'titleAndURL', value: '%TITLE%\n%URL%' },
136 { label: 'title', value: '%TITLE%' },
137 { label: 'anchor', value: '<a href="%URL%">%TITLE%</a>' },
138 { label: 'selanchor', value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' },
139 { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' }
143 copy_templates = liberator.globalVariables.copy_templates.map(function(t){
144 return { label: t.label, value: t.value, custom: t.custom, map: t.map }
147 copy_templates.forEach(function(template){
148 if (typeof template.map == 'string')
149 addUserMap(template.label, [template.map]);
150 else if (template.map instanceof Array)
151 addUserMap(template.label, template.map);
154 const REPLACE_TABLE = {
155 get TITLE () buffer.title,
156 get URL () buffer.URL,
159 var win = new XPCNativeWrapper(window.content.window);
160 var selection = win.getSelection();
161 if (selection.rangeCount < 1)
164 for (var i=0, c=selection.rangeCount; i<c; i++){
165 sel += selection.getRangeAt(i).toString();
171 var win = new XPCNativeWrapper(window.content.window);
172 var selection = win.getSelection();
173 if (selection.rangeCount < 1)
176 var serializer = new XMLSerializer();
177 for (var i=0, c=selection.rangeCount; i<c; i++){
178 htmlsel += serializer.serializeToString(selection.getRangeAt(i).cloneContents());
180 return htmlsel.replace(/<(\/)?(\w+)([\s\S]*?)>/g, function(all, close, tag, attr){
181 return "<" + close + tag.toLowerCase() + attr + ">";
185 return util.readFromClipboard();
188 'hostname pathname host port protocol search hash'.split(' ').forEach(function (name){
189 REPLACE_TABLE[name.toUpperCase()] = function () content.location && content.location[name];
192 // used when argument is none
193 //const defaultValue = templates[0].label;
194 commands.addUserCommand(['copy'],'Copy to clipboard',
196 liberator.plugins.exCopy.copy(args.literalArg, args.bang, !!args["-append"]);
198 completer: function(context, args){
200 completion.javascript(context);
203 context.title = ['Template','Value'];
204 var templates = copy_templates.map(function(template)
205 [template.label, liberator.modules.util.escapeString(template.value, '"')]
207 if (!context.filter){ context.completions = templates; return; }
209 var filter = context.filter.toLowerCase();
210 context.completions = templates.filter(function(template) template[0].toLowerCase().indexOf(filter) == 0);
215 [["-append","-a"], commands.OPTION_NOARG]
221 function addUserMap(label, map){
222 mappings.addUserMap([modes.NORMAL,modes.VISUAL], map,
224 function(){ liberator.plugins.exCopy.copy(label); },
228 function getCopyTemplate(label){
230 copy_templates.some(function(template)
231 template.label == label ? (ret = template) && true : false);
234 function replaceVariable(str){
236 function replacer(orig, name){ //{{{
239 if (!REPLACE_TABLE.hasOwnProperty(name))
241 let value = REPLACE_TABLE[name];
242 if (typeof value == 'function')
245 return value.toString();
248 return str.replace(/%([A-Z]*)%/g, replacer);
251 function wedataRegister(item){
252 var libly = liberator.plugins.libly;
253 var logger = libly.$U.getLogger("copy");
255 if (excludeLabelsMap[item.label]) return;
257 if (item.custom && item.custom.toLowerCase().indexOf('function') != -1) {
258 if (!liberator.globalVariables.copy_wedata_include_custom ||
259 item.label == 'test') {
260 logger.log('skip: ' + item.label);
264 let custom = (function(item){
266 return function(value, value2){
267 var STORE_KEY = 'plugins-copy-ok-func';
268 var store = storage.newMap(STORE_KEY, true);
269 var check = store.get(item.label);
273 ans = window.confirm(
274 'warning!!!: execute "' + item.label + '" ok ?\n' +
275 '(this function is working with unsafe sandbox.)\n\n' +
276 '----- execute code -----\n\n' +
277 'value: ' + item.value + '\n' +
282 if (item.value == check.value &&
283 item.custom == check.custom &&
284 item.map == check.map){
287 ans = window.confirm(
288 'warning!!!: "' + item.label + '" was changed when you registered the function.\n' +
289 '(this function is working with unsafe sandbox.)\n\n' +
290 '----- execute code -----\n\n' +
291 'value: ' + item.value + '\n' +
299 store.set(item.label, item);
304 func = window.eval('(' + item.custom + ')');
307 logger.log(item.custom);
310 return func(value, value2);
314 exCopyManager.add(item.label, item.value, custom, item.map);
316 exCopyManager.add(item.label, item.value, null, item.map);
319 var exCopyManager = {
320 add: function(label, value, custom, map){
321 var template = {label: label, value: value, custom: custom, map: map};
322 copy_templates.unshift(template);
323 if (map) addUserMap(label, map);
327 get: function(label){
328 return getCopyTemplate(label);
330 copy: function(arg, special, appendMode){
335 copyString = liberator.eval(arg);
336 switch (typeof copyString){
338 copyString = copyString === null ? 'null' : copyString.toSource();
341 copyString = copyString.toString();
345 copyString = '' + copyString;
348 copyString = 'undefined';
353 copyString = e.toString();
356 if (!arg) arg = copy_templates[0].label;
358 var template = getCopyTemplate(arg) || {value: arg};
359 if (typeof template.custom == 'function'){
360 copyString = template.custom.call(this, template.value, replaceVariable(template.value));
361 } else if (template.custom instanceof Array){
362 copyString = replaceVariable(template.value).replace(template.custom[0], template.custom[1]);
364 copyString = replaceVariable(template.value);
369 copyString = util.readFromClipboard() + copyString;
373 util.copyToClipboard(copyString);
375 liberator.echoerr('CopiedErrorString: `' + copyString + "'");
377 liberator.echo('CopiedString: `' + util.escapeHTML(copyString || '') + "'");
382 if (liberator.globalVariables.copy_use_wedata){
383 function loadWedata(){
384 if (!liberator.plugins.libly){
385 liberator.echomsg("need a _libly.js when use wedata.");
389 var libly = liberator.plugins.libly;
390 copy_templates.forEach(function(item) excludeLabelsMap[item.label] = item.value);
391 if (liberator.globalVariables.copy_wedata_exclude_labels)
392 liberator.globalVariables.copy_wedata_exclude_labels.forEach(function(item) excludeLabelsMap[item] = 1);
393 var wedata = new libly.Wedata("vimp%20copy");
394 wedata.getItems(24 * 60 * 60 * 1000, wedataRegister);
399 return exCopyManager;
402 // vim: set fdm=marker sw=4 ts=4 et: