Revisions

gist: 147362 Download_button fork
public
Public Clone URL: git://gist.github.com/147362.git
Embed All Files: show embed
JavaScript #
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
/*
Copyright (c) 2009 Mike Chambers
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
 
CmdUtils.CreateCommand({
names: ["fyi"],
homepage:"http://www.mikechambers.com/ubiquity/fyi/",
author:{name:"Mike Chambers", email:"mikechambers@gmail.com"},
license:"MIT",
version:".90",
//takes: {"to (optional)": noun_arb_text},
arguments: [{role:"object", nountype:noun_arb_text, label:"to (optional)"}],
 
description:"Creates an email with information about the current page and selection.",
 
help:"If text from the current page is selected, the command will create a new email with the selected text and current location / url in the body, with the page title as the subject. If no text is selected, the current location and title will be included. The command takes an optional email address argument.",
 
//second argument is either:
//-argument to command
//-text selection
//-argument used in last command
//there is no way to tell, so this can be a little buggy
preview: function(pblock, arguments)
{
var email = arguments.object;
 
var doc = this.getDocument();
var sel = this.getSelection();
 
//this is a hack since there is no way to tell what the second argument
//actually contains
var to = (email.text.indexOf("@") >= 0)?email.text:"";
 
 
//generate the preview
var h = "<p><strong>TO:</strong> " + to +"<br />" +
"<strong>SUBJECT:</strong> fyi : " + doc.title + " <br />" +
"<strong>BODY:</strong></p>" +
"<p>" + doc.location + "</p>";
 
//check if the user selected any text
if(sel != "")
{
//if so, add it to the preview
h += "<p>--<br />" + sel + "<br />--</p>";
}
 
pblock.innerHTML = h;
},
 
execute: function( arguments )
{
//check if an email address was specified (we dont validate)
 
var email = arguments.object;
var to;
if(email == undefined)
{
to = "";
}
else
{
to = (email.text.indexOf("@") >= 0)?email.text:"";
}
 
//get the current document
var doc = this.getDocument();
 
 
var title = escape("fyi : " + doc.title);
 
//check if we are running on Mac
if(CmdUtils.getWindow().navigator.platform.indexOf("Mac") != -1)
{
//the replace is because the default escape for the &raquo; (ยป) char
// is %BB. However, if this is in the URL, the things dont work.
//passing in %C2%BB instead works, and renders the char correctly.
//not sure why
title = title.replace(/%BB/g, "%C2%BB");
}
 
//grab the text selection
var sel = this.getSelection();
 
//url to hold our mailto url info
//no email, so user can enter email
var url = "mailto:" + to + "?subject=" +
//add title
title;
 
//add url to body
url += "&body=" + escape(doc.location + "\n");
 
//if text is selected
if(sel != "")
{
//add selection to body
url +=
escape("\n--\n" +
sel +
"\n--\n\n");
}
 
//tell the browser to open the mailto url (and thus open the default
//email client)
//CmdUtils.log(url);
doc.location.href = url;
//Utils.openUrlInBrowser(url);
},
 
getDocument:function()
{
return CmdUtils.getDocumentInsecure();
},
 
getSelection:function()
{
return CmdUtils.getWindow().getSelection();
},
})