-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathanalyseFrameWithBestPractices.js
137 lines (126 loc) · 4.88 KB
/
analyseFrameWithBestPractices.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
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (C) 2016 The EcoMeter authors (https://gitlab.com/ecoconceptionweb/ecometer)
* Copyright (C) 2019-2022 [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function start_analyse() {
const analyseStartingTime = Date.now();
const dom_size = getDomSizeWithoutSvg();
// test with http://www.wickham43.net/flashvideo.php
const pluginsNumber = getPluginsNumber();
const printStyleSheetsNumber = getPrintStyleSheetsNumber();
const inlineStyleSheetsNumber = getInlineStyleSheetsNumber();
const inlineJsScript = getInlineJsScript();
const inlineJsScriptsNumber = getInlineJsScriptsNumber();
const imagesResizedInBrowser = getImagesResizedInBrowser();
const pageAnalysis = {
analyseStartingTime: analyseStartingTime,
url: document.URL,
domSize: dom_size,
pluginsNumber: pluginsNumber,
printStyleSheetsNumber: printStyleSheetsNumber,
inlineStyleSheetsNumber: inlineStyleSheetsNumber,
inlineJsScript: inlineJsScript,
inlineJsScriptsNumber: inlineJsScriptsNumber,
imagesResizedInBrowser: imagesResizedInBrowser,
};
chrome.runtime.sendMessage(pageAnalysis);
}
function getDomSizeWithoutSvg() {
let dom_size = document.getElementsByTagName("*").length;
const svgElements = document.getElementsByTagName("svg");
for (let i = 0; i < svgElements.length; i++) {
dom_size -= getNbChildsExcludingNestedSvg(svgElements[i]) - 1;
}
return dom_size;
}
function getNbChildsExcludingNestedSvg(element) {
if (element.nodeType === Node.TEXT_NODE) return 0;
let nb_elements = 1;
for (let i = 0; i < element.childNodes.length; i++) {
// deal with svg nested case
if (element.childNodes[i].tagName !== "svg")
nb_elements += getNbChildsExcludingNestedSvg(element.childNodes[i]);
else nb_elements += 1;
}
return nb_elements;
}
function getPluginsNumber() {
const plugins = document.querySelectorAll("object,embed");
return plugins === undefined ? 0 : plugins.length;
}
function getPrintStyleSheetsNumber() {
return (
document.querySelectorAll("link[rel=stylesheet][media~=print]").length +
document.querySelectorAll("style[media~=print]").length
);
}
function getInlineStyleSheetsNumber() {
let styleSheetsArray = Array.from(document.styleSheets);
let inlineStyleSheetsNumber = 0;
styleSheetsArray.forEach((styleSheet) => {
try {
// Ignore SVG styles in count
const isSvgStyleSheet = styleSheet.ownerNode instanceof SVGStyleElement;
if (!styleSheet.href && !isSvgStyleSheet) inlineStyleSheetsNumber++;
} catch (err) {
console.log("GREENIT-ANALYSIS ERROR ," + err.name + " = " + err.message);
console.log("GREENIT-ANALYSIS ERROR " + err.stack);
}
});
return inlineStyleSheetsNumber;
}
function getInlineJsScript() {
let scriptArray = Array.from(document.scripts);
let scriptText = "";
scriptArray.forEach((script) => {
let isJSON = String(script.type) === "application/ld+json"; // Exclude type="application/ld+json" from parsing js analyse
if (script.text.length > 0 && !isJSON) scriptText += "\n" + script.text;
});
return scriptText;
}
function getInlineJsScriptsNumber() {
let scriptArray = Array.from(document.scripts);
let inlineScriptNumber = 0;
scriptArray.forEach((script) => {
let isJSON = String(script.type) === "application/ld+json"; // Exclude type="application/ld+json" from count
if (script.text.length > 0 && !isJSON) inlineScriptNumber++;
});
return inlineScriptNumber;
}
function getImagesResizedInBrowser() {
const imgArray = Array.from(document.querySelectorAll("img"));
let imagesResized = [];
imgArray.forEach((img) => {
if (
img.clientWidth < img.naturalWidth ||
img.clientHeight < img.naturalHeight
) {
// Images of one pixel are some times used ... , we exclude them
if (img.naturalWidth > 1) {
const imageMeasures = {
src: img.src,
clientWidth: img.clientWidth,
clientHeight: img.clientHeight,
naturalWidth: img.naturalWidth,
naturalHeight: img.naturalHeight,
};
imagesResized.push(imageMeasures);
}
}
});
return imagesResized;
}
start_analyse();