Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
293 lines (221 loc) · 9.22 KB
/
Copy pathbackground.js
File metadata and controls
293 lines (221 loc) · 9.22 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
importScripts('libs/tf.es2017.js');
importScripts('libs/universal-sentence-encoder@1.3.3.js');
let data = [];
let title='';
let model = null;
let info='';
let precomputedEmbeddings = []; // Store computed embeddings progressively
let embeddingInProgress = false;
const batchSize = 10; // Define batch size
let last_url="";
function checkAndInjectScript(tabId) {
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError) {
console.error("Error fetching tab:", chrome.runtime.lastError);
return;
}
if (tab && tab.url) {
const url = tab.url;
const isAmazonProductPage = url.includes("amazon.") && url.includes("/dp/");
const isFlipkartProductPage = url.includes("flipkart.com") && url.includes("/p/");
if (isAmazonProductPage || isFlipkartProductPage) {
if(url!=last_url){
if(last_url==""){
last_url=url;
}
else{
console.log("reset reset");
data=[];
precomputedEmbeddings=[];
}
last_url=url;
}
}
}
});
}
chrome.webNavigation.onCompleted.addListener((details) => {
checkAndInjectScript(details.tabId);
});
// Listener for tab activation (tab change)
chrome.tabs.onActivated.addListener((activeInfo) => {
checkAndInjectScript(activeInfo.tabId);
});
// Load the Universal Sentence Encoder model
async function loadModel() {
try {
model = await use.load();
console.log('Model loaded');
} catch (error) {
console.error('Error loading model:', error);
}
}
// Function to embed a sentence or text using Universal Sentence Encoder
async function embedText(text) {
try {
const embeddings = await model.embed([text]);
return embeddings.arraySync();
} catch (error) {
console.error('Error embedding text:', error);
return [];
}
}
// Function to compute cosine similarity between two vectors
function cosineSimilarity(A, B) {
const dotProduct = A.map((val, idx) => val * B[idx]).reduce((a, b) => a + b, 0);
const magnitudeA = Math.sqrt(A.map(val => val * val).reduce((a, b) => a + b, 0));
const magnitudeB = Math.sqrt(B.map(val => val * val).reduce((a, b) => a + b, 0));
return dotProduct / (magnitudeA * magnitudeB);
}
async function getRelevantSentences(prompt) {
if (!model) {
await loadModel();
}
console.log('model started', prompt);
const promptEmbedding = await embedText(prompt); // Embed prompt only once
console.log('embedded text');
if (!promptEmbedding.length) return [];
// Initialize embedding process if not started
if (!embeddingInProgress && precomputedEmbeddings.length < data.length ) {
embeddingInProgress = true;
precomputeInBatches(data, batchSize);
// Start embedding in batches
}
// Wait for initial embeddings to be ready
await waitForInitialEmbeddings(batchSize);
// Calculate similarities using available embeddings
let similarities = precomputedEmbeddings.map(({ sentence, embedding }) => {
const similarity = cosineSimilarity(promptEmbedding[0], embedding[0]);
return { sentence, similarity };
});
// Sort the sentences by similarity score and get top results
similarities.sort((a, b) => b.similarity - a.similarity);
console.log(similarities.slice(0, 4).map(item => item.sentence));
// Return top 4 relevant sentences
return similarities.slice(0, 4).map(item => item.sentence);
}
// Batch embedding function that progressively adds embeddings to the array
async function precomputeInBatches(data, batchSize) {
for (let i = precomputedEmbeddings.length; i < data.length; i += batchSize) {
const batch = data.slice(i, i + batchSize).filter(sentence => sentence!==undefined && sentence!=='');
const embeddedBatch = await Promise.all(
batch.map(async (sentence) => ({
sentence,
embedding: await embedText(sentence)
}))
);
precomputedEmbeddings.push(...embeddedBatch);
console.log(`Precomputed ${precomputedEmbeddings.length} embeddings`);
// If enough embeddings are ready, stop and continue in background
if (precomputedEmbeddings.length >= batchSize) break;
}
embeddingInProgress = false; // Mark as complete if all data is embedded
}
// Wait function to ensure at least one batch is available for processing
async function waitForInitialEmbeddings(batchSize) {
while (precomputedEmbeddings.length < batchSize && embeddingInProgress) {
await new Promise(resolve => setTimeout(resolve, 50)); // Short wait
}
}
// Unified message listener
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
if (request.action === "fetchReviews") {
precomputedEmbeddings=[];
console.log('Fetching reviews from:', request.url);
let headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36",
"Accept-Language": "en-US,en;q=0.9"
};
// Customize headers based on domain
if (request.url.includes("amazon.in")) {
headers["Referer"] = "https://www.amazon.in/";
} else if (request.url.includes("amazon.com")) {
headers["Referer"] = "https://www.amazon.com/";
} else if (request.url.includes("flipkart.com")) {
headers["Referer"] = "https://www.flipkart.com/";
}
// Fetch multiple pages of reviews
async function fetchMultiplePages(url, maxPages = 5) {
let allReviews = ''; // Accumulate reviews in text format
let pageParam = ''; // Parameter for pagination
// Determine pagination parameter based on domain
if (url.includes("amazon")) {
pageParam = "pageNumber";
} else if (url.includes("flipkart")) {
pageParam = "page";
}
for (let page = 1; page <= maxPages; page++) {
const pageUrl =` ${url}&${pageParam}=${page}`;
console.log(`Fetching page ${page} from: ${pageUrl}`);
try {
const response = await fetch(pageUrl, {
method: "GET",
headers: headers
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const reviewData = await response.text();
console.log(`Fetched review data for page ${page}:, reviewData`);
allReviews += reviewData; // Append review data to allReviews
// Check if the next page exists
if (!nextPageExists(reviewData)) {
console.log(`No more pages after page ${page}.`);
break;
}
} catch (error) {
console.error(`Error fetching page ${page} reviews:, error`);
break; // Stop fetching if there’s an error
}
}
return allReviews;
}
// Function to check if a next page exists in the HTML content
function nextPageExists(htmlContent) {
const lowerCaseContent = htmlContent.toLowerCase();
if (request.url.includes("amazon") && lowerCaseContent.includes("next")) {
return true;
} else if (request.url.includes("flipkart") && lowerCaseContent.includes("next")) {
return true;
}
return false;
}
// Fetch the pages and send the response
const reviews = await fetchMultiplePages(request.url);
sendResponse({ reviews: reviews });
return true; // Indicate asynchronous response
}
if (request.action === 'processMessage') {
const prompt = request.message;
try {
const relevantSentences = await getRelevantSentences(prompt);
sendResponse({ relevantSentences,title,info });
} catch (error) {
console.error('Error processing the message:', error);
sendResponse({ relevantSentences: [] });
}
}
return true; // Indicate asynchronous response
});
async function embed_check() {
if (!model) {
await loadModel();
}
if (!embeddingInProgress && precomputedEmbeddings.length < data.length) {
embeddingInProgress = true;
precomputeInBatches(data, batchSize); // Start embedding in batches
}
}
// Initialize the model on script load
loadModel();
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === "data") {
data=request.data;
title=request.title;
info=request.productinfo;
console.log('info',info);
embed_check();
// send_trigger();
}
return true;
});