-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-script.js
131 lines (118 loc) · 4.35 KB
/
content-script.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
// Retrieve <link> or <script> tags
function retrieveTags(tags, type) {
let attribute = "";
// If we try to retrieve <link> tags
if (type === "link") {
attribute = "href";
}
// If we try to retrieve <script> tags
else if (type === "script") {
attribute = "src";
}
else {
console.error(`${type} type doesn't exist for retrieveTags function. Please choose between "link" or "script"`);
return;
}
console.group(`[checker] Logs to retrieve ${type} tags`);
console.log(`There are ${tags.length} ${type} tags in this document...`, tags);
let tagsWithURL = {};
for (let tag of tags) {
console.log("URL " + tag[attribute]);
if (tag.hasAttribute(attribute)) {
// We collect the diffrent attributes, except "src" or "href"
const attributeNames = tag.getAttributeNames().filter(attributeName => attributeName != attribute);
let tmp = {};
for (let attributeName of attributeNames) {
tmp[attributeName] = tag.getAttribute(attributeName)
}
tagsWithURL[tag[attribute]] = tmp;
}
}
console.log(`... including ${Object.keys(tagsWithURL).length} with the "href" attribute:`, tagsWithURL)
console.groupEnd();
return tagsWithURL;
}
/*url = new URL(i.src);
if (url.hostname != document.location.hostname){
if (i.hasAttribute("integrity")) {
scriptTagsCorrect.push(i);
}
else {
scriptTagsIncorrect.push(i);
}
}*/
// scriptTagsWithSrc.push(i);
// We collect the diffrent attributes, except "src"
// On startup, check if we have stored data
// If we don't, then store the default settings
async function getData(hostname) {
console.group(`[checker] Checking stored data for ${hostname}`);
let data = await browser.storage.local.get(hostname);
// If there is no data for the hostname, we create the default setting
if (Object.keys(data).length === 0) {
console.log("No data currently avalable");
data[hostname] = {
"headers-all":{
},
"headers-security":{
"strict-transport-security": new Set(),
"x-xss-protection": new Set(),
"content-security-policy": new Set(),
"x-frame-options": new Set(),
"x-content-type-options": new Set(),
"cache-control": new Set(),
"server": new Set(),
"x-powered-by": new Set()
},
"linkTags":{
"allURLs": {}
},
"scriptTags":{
"allURLs": {}
}
};
}
console.log("data", data);
console.groupEnd();
return data;
}
// Store items in the storage area, or update existing items
async function saveData(data, tagsWithURL, type) {
console.group("[checker] Checking for saveData")
let attribute = "";
// If we try to save <link> tags
if (type === "link") {
attribute = "linkTags";
}
// If we try to save <script> tags
else if (type === "script") {
attribute = "scriptTags";
}
else {
console.error(`${type} type doesn't exist for saveData function. Please choose between "link" or "script"`);
return;
}
data[document.location.hostname][attribute][document.location.pathname] = tagsWithURL;
for (let url in tagsWithURL) {
data[document.location.hostname][attribute]["allURLs"][url] = tagsWithURL[url];
}
console.log("data", data)
browser.storage.local.set(data).catch(err => console.log(err));
console.groupEnd();
}
function onGot(item) {
console.log(item);
}
function onError(error) {
console.log(`Error: ${error}`);
}
console.log(`Test du content-script : ${document.location.hostname}`);
let linkTags = document.getElementsByTagName('link');
let scriptTags = document.getElementsByTagName("script");
let linkTagsWithHref = retrieveTags(linkTags, "link");
let scriptTagsWithSrc = retrieveTags(scriptTags, "script");
getData(document.location.hostname).then(result => {
saveData(result, linkTagsWithHref, "link");
saveData(result, scriptTagsWithSrc, "script");
});
browser.storage.local.get().then(result => console.log("All", result));