-
Notifications
You must be signed in to change notification settings - Fork 10
/
link-to-inbox.js
126 lines (100 loc) · 2.71 KB
/
link-to-inbox.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.linkToInbox = factory();
}
}(this, function () {
var createLink = function(email, filter, onlyMatched, text){
text = text || 'Check your %s inbox';
var spec = getSpec(email, filter, onlyMatched);
if (!spec) {
return false;
}
var href = getHref(email, filter, onlyMatched);
var linkText = text.replace(/%s/ig, spec.name);
return '<a href="' + href + '" target="_blank">' + linkText + '</a>';
}
var getHref = function(email, filter, onlyMatched){
var spec = getSpec(email, filter, onlyMatched);
if (!spec) {
return false;
}
var href = spec.protocol + '://' + spec.domain + spec.path;
return href;
}
var getSpec = function(email, filter, onlyMatched){
var matched = true;
var domain = email.split('@')[1];
var parsedFilters = null;
var spec = {
name: domain,
protocol: 'https',
domain: domain,
path: ''
};
if (filter) {
parsedFilters = {};
if (filter.subject) {
parsedFilters.subject = encodeURIComponent(filter.subject);
}
if (filter.sender) {
parsedFilters.sender = encodeURIComponent(filter.sender);
}
}
switch (domain) {
case 'gmail.com':
case 'googlemail.com':
spec.name = 'Gmail';
spec.domain = 'mail.google.com';
spec.path = '/mail/u/0/';
if (parsedFilters) {
spec.path += '#search/in%3Aanywhere';
if (parsedFilters.subject) {
spec.path += '+subject%3A%22' + parsedFilters.subject + '%22';
}
if (parsedFilters.sender) {
spec.path += '+from%3A' + parsedFilters.sender;
}
}
break;
case 'live.com':
case 'hotmail.com':
case 'outlook.com':
spec.name = 'Outlook';
spec.domain = 'mail.live.com';
spec.path = '/';
if (parsedFilters) {
spec.path += '?fid=flsearch&srch=1&sdr=4&satt=0';
if (parsedFilters.subject) {
spec.path += '&skws=' + parsedFilters.subject;
}
else if (parsedFilters.sender) {
spec.path += '&skws=' + parsedFilters.sender;
}
}
break;
default:
matched = false;
}
if (onlyMatched && !matched) {
return false;
}
return spec;
}
// Just return a value to define the module export.
// This example returns an object, but the module
// can return a function as the exported value.
return {
createLink: createLink,
getHref: getHref,
getSpec: getSpec
}
}));