powchin (owner)

Fork Of

gist: 99201 by satyr Builds a text (from the pag...

Forks

Revisions

gist: 137280 Download_button fork
public
Description:
Builds a text (from the page title etc.) and sends it to your clipboard.
Public Clone URL: git://gist.github.com/137280.git
Embed All Files: show embed
copy.ubiq.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const Aliases = {
  t: {
    help: 'title',
    get text() info().title,
  },
  u: {
    help: 'URL',
    get text() info().url,
  },
  p: {
    help: 'selection as HTML',
    get text() CmdUtils.getSelection(),
  },
  a: {
    help: 'title and URL as link tag',
    get text(){ with(info()) return <a href={url}>{title}</a>.toXMLString() },
  },
  f: {
    help: 'favicon as data:image/png',
    get text() img2du(Utils.currentChromeWindow.gBrowser.selectedTab.image),
  },
  s: {
    help: 'selection as anchor text',
    get text(){
      with(info())
        return ((<a href={url} title={title}><x/></a>+'')
                .replace(/\s*<x\/>\s{0,}/, CmdUtils.getSelection()));
    }
  },
  h: {
    help: 'selection as HTML',
    get text() CmdUtils.getHtmlSelection(),
  },
  q: {
    help: 'selection as blockquote',
    get text(){
      with(info())
        return ((<blockquote cite={url} title={title}><x/></blockquote>+'')
                .replace(/\s*<x\/>\s{0,}/, CmdUtils.getHtmlSelection()));
    }
  },
  m: {
    help: 'selection as markdown',
    get text(){
      with(info())
        return ('['+CmdUtils.getSelection()+']('+url+' "'+title+'")');
    }
  },
  i: {
    help: 'selected image (or "1x1 transparent" if none) as data:image/png',
    get text(){
      var doc = CmdUtils.getDocument();
      return img2du(
        (/^image/.test(doc.contentType)
         ? doc.URL
         : (/src=([\"\']?)(\S+?)\1/(CmdUtils.getHtmlSelection())||0)[2]),
        ('data:image/png;base64,'+
         'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAA'+
         'AAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII='));
    }
  },
  j: {
    help: 'snapshot of current tab as data:image/jpeg',
    get text() CmdUtils.getWindowSnapshot(CmdUtils.getWindow(), {width: 600}),
  },
  M: {
    help: 'selection as MD5 hash',
    get text() Utils.computeCryptoHash('MD5', CmdUtils.getSelection()),
  },
  S: {
    help: 'selection as SHA1 hash',
    get text() Utils.computeCryptoHash('SHA1', CmdUtils.getSelection()),
  },
  d: {
    help: 'current datetime in RFC3399 format',
    get text() new Date().toString('yyyy-MM-ddThh:mm:ddZ'),
  },
  J: {
    help: 'URL as JSON',
    get text(){
      var {url} = info(), o = {url: url},
      a = context.focusedWindow.document.createElement('a');
      a.href = url;
      for each(var p in ['protocol', 'host', 'pathname', 'search', 'hash'])
        o[p] = a[p];
      o.query = o.search ? Utils.urlToParams(o.search) : {};
      return Utils.encodeJson(o);
    },
  },
  X: {
    help: 'selection as hex',
    get text(){
      return CmdUtils.getSelection()
       .replace(/[^ -~]|\\/g, function(m0){
          var code = m0.charCodeAt(0);
          return '\\u' + ((code < 0x10) ? '000' :
                          (code < 0x100) ? '00' :
                          (code < 0x1000) ? '0' : '')
                          + code.toString(16); });
    }
  },
  x: {
    help: 'selected hex as unescaped',
    get text(){
      return CmdUtils.getSelection()
        .replace(/\\u([a-fA-F0-9]{4})/g, function(m0, m1){
          return String.fromCharCode(parseInt(m1, 16)); });
    }
  },
  e: {
    help: 'selection as EncodeURIComponent (UTF-8)',
    get text(){
      return encodeURIComponent(CmdUtils.getSelection());
    }
  },
  d: {
    help: 'selection as DecodeURIComponent (UTF-8)',
    get text(){
      return decodeURIComponent(CmdUtils.getSelection());
    }
  },
  n: {
    help: 'line feed character',
    text: '\n',
  },
  T: {
    help: 'tab character',
    text: '\t',
  },
  /*
_: {
help: '',
get text(){},
},
*/
},
REA = RegExp('['+ [k for(k in Aliases)] +']', 'g'),
Help = ('<ul style="list-style:none; margin:0.2em; padding:0">'+
        [<li><tt><b>{k}</b>:</tt>{v.help}</li>
          for([k, v] in Iterator(Aliases))].join('') +
        '</ul>'),
DUCache = {'': 'data:,...'};
function img2du(src, ph)(
  DUCache[src] ||
  (CmdUtils.getImageSnapshot(src, function(du){ DUCache[src] = du }),
   DUCache[src] = ph || DUCache['']));
function info(){
  var t, u, {top, document: {title, URL}} = CmdUtils.getWindow();
  (/^https?:\/\/www\.google\.\w+\/reader\/view/.test(URL)
   ? ({title: t, url: u}) = top.wrappedJSObject.getPermalink() :
   /^https?:\/\/(?:reader\.livedoo|fastladde)r\.com\/reader/.test(URL)
   ? ({title: t, link: u}) = top.wrappedJSObject.get_active_item(1) : 0);
  return {title: t || title, url: u || URL};
}
function parse(txt) txt.replace(REA, replr);
function replr(m) Aliases[m].text;
CmdUtils.CreateCommand({
  name: 'copy',
  icon: 'chrome://ubiquity/skin/icons/convert.png',
  argument: {
    label: 'copy text',
    rankLast: true,
    default: function() this._sugg(Bin.lastInput() || ''),
    suggest: function(txt, htm, cb, sx){
      if(sx || !txt) return [];
      return [this._sugg(Bin.lastInput(txt))];
    },
    _sugg: function(t)({text: t, summary: t.replace(REA, this._mark)}),
    _mark: function($) <u>{$}</u>.toXMLString(),
  },
  description: ('Collects various informations into a text'+
                ' and sends it to your clipboard.'),
  help: (<>Try "copy tnu" to copy the page title and url
         separated by a new line.<br/><br/>
         [ Aliases ]</> + Help),
  execute: function copy_execute({object: {text}}){
    if(!text) return;
    displayMessage(Utils.clipboard.text = parse(text), this);
  },
  preview: function copy_preview(pb, {object: {text}}){
    pb.innerHTML =
      <pre>{parse(text) || <em>empty</em>}</pre>.toXMLString() + Help;
  },
  previewDelay: 200,
  author: 'satyr',
  contributors: [{name: 'powchin', homepage: 'http://friendfeed.com/powchin'}],
  license: 'MIT',
});
 
const noun_copyee = CmdUtils.NounType('...', {
  '@href': {
    help: 'page link "href"',
    get texts() [x.href
                 for each(x in $('a[href], link[href]',
                                 CmdUtils.getDocument()).get())],
    get htmls() [(<a href={t}>{t}</a>).toXMLString()
                 for each(t in this.texts)],
  },
  'img@src': {
    help: 'page image "src"',
    get texts() [x.src
                 for each(x in $('img[src]', CmdUtils.getDocument()).get())],
    get htmls() [(<img src={t} style="max-width:90%"/>).toXMLString()
                 for each(t in this.texts)],
  },
  tabs: {
    help: 'tab title and URL',
    get texts() [d.title +'\n'+ d.URL
                 for each(d in this._docs)],
    get htmls() [(<a href={d.URL}>{d.title}</a>).toXMLString()
                 for each(d in this._docs)],
    get _docs() (t.document for each(t in Utils.tabs.get())),
  },
  feeds: {
    help: 'page feeds',
    get texts() [l.href for each(l in this._lnks)],
    get htmls() [(<a href={l.href}>{l.title || l.href}</a>) +
                 (<span> ({l.tipe})</span>)
                 for each(l in this._lnks)],
    get _lnks() Array.filter(
      CmdUtils.getDocument().querySelectorAll('link[rel="alternate"]'),
      function(l) (/\b(?:rss|atom|xml)\b/i(l.type) &&
                   (l.tipe = RegExp.lastMatch))),
  },
  /*
_: {
help: '',
get texts() [],
get htmls() [],
},
*/
});
CmdUtils.CreateCommand({
  name: 'copy all',
  icon: 'chrome://ubiquity/skin/icons/convert.png',
  argument: noun_copyee,
  description:
  'Copies to clipboard a newline separated list of ...',
  help: ''+ (
    [<><dt><b><code>{s.text}</code></b></dt><dd>{s.data.help}</dd></>
     for each(s in noun_copyee._list)]
      .reduce(function(l, x) l.appendChild(x), <dl/>)),
  execute: function copyall_execute({object: {text, data}}){
    if(!text) return;
    var {texts} = data;
    CmdUtils.copyToClipboard(texts.join('\n'));
    displayMessage(text +' (x'+ texts.length +')', this);
  },
  preview: function copyall_preview(pb, {object: {text, data}}){
    if(!text) return void this.previewDefault(pb);
    CmdUtils.previewList(pb, data.htmls, function copyall_list(i){
      displayMessage(Utils.clipboard.text = data.texts[i], this);
    });
  },
  author: 'satyr', license: 'MIT',
});