-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (50 loc) · 2.04 KB
/
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
const apiKey = '';
// Store question for GPT-3 and GPT-3's response.
const questionElement = document.getElementById('question');
const responseElement = document.getElementById('response');
function getWebsiteName(url) {
// Remove "www." and ".com"
let websiteName = url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, '').replace(/\/.*$/, '');
// Remove subdomains
websiteName = websiteName.split('.').slice(-2, -1)[0];
// Convert to uppercase
websiteName = websiteName.charAt(0).toUpperCase() + websiteName.slice(1);
return websiteName;
}
// Prepare API input
chrome.tabs.query({ active: true, currentWindow: true }, async function (tabs) {
responseElement.innerText = "I'm sorry, but this webpage is not a valid WWW address. This extension can only handle internet-level webpages, not system-level ones.";
const url = new URL(tabs[0].url).hostname;
const domain = getWebsiteName(url);
questionElement.innerText = `What data does ${domain} collect about its users, and how does it use it?`;
responseElement.innerText = "Loading...";
const prompt = `Summarize how this ${domain} collects data about its users, and how the company uses it.`;
const temperature = 0.2;
const maxTokens = 200;
const body = {
prompt,
temperature,
max_tokens: maxTokens,
};
// Call API
try {
const response = await fetch(`https://api.openai.com/v1/models/gpt-4o&prompt=${encodeURIComponent(prompt)}&temperature=${temperature}&max_tokens=${maxTokens}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify(body)
});
// Ensure response is valid
const data = await response.json();
const answer = data.choices && data.choices.length > 0 ? data.choices[0].text.trim()+"..." : null;
if (answer) {
responseElement.innerText = answer;
} else {
responseElement.innerText = "Error: No answer received from API.";
}
} catch (error) {
responseElement.innerText = `Error: ${error.message}`;
}
});