2 xml`<plugin name="copy" version="0.7.6"
3 href="http://github.com/vimpr/vimperator-plugins/blob/master/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>
118 liberator.plugins.exCopy = (function(){
119 var excludeLabelsMap = {};
120 var copy_templates = [];
121 if (!liberator.globalVariables.copy_templates){
122 liberator.globalVariables.copy_templates = [
123 { label: 'titleAndURL', value: '%TITLE%\n%URL%' },
124 { label: 'title', value: '%TITLE%' },
125 { label: 'anchor', value: '<a href="%URL%">%TITLE%</a>' },
126 { label: 'selanchor', value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' },
127 { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' }
131 copy_templates = liberator.globalVariables.copy_templates.map(function(t){
132 return { label: t.label, value: t.value, custom: t.custom, map: t.map }
135 copy_templates.forEach(function(template){
136 if (typeof template.map == 'string')
137 addUserMap(template.label, [template.map]);
138 else if (template.map instanceof Array)
139 addUserMap(template.label, template.map);
142 const REPLACE_TABLE = {
143 get TITLE () buffer.title,
144 get URL () buffer.URL,
147 var win = new XPCNativeWrapper(window.content.window);
148 var selection = win.getSelection();
149 if (selection.rangeCount < 1)
152 for (var i=0, c=selection.rangeCount; i<c; i++){
153 sel += selection.getRangeAt(i).toString();
159 var win = new XPCNativeWrapper(window.content.window);
160 var selection = win.getSelection();
161 if (selection.rangeCount < 1)
164 var serializer = new XMLSerializer();
165 for (var i=0, c=selection.rangeCount; i<c; i++){
166 htmlsel += serializer.serializeToString(selection.getRangeAt(i).cloneContents());
168 return htmlsel.replace(/<(\/)?(\w+)([\s\S]*?)>/g, function(all, close, tag, attr){
169 return "<" + close + tag.toLowerCase() + attr + ">";
173 return util.readFromClipboard();
176 'hostname pathname host port protocol search hash'.split(' ').forEach(function (name){
177 REPLACE_TABLE[name.toUpperCase()] = function () content.location && content.location[name];
180 // used when argument is none
181 //const defaultValue = templates[0].label;
182 commands.addUserCommand(['copy'],'Copy to clipboard',
184 liberator.plugins.exCopy.copy(args.literalArg, args.bang, !!args["-append"]);
186 completer: function(context, args){
188 completion.javascript(context);
191 context.title = ['Template','Value'];
192 var templates = copy_templates.map(function(template)
193 [template.label, liberator.modules.util.escapeString(template.value, '"')]
195 if (!context.filter){ context.completions = templates; return; }
197 var filter = context.filter.toLowerCase();
198 context.completions = templates.filter(function(template) template[0].toLowerCase().indexOf(filter) == 0);
203 [["-append","-a"], commands.OPTION_NOARG]
209 function addUserMap(label, map){
210 mappings.addUserMap([modes.NORMAL,modes.VISUAL], map,
212 function(){ liberator.plugins.exCopy.copy(label); },
216 function getCopyTemplate(label){
218 copy_templates.some(function(template)
219 template.label == label ? (ret = template) && true : false);
222 function replaceVariable(str){
224 function replacer(orig, name){ //{{{
227 if (!REPLACE_TABLE.hasOwnProperty(name))
229 let value = REPLACE_TABLE[name];
230 if (typeof value == 'function')
233 return value.toString();
236 return str.replace(/%([A-Z]*)%/g, replacer);
239 function wedataRegister(item){
240 var libly = liberator.plugins.libly;
241 var logger = libly.$U.getLogger("copy");
243 if (excludeLabelsMap[item.label]) return;
245 if (item.custom && item.custom.toLowerCase().indexOf('function') != -1) {
246 if (!liberator.globalVariables.copy_wedata_include_custom ||
247 item.label == 'test') {
248 logger.log('skip: ' + item.label);
252 let custom = (function(item){
254 return function(value, value2){
255 var STORE_KEY = 'plugins-copy-ok-func';
256 var store = storage.newMap(STORE_KEY, {store: true});
257 var check = store.get(item.label);
261 ans = window.confirm(
262 'warning!!!: execute "' + item.label + '" ok ?\n' +
263 '(this function is working with unsafe sandbox.)\n\n' +
264 '----- execute code -----\n\n' +
265 'value: ' + item.value + '\n' +
270 if (item.value == check.value &&
271 item.custom == check.custom &&
272 item.map == check.map){
275 ans = window.confirm(
276 'warning!!!: "' + item.label + '" was changed when you registered the function.\n' +
277 '(this function is working with unsafe sandbox.)\n\n' +
278 '----- execute code -----\n\n' +
279 'value: ' + item.value + '\n' +
287 store.set(item.label, item);
292 func = window.eval('(' + item.custom + ')');
295 logger.log(item.custom);
298 return func(value, value2);
302 exCopyManager.add(item.label, item.value, custom, item.map);
304 exCopyManager.add(item.label, item.value, null, item.map);
307 var exCopyManager = {
308 add: function(label, value, custom, map){
309 var template = {label: label, value: value, custom: custom, map: map};
310 copy_templates.unshift(template);
311 if (map) addUserMap(label, map);
315 get: function(label){
316 return getCopyTemplate(label);
318 copy: function(arg, special, appendMode){
323 copyString = liberator.eval(arg);
324 switch (typeof copyString){
326 copyString = copyString === null ? 'null' : copyString.toSource();
329 copyString = copyString.toString();
333 copyString = '' + copyString;
336 copyString = 'undefined';
341 copyString = e.toString();
344 if (!arg) arg = copy_templates[0].label;
346 var template = getCopyTemplate(arg) || {value: arg};
347 if (typeof template.custom == 'function'){
348 copyString = template.custom.call(this, template.value, replaceVariable(template.value));
349 } else if (template.custom instanceof Array){
350 copyString = replaceVariable(template.value).replace(template.custom[0], template.custom[1]);
352 copyString = replaceVariable(template.value);
357 copyString = util.readFromClipboard() + copyString;
361 util.copyToClipboard(copyString);
363 liberator.echoerr('CopiedErrorString: `' + copyString + "'");
365 liberator.echomsg('CopiedString: `' + util.escapeHTML(copyString || '') + "'");
370 if (liberator.globalVariables.copy_use_wedata){
371 function loadWedata(){
372 if (!liberator.plugins.libly){
373 liberator.echomsg("need a _libly.js when use wedata.");
377 var libly = liberator.plugins.libly;
378 copy_templates.forEach(function(item) excludeLabelsMap[item.label] = item.value);
379 if (liberator.globalVariables.copy_wedata_exclude_labels)
380 liberator.globalVariables.copy_wedata_exclude_labels.forEach(function(item) excludeLabelsMap[item] = 1);
381 var wedata = new libly.Wedata("vimp copy");
382 wedata.getItems(24 * 60 * 60 * 1000, wedataRegister);
387 return exCopyManager;
390 // vim: set fdm=marker sw=4 ts=4 et: