aboutsummaryrefslogtreecommitdiffstats
path: root/lib/html_script_finder/web_labels/find_js_labels.js
blob: 3986e0fcc7c115f918afe8330df7ffc2458e0bb6 (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
/**
 * GNU LibreJSXUL - A browser add-on to block nonfree nontrivial JavaScript.
 * *
 * Copyright (C) 2011, 2012, 2013, 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/>.
 */

/**
 * This file works in conjunction with lib/html_script_finder/js_web_labels.js
 * to find mentions of external JavaScript files and their license information.
 * This allows the dom_handler to allow them by default.
 */

/**
 * @param {Array} licenses - An array of html nodes.
 *
 * @return {Array} - An array of simple license objects.
 */
function getLicensesArrayFromElements(licenses) {
    var a = [];
    // Convert the html node into a simpler object
    for (var i = 0; i < licenses.length; i++) {
        a.push({
            licenseName: licenses[i].textContent,
            licenseUrl: licenses[i].href
        });
    }
    return a;
}

/**
 * @param {Array} sources - An array of html nodes.
 *
 * @return {Array} - An array of simple source objects.
 */
function getSourcesArrayFromElements(sources) {
    var a = [];
    for (var i = 0; i < sources.length; i++) {
        a.push({
            sourceName: sources[i].textContent,
            sourceUrl: sources[i].href
        });
    }
    return a;
}

// find table.
exports.getLicenseList = function(document) {
    var tbl = document.getElementById('jslicense-labels1');
    var jsList = [];
    var i = 0;
    var le;
    var rows;
    var link;
    var fileCell;
    var licenseCell;
    var sourceCell;
    var row;

    if (tbl) {
        try {
            rows = tbl.getElementsByTagName('tr');
            le = rows.length;
            var mockElem = {textContent: 'Unknown', href: 'Unknown'};
            // loop through rows, and add each valid element to
            // the array.
            for (; i < le; i++) {
                row = rows[i].getElementsByTagName('td');

                // Find script url
                if (row[0] && row[0].getElementsByTagName('a')[0]) {
                    fileCell = row[0].getElementsByTagName('a')[0];
                } else {
                    fileCell = mockElem;
                }

                // 'licenses' and 'sources' will, for normal cases, just
                // contain one element. If the fileCell is pointing to a
                // combined JS file with multiple licenses, though, these
                // arrays will contain multiple elements.

                // Find license info
                var licenses = [mockElem];
                if (row[1] && row[1].getElementsByTagName('a').length > 0) {
                    licenses = getLicensesArrayFromElements(
                        row[1].getElementsByTagName('a'));
                }

                // Find original source info
                var sources = [mockElem];
                if (row[2] && row[2].getElementsByTagName('a').length > 0) {
                    sources = getSourcesArrayFromElements(
                        row[2].getElementsByTagName('a'));
                }

                if (fileCell.href !== 'Unknown') {
                    jsList.push({
                        'fileName': fileCell.textContent,
                        'fileUrl': fileCell.href,

                        // we'll fill this with value when needed to compare
                        // script.
                        'fileHash': null,

                        'licenses': licenses,
                        'sources': sources
                    });
                }
            }
        } catch (e) {
            console.debug(
                'Error fetching JS Web Label licenses',
                e, e.lineNumber, e.fileName, 'index is', i);
        }
    }

    return jsList;
};