aboutsummaryrefslogtreecommitdiffstats
path: root/data/complain/contact_finder.js
blob: 0a6b52a8a24d411e78c22e7c2357e16d3672d065 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
 * GNU LibreJSXUL - A browser add-on to block nonfree nontrivial JavaScript.
 * *
 * Copyright (C) 2011, 2012, 2014 Loic J. Duros
 * Copyright (C) 2014, 2015 Nik Nyby
 *
 * This file is part of GNU LibreJSXUL.
 *
 * GNU LibreJSXUL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GNU LibreJSXUL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU LibreJSXUL.  If not, see <http://www.gnu.org/licenses/>.
 */

var ContactFinder = function(options) {
    if (typeof options === 'undefined') {
        options = {};
    }
    this.complaintEmailSubject = options.complaintEmailSubject;
    this.complaintEmailBody = options.complaintEmailBody;

    // initial full list of links
    // on a page.
    this.pageLinks = null;

    // arrays of links.
    this.certainLinks = null;
    this.probableLinks = null;
    this.uncertainLinks = null;

    // if not page worker,
    // then allow to trigger a page worker.
    this.isPageWorker = false;

    // keep track of links already visited.
    this.visitedLinks = {};

    // keep track of the hostname of the original page.
    this.originalHostname = null;
};

ContactFinder.prototype.init = function(isPageWorker) {
    if (isPageWorker) {
        this.isPageWorker = true;
        console.debug('visiting', document.location.href);
    }

    this.convertStringToRegexp();
};

/**
 * searchForContactLink Main interface method to call from outside
 * the object.
 */
ContactFinder.prototype.searchForContactLink = function (originalUrl) {
    // find hostname of original page.
    this.originalHostname = this.getHostname(originalUrl);

    // initialize arrays of links to keep.
    this.certainLinks = [];
    this.probableLinks = [];
    this.uncertainLinks = [];

    // select all a tags.
    this.pageLinks = $('a').get();

    this.searchForUSPhoneNumber($('body').text());

    // run through list of links.
    this.processLinks();
};

/**
 * loopThroughLanguages
 * Select strings for all languages.
 */
ContactFinder.prototype.loopThroughLanguages = function (callback) {
    var le;

    for (var language in contactStr) {
        for (var degree in contactStr[language]) {
            le = contactStr[language][degree].length;
            callback(degree, contactStr[language][degree], le);
        }
    }
};

/**
 * convertStringToRegexp
 *
 */
ContactFinder.prototype.convertStringToRegexp = function () {
    var regexpList, i, le;
    this.loopThroughLanguages(function (degreeName, arr, le) {
        for (i = 0; i < le; i++) {
            try {
                arr[i] = new RegExp(arr[i], 'i');
            } catch (e) {}
        }
    });
};

/**
 * processLinks
 *
 * Run through the list of links in a page
 * and call the method that checks the regexs
 * for each of them.
 *
 */
ContactFinder.prototype.processLinks = function () {
    var start = Date.now();
    var currentLink;

    while (this.pageLinks.length) {
        currentLink = this.pageLinks.pop();

        if (currentLink !== undefined) {
            this.matchContact(currentLink);
        }

        var end = Date.now();

        if (this.pageLinks.length) {
            if ((end - start) > 8) {
                setTimeout(this.processLinks.bind(this), 100);
                return;
            }
        } else if (this.isPageWorker) {
            self.postMessage({event: 'destroy'});
            return;
        }
    }
};

/**
 * searchForUSPhoneNumber
 *
 */
ContactFinder.prototype.searchForUSPhoneNumber = function (str) {
    var phoneMatch, phone;
    var regClutter = /[\(\)\-\. ]+/gm;

    while ((phoneMatch = usaPhoneNumber.exec(str)) !== null) {
        phone = $.trim(phoneMatch)
            .replace(regClutter, '-').replace(/^\-/, '');
        phone = phone.replace(/[^0-9]$/, '');

        self.postMessage({
            event: linkTypes.PHONE_NUMBER_FOUND,
            contact: {
                'label': phone,
                'link': 'javascript:void("' + phone + '")'
            }
        });
    }
};

/**
 * searchForSocialMedia
 *
 * Match a Twitter or identi.ca url.
 *
 */
ContactFinder.prototype.searchForSocialMedia = function (link) {
    var elem = $(link),
        eventType;

    var text = this.notEmptyOrUri(link);

    if (reTwitter.test(elem.attr('href'))) {
        eventType = linkTypes.TWITTER_LINK_FOUND;
    }

    else if (reIdentiCa.test(elem.attr('href'))) {
        eventType = linkTypes.IDENTICA_LINK_FOUND;
    }

    if (eventType) {
        self.postMessage({
            event: eventType,
            contact: {
                'label': text,
                'link': elem.attr('href')
            }
        });
        return true;
    }

    return false;
};

/**
 * notEmptyOrUri
 *
 * If link has text, use it, if not,
 * then return uri.
 *
 */
ContactFinder.prototype.notEmptyOrUri = function (link) {

    var elem = $(link);

    if (/([^\s]*)]/.test(elem.text())) {
        // contains something else than just space.
        // It is valid.
        return elem.text();
    } else {
        return link.href;
    }
};

/**
 * searchForContactEmail
 *
 * Sends a particular message if a matching email
 * is found.
 *
 */
ContactFinder.prototype.searchForContactEmail = function (link) {
    var elem = $(link),
        eventType;

    if (reEmail.test(elem.attr('href'))) {
        console.debug('found an email address', elem.attr('href'));
        if (this.isSameHostname(elem.attr('href'))) {
            // this is a good email with same hostname, priceless!
            eventType = linkTypes.CERTAIN_EMAIL_ADDRESS_FOUND;
        } else {
            // not the same hostname... we'll keep it just in case.
            eventType = linkTypes.UNCERTAIN_EMAIL_ADDRESS_FOUND;
        }
    } else if (reAnyEmail.test(elem.attr('href'))) {
        if (this.isSameHostname(elem.attr('href'))) {
            eventType = linkTypes.UNCERTAIN_EMAIL_ADDRESS_FOUND;
        }
    }

    if (eventType) {
        var label = elem.attr('href').replace('mailto:', '');
        var myLink = elem.attr('href');

        // Add customizable complaintEmailSubject and complaintEmailBody to
        // mailto link
        if (this.complaintEmailSubject && this.complaintEmailBody) {
            myLink = myLink + '?subject=' + this.complaintEmailSubject +
                '&body=' + this.complaintEmailBody;
        }

        // we found an email address.
        // send message with whatever event type was found.
        self.postMessage({
            event: eventType,
            contact: {
                'label': label,
                'link': myLink
            }
        });
        return true;
    } else {
        // not an email address.
        return false;
    }
};

/**
 * matchContact
 *
 * Loop through the regexp and try to find a match.
 *
 */
ContactFinder.prototype.matchContact = function (currentLink) {
    // search for an email address.
    if (this.searchForContactEmail(currentLink)) {
        return true;
    }
    if (this.searchForSocialMedia(currentLink)) {
        return true;
    }

    // check all contact link strings.
    this.matchContactLink(currentLink);

    // this link is worth nothing.
    //return false;

};

/**
 * matchContactLink
 *
 * loop through regexp for a contact link.
 * And send a message to trigger a page worker
 * if not currently a page worker.
 *
 */
ContactFinder.prototype.matchContactLink = function (currentLink) {

    var that = this;

    this.loopThroughLanguages(
        function (degreeName, arr, le) {
            var text, j, href;

            for (j = 0; j < le; j++) {
                text = $(currentLink).text();
                href = currentLink.href;

                if (arr[j].test(text)) {

                    if (degreeName === 'certain') {

                        self.postMessage({
                            event: linkTypes.CERTAIN_LINK_FOUND,
                            contact: {
                                'label': text,
                                'link': href
                            }
                        });

                        if (!that.isPageWorker) {
                            that.complaintSearch(linkTypes.CERTAIN_LINK_FOUND, href);
                        }

                    } else if (degreeName === 'probable'){

                        self.postMessage({
                            event: linkTypes.PROBABLE_LINK_FOUND,
                            contact: {
                                'label': text,
                                'link': href
                            }
                        });

                        if (!that.isPageWorker) {
                            that.complaintSearch(linkTypes.PROBABLE_LINK_FOUND, href);
                        }
                    } else if (degreeName === 'uncertain') {
                        self.postMessage({
                            event: linkTypes.UNCERTAIN_LINK_FOUND,
                            contact: {
                                'label': text,
                                'link': href
                            }
                        });

                        if (!that.isPageWorker) {
                            that.complaintSearch(
                                linkTypes.UNCERTAIN_LINK_FOUND, href);
                        }
                    }
                }
            }
        });
};

/**
 * complaintSearch
 * returns to ui_info a link to open.
 */
ContactFinder.prototype.complaintSearch = function (linkType, link) {
    if (!this.isEmailLink(link)) {
        // we don't want to "visit" mailto links.
        self.postMessage({
            event: 'complaintSearch',
            urlSearch: {
                'type': linkType,
                'linkValue': link
            }
        });
    }
};

/**
 * getHostname
 * small regex taken from
 * http://beardscratchers.com/journal/using-javascript-to-get-the-hostname-of-a-url
 * to extract hostname from url.
 * do not consider www as subdomain.
 *
 */
ContactFinder.prototype.getHostname = function (str) {
    // remove www, but not other kind of subdomains (which most likely
    // may not be the same site than the domain itself.)
    str = this.removeWWW(str);
    var urlHostname = /^(?:f|ht)tp(?:s)?\:\/\/([^\/]+)/im;
    var emailHostname = /^mailto:[A-Z0-9\.\_\+\-]+\@([A-Z0-9\.\-]+\.[A-Z]{2,6})$/im;
    var match1 = urlHostname.exec(str);
    var match2 = emailHostname.exec(str);

    if (match1) {
        return match1[1];
    } else if (match2) {
        return match2[1];
    }

    // no match.
    return false;
};

/**
 * isSameHostname
 *
 * Checks a link has the same hostname than the original url.
 *
 */
ContactFinder.prototype.isSameHostname = function (url) {
    return this.getHostname(url) === this.originalHostname;
};

/**
 * remove www from hostname.
 */
ContactFinder.prototype.removeWWW = function (str) {
    if (typeof str !== 'string') {
        return '';
    }
    return str.replace("www.", "", 'i');
};

ContactFinder.prototype.isEmailLink = function (str) {
    if (typeof str !== 'string') {
        return '';
    }
    return /^mailto:/i.test(str);
};

var contactFinder = new ContactFinder();