forked from dsernst/lfx-post
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
103 lines (71 loc) · 2.34 KB
/
popup.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
// Letsfix Chrome Extension
//
// Share URL with crowd.
// Connect with other who are feeling what you're feeling at that exact moment.
// by: letsfix.net
var gatherTabInfo = function(){
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function(tabs){
var url = tabs[0].url;
var title = tabs[0].title;
passInfoToPopup(url, title);
});
};
var passInfoToPopup = function(url, title){
$('#pageURL').html(url);
// $('#pageURL').attr('href', url); // commented out due to focus bug.
$('#pageTitle').html(title);
$('#commentField').focus();
};
document.addEventListener('DOMContentLoaded', function() {
$('#submitComment').click(function (e) {
e.preventDefault();
collectComment();
});
});
var collectComment = function(){
var url = $('#pageURL').html();
var title = $('#pageTitle').html();
// escape special characters to prep for base64 encoding
url = encodeWeirdChars(url);
title = encodeWeirdChars(title);
var comment = $('#commentField').val();
buildMessage(url, title, comment);
}
var encodeWeirdChars = function(string){
var encoded = encodeURI(string);
var normalSpaces = encoded.replace(/%20/g," ");
return normalSpaces;
};
var buildMessage = function(url, title, comment){
// Build email parameters (to, subject, body)
var message = "To: <[email protected]>\nSubject: [Lfx-post] " + title + "\n\n" + url + "\n\n" + comment;
console.log(message);
auth(message);
};
var auth = function(message){
// Ask user permission to authorize Gmail OAuth API
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
console.log(token);
sendMessage(message, noteSuccess() );
});
};
// Send mail through SMTP with Gmail OAuth API
// see https://developers.google.com/gmail/api/v1/reference/users/messages/send
var sendMessage = function(message, callback){
var base64EncodedEmail = btoa(message);
var request = gapi.client.gmail.users.messages.send({
// 'me' is a special value, uses authenticated user
'userId': 'me',
'message': {
'raw': base64EncodedEmail
}
});
request.execute(callback);
};
var noteSuccess = function(){
document.body.innerHTML = "<h3>Success...?</h3>";
};
// Begin running our script as soon as the document's DOM is ready.
document.addEventListener('DOMContentLoaded', function () {
gatherTabInfo();
});