-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
204 lines (177 loc) Β· 7.7 KB
/
popup.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Create the context menu item
chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create({
id: "inspectTos",
title: "PolicyPal⨠- Inspect policy",
contexts: ["link"],
documentUrlPatterns: ["*://*/*"]
});
});
// Add a listener for the context menu item click event
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "inspectTos") {
var link = info.linkUrl;
if (link !== '') {
openProgressPopup();
fetchContent(link)
.then(function (content) {
updateProgress('Prompting Content');
return analyzeContent(content);
})
.then(function (summary) {
updateProgress('Analysis complete β
');
showSummary(summary);
closeProgressPopup();
})
.catch(function (error) {
console.error('Error:', error);
updateProgress('Error: \n' + error + "\n Please try again later or contact GD");
});
} else {
window.alert("Please paste link before clikcing the button! What will I analyze with an empty input box? Your head ?")
}
}
});
document.addEventListener('DOMContentLoaded', function () {
var analyzeButton = document.getElementById('analyzeButton');
analyzeButton.addEventListener('click', function () {
var linkInput = document.getElementById('linkInput');
var link = linkInput.value.trim();
if (link !== '') {
openProgressPopup();
fetchContent(link)
.then(function (content) {
updateProgress('Prompting Content');
return analyzeContent(content);
})
.then(function (summary) {
updateProgress('Analysis complete β
\n' + summary);
showSummary(summary);
closeProgressPopup();
})
.catch(function (error) {
console.error('Error:', error);
updateProgress('Error:', error + "\n Please try again later or contact GD");
});
}
});
var optionsLink = document.getElementById('optionsLink');
optionsLink.addEventListener('click', function () {
chrome.runtime.openOptionsPage();
});
});
//progress interact functions
function openProgressPopup() {
var progressUrl = chrome.extension.getURL('progress.html');
chrome.windows.create({
url: progressUrl,
type: 'popup',
width: 365,
height: 460
}, function (window) {
// Send a message to the progress popup to update the progress
chrome.tabs.sendMessage(window.tabs[0].id, { action: 'updateProgress', progress: 'Processing...' });
});
}
function updateProgress(progress) {
// Send a message to the progress popup to update the progress
chrome.runtime.sendMessage({ action: 'updateProgress', progress: progress });
}
function closeProgressPopup() {
// Send a message to the progress popup to update the progress
chrome.runtime.sendMessage({ action: 'closeProgressPopup' });
}
function fetchContent(link) {
return new Promise(function (resolve, reject) {
chrome.tabs.create({ url: link, active: false }, function (tab) {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
if (tabId === tab.id && changeInfo.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
setTimeout(function () {
chrome.tabs.sendMessage(tab.id, { action: 'getContent' }, function (response) {
if (chrome.runtime.lastError) {
reject(new Error('Error executing script: ' + chrome.runtime.lastError.message));
} else {
const result = processTos(response.content);
console.log("toc extracted: " + result);
resolve(result);
}
chrome.tabs.remove(tab.id); // Close the tab after scraping
});
}, 50); // Adjust the delay as needed based on the page's loading behavior
}
});
});
});
}
function processTos(content) {
const cleanedCont = content.replace(/\n/g, ' ').replace(/"/g, '').replace(/\t/g, '').replace("Learn more", ""); //removing all unwanted elements that hinder the prompting
const trucCont = shortenString(cleanedCont, 1450)
console.log("web content truncated to 1500 words: " + trucCont);
return trucCont;
}
function shortenString(str, wordLimit) {
if (str.length <= wordLimit) {
return str; // No need to shorten if the string is already within the limit
}
const words = str.split(' ');
const topPortion = words.slice(0, Math.floor(wordLimit / 3)).join(' ');
const middlePortion = words.slice(
Math.floor((words.length - wordLimit) / 2),
Math.floor((words.length - wordLimit) / 2) + Math.floor(wordLimit / 3)
).join(' ');
const bottomPortion = words.slice(-Math.floor(wordLimit / 3)).join(' ');
return topPortion + ' ' + middlePortion + ' ' + bottomPortion;
}
function analyzeContent(content) {
return new Promise(function (resolve, reject) {
// Retrieve the stored API key from the browser's storage
chrome.storage.sync.get('apiKey', function (data) {
var apiKey = data.apiKey;
if (!apiKey) {
alert('Please enter your API key in the options page.');
reject(new Error('API key not found'));
return;
}
// Create the request body
//const promptEng = "Given below is the terms of servoce or privacy policy of a website. Analyze and summarize it : ";
const promptEng = "Given followed is the toc or privacy polcy content copied from a website. Please analyze the content provided below and identify any policy or terms that may be concerning or problematic. Provide detailed insights and explanations regarding your findings." + content;
var requestBody = JSON.stringify({
prompt: promptEng,
psidCookie: apiKey
});
// Create the request options
var requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: requestBody
};
console.log("posting summary request to bard...");
// Make the POST request to the endpoint
fetch("https://bardapi-gd.onrender.com/ask", requestOptions)
.then(function (response) {
if (!response.ok) {
throw new Error("Request failed with status: " + response.status);
}
return response.json();
})
.then(function (data) {
// Extract the summarized text from the response
var summarizedText = data; //data.summary;
// Resolve with the summarized text
resolve(summarizedText);
})
.catch(function (error) {
// Handle any errors
reject(error);
});
})
});
}
function showSummary(summary) {
// Show the summarized output to the user
window.alert(summary);
console.log(summary);
}