-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPageMetas.js
More file actions
177 lines (154 loc) · 5.56 KB
/
getPageMetas.js
File metadata and controls
177 lines (154 loc) · 5.56 KB
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
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + "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"
};
console.log("getPageMetas - begin")
// array with relevant details for evaluation
const result = []; // Declare and initialize result variable here
var metas = document.getElementsByTagName('meta');
var metaArr = [];
for (var i=0; i<metas.length; i++) {
var name = metas[i].getAttribute("name");
var property = metas[i].getAttribute("property");
var httpequiv = metas[i].getAttribute("http-equiv");
var content = metas[i].getAttribute("content");
var charset = metas[i].getAttribute("charset");
metaArr.push([name, property, httpequiv, content, charset]);
}
console.log("got metas")
console.log("metaArr information: ", metaArr)
// get product title, description, manufacturer from metaArr
var title = "";
var ingredients = "";
var packaging = "";
var company = document.domain;
for (var i=0; i<metaArr.length; i++) {
if (metaArr[i][0] === "title") {
title = metaArr[i][3];
} else if (metaArr[i][0] === "description") {
var desc = metaArr[i][3].toLowerCase();
ingredients = desc;
// if (desc.includes("ingredients")) {
// ingredients = desc.split("ingredients: ")[1];
// } else if (desc.includes("packaging")) {
// packaging = desc.split("packaging: ")[1];
// }
}
}
console.log("title", title)
console.log("ingredients", ingredients)
console.log("packaging", packaging)
console.log("checkpoint 1")
const score_prompt = `
Provide a score between 0 and 100 regarding the cyber ethics and digital rights of the company: ${company}. Return a number only.`
console.log("score_prompt", score_prompt)
const product_explanation_prompt = `
Give a one paragraph explanation regarding the strengths of the cyber ethics and digital rights of the company: ${company} and
one paragraph explanation regarding the weaknesses of the cyber ethics and digital rights of the company: ${company}.
Finally a one paragraph explanation regarding the overall state of the cyber ethics and digital rights of the company: ${company}. Be concise and use bullet points.`
console.log("product_explanation_prompt", product_explanation_prompt)
const alternative_prompt = `
Find 3 alternative websites that are similar to the company: ${company} that have better cyber ethics and digital rights. They must score equal or better than ${company} between 0 and 100. Only reply with the name and score.`
//const users_prompt = `How many total users does ${company} have? Return a number only.`
const type_data_prompt = `What type of data does ${company} collect from its users? Reply concisely and separate the types with commas.`
const breaches_prompt = `Give one short paragraph about the breach potential in ${company}'s industry.`
const risk_prompt = `What is the risk of data exposure using ${company}'s services?`
const breach_prompt = `Tell me about the last data breach of ${company}?`
console.log("alternative_prompt", alternative_prompt)
let dataBreach = "";
/*switch(document.domain) {
case "www.google.com":
// code block
dataBreach = "Jan 2023"
break;
case "www.snapchat.com":
// code block
dataBreach = "Mar 2022"
break;
case "facebook.com":
dataBreach = "Apr 2021"
// code block
break;
case "www.instagram.com":
// code block
dataBreach = "Jan 2021"
break;
default:
// code block
}*/
const url = "https://api.openai.com/v1/engines/text-davinci-003/completions";
const body = {
prompt: [score_prompt, product_explanation_prompt, alternative_prompt],
max_tokens: 2000,
n: 1,
stop: ""
};
const body2= {
prompt: [type_data_prompt, breaches_prompt, risk_prompt, breach_prompt],
max_tokens: 2000,
n: 1,
stop: ""
};
const testing = false;
if (!testing){
fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: headers
}).then(response => {
console.log("11111111")
if (response.ok) {
return response.json();
} else {
throw new Error("ChatGPT API request failed.");
}
}).then(data => {
console.log(data.choices);
chrome.runtime.sendMessage({
method:"getMetas",
metas:metaArr,
dataBreach: dataBreach,
domain: document.domain,
score:data.choices[0].text.trim(),
product_explanation:data.choices[1].text.trim(),
alternatives:data.choices[2].text.trim()
});
}).catch(error => {
console.error(error);
}).finally(() => {
console.log("getResponse1 - end");
})
// Second request
fetch(url, {
method: "POST",
body: JSON.stringify(body2),
headers: headers,
})
.then((response) => {
console.log("22222222");
if (response.ok) {
return response.json();
} else {
throw new Error("ChatGPT API request failed.");
}
})
.then((data) => {
console.log(data.choices);
chrome.runtime.sendMessage({
method: "getMetas2",
metas: metaArr,
dataBreach: dataBreach,
domain: document.domain,
type_data: data.choices[0].text.trim(),
breaches: data.choices[1].text.trim(),
risk: data.choices[2].text.trim(),
lastBreach: data.choices[3].text.trim(),
});
})
.catch((error) => {
console.error(error);
})
.finally(() => {
console.log("getResponse2 - end");
})
};