-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.js
443 lines (374 loc) · 15.5 KB
/
background.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import {clear_object_stores, fetch_from_object_store, open_indexDB, push_to_object_store} from "./database.js";
import {CoT} from "./generation.js";
import OpenAI from "openai";
function collect_content() {
const TEXT_BOUNDARY_MIN = 20;
function get_position(element) {
let top = 0, left = 0;
while (element) {
top += element.offsetTop || 0;
left += element.offsetLeft || 0;
element = element.offsetParent;
}
return {top, left};
}
let nodeWeightCache = new Map();
function calculate_weight(node) {
if (nodeWeightCache.has(node)) {
return nodeWeightCache.get(node);
}
let htmlWeight = 0;
let contentWeight = 0;
if (node.nodeType === 3) { //Checking if nodeType is TEXT_NODE
contentWeight = node.textContent.length;
htmlWeight = 0;
} else if (node.nodeType === 8) { //Checking if nodeType is COMMENT_NODE
contentWeight = 0;
htmlWeight = node.nodeValue.length;
} else {
Array.from(node.childNodes).forEach(child => {
const {htmlWeight: childHtmlWeight, contentWeight: childContentWeight} = calculate_weight(child);
htmlWeight += childHtmlWeight;
contentWeight += childContentWeight;
});
try {
if (node.outerHTML && node.innerHTML) {
htmlWeight += node.outerHTML.length - node.innerHTML.length;
} else if (node.outerHTML) {
htmlWeight += node.outerHTML.length;
}
} catch (error) {
console.warn(node, error);
}
}
const result = {htmlWeight, contentWeight};
nodeWeightCache.set(node, result);
return result;
}
function sigmoid(x, b = 0.5, a = 1) {
return 1 / (1 + Math.exp(-a * (x - b)));
}
function decompose(parentWeight, childrenWeights) {
const {htmlWeight: parentHtmlWeight, contentWeight: parentContentWeight} = parentWeight;
const totalChildHtmlWeight = childrenWeights.reduce((sum, weight) => sum + weight.htmlWeight, 0);
const totalChildContentWeight = childrenWeights.reduce((sum, weight) => sum + weight.contentWeight, 0);
const htmlWeightReduction = parentHtmlWeight - totalChildHtmlWeight;
const contentWeightLoss = parentContentWeight - totalChildContentWeight;
const htmlWeightFactor = sigmoid(parentHtmlWeight / 500, 0.5, 10); // Adjust '10' for steepness
console.log(htmlWeightFactor);
const contentWeightFactor = sigmoid(totalChildContentWeight / parentContentWeight, 0.5, 10);
console.log(contentWeightFactor)
const weightedHtmlWeightReduction = htmlWeightReduction * htmlWeightFactor;
const weightedContentWeightLoss = contentWeightLoss * (1 - contentWeightFactor);
console.log([weightedHtmlWeightReduction, weightedContentWeightLoss]);
return totalChildContentWeight >= TEXT_BOUNDARY_MIN && weightedHtmlWeightReduction > weightedContentWeightLoss;
}
function traverse_dom(node) {
let bestNodes = [];
function traverse(node) {
const {htmlWeight, contentWeight} = calculate_weight(node);
console.log([node, htmlWeight, contentWeight])
if (!node.children || node.children.length === 0) {
if (contentWeight >= TEXT_BOUNDARY_MIN && node.tagName !== 'SCRIPT') {
bestNodes.push(node);
}
return;
}
const childrenWeights = Array.from(node.children).map(child => calculate_weight(child));
if (decompose({htmlWeight, contentWeight}, childrenWeights)) {
Array.from(node.children).forEach(child => traverse(child));
} else {
if (contentWeight >= TEXT_BOUNDARY_MIN && node.tagName !== 'SCRIPT') {
bestNodes.push(node);
}
}
console.log(node, htmlWeight, contentWeight)
}
traverse(node);
console.log("Best nodes:", bestNodes)
return bestNodes;
}
function get_xpath(node) {
const parts = [];
for (; node && node.nodeType === 1; node = node.parentNode) { //Checking if nodeType is ELEMENT_NODE
let index = 0;
for (let sibling = node.previousSibling; sibling; sibling = sibling.previousSibling) {
if (sibling.nodeType === 10) continue; //Checking if nodeType is DOCUMENT_NODE
if (sibling.nodeName === node.nodeName) ++index;
}
const nodeName = node.nodeName.toLowerCase();
const part = (index ? nodeName + '[' + (index + 1) + ']' : nodeName);
parts.unshift(part);
}
return parts.length > 0 ? '/' + parts.join('/') : '';
}
function validate_node(orig_node, node) {
console.log([orig_node, node])
return orig_node.innerText && orig_node.innerText.length > 40 && orig_node.offsetWidth && orig_node.offsetHeight && node.innerHTML && node.plainText && orig_node.tagName !== 'SCRIPT'
}
const root = document.body
let nodes = [];
console.log(root)
function push(node) {
let {top, left} = get_position(node);
let minimal = {
xpath: get_xpath(node),
layout: {
left: left,
top: top,
},
innerHTML: node.innerHTML,
plainText: node.textContent,
}
if (validate_node(node, minimal)) {
nodes.push(minimal);
}
}
traverse_dom(root).forEach(node => push(node));
console.log(nodes)
return nodes;
}
/*
--###--
Generation (DOESN'T WORK)
--###--
*/
async function* generate(nodes, template, openai, default_openai) {
let key = openai
console.log("Community key is: ", default_openai)
if(key) {
const client = new OpenAI({apiKey: key, dangerouslyAllowBrowser: true});
try {
await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{
"role": "system",
"content": "ping"
}
]
})
} catch (e) {
console.log("Fetching data!")
await fetch('https://gist.githubusercontent.com/fedor-palisade-research/15cc05c51d4659d7bbec3f5e9594aaf6/raw/311a92e4a25ce1fd5a64951ad35ab09b98399f88/community_key.txt')
.then(response => response.text())
.then(data => {
console.log(data)
function decodeBase64(str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
key = decodeBase64(data);
})
.catch(error => {
console.error('Error fetching the string:', error)
key = default_openai
});
}
} else {
console.log("Fetching data!")
await fetch('https://gist.githubusercontent.com/fedor-palisade-research/15cc05c51d4659d7bbec3f5e9594aaf6/raw/311a92e4a25ce1fd5a64951ad35ab09b98399f88/community_key.txt')
.then(response => response.text())
.then(data => {
console.log(data)
function decodeBase64(str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
key = decodeBase64(data);
})
.catch(error => {
console.error('Error fetching the string:', error)
key = default_openai
});
}
const promises = nodes.map(async node => {
console.log("CoT launched for node", node, "at", Date.now(), "with template", template.generation);
const original = node.innerHTML;
const generation = await CoT(key, template, original);
console.log("CoT finished for node", node, "at", Date.now(), "with result:", generation);
return {xpath: node.xpath, html: generation};
});
for (const promise of promises) {
const completion = await promise;
if (completion.html) {
yield completion;
}
}
}
/*
--###--
SETUP
--###--
*/
chrome.webNavigation.onCompleted.addListener(async function (details) {
if (details.frameId === 0) {
await clear_object_stores(new URL(details.url).hostname + new URL(details.url).pathname);
chrome.runtime.sendMessage({
action: "close_popup",
});
}
});
function push_cached_template(request) {
fetch_from_object_store(request.url, 'original').then(original_nodes => {
console.log("Original Nodes:", original_nodes)
original_nodes.forEach(async original_node => {
const xpath = original_node.xpath;
const html = original_node.innerHTML;
const func = function (xpath, html) {
const node = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (node) {
node.innerHTML = html;
} else {
console.log(`No element matches the provided XPath: ${xpath}`);
}
}
await chrome.scripting.executeScript({
target: {tabId: request.id},
function: func,
args: [xpath, html]
});
});
});
console.log("Original pushed...")
console.log("Fetching template", request.template.name)
// Then fetch and push the chosen template
fetch_from_object_store(request.url, request.template.name).then(nodes => {
console.log("Template Nodes:", nodes)
nodes.forEach(async node => {
const xpath = node.xpath;
const html = node.html;
const func = function (xpath, html) {
const node = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (node) {
node.innerHTML = html;
} else {
console.log(`No element matches the provided XPath: ${xpath}`);
}
}
await chrome.scripting.executeScript({
target: {tabId: request.id},
function: func,
args: [xpath, html]
});
});
});
}
async function process_request(request) {
if (request.action === "setup") {
console.log('Setting up ...')
open_indexDB(request.url, Object.values(request.templates).map(template => template.name)).then(async () => {
let result;
try {
result = await chrome.scripting.executeScript({
target: {tabId: request.id},
func: collect_content
});
} catch (e) {
console.warn(e.message || e);
return;
}
const nodes = result[0].result
push_to_object_store(request.url, 'original', nodes)
.then(() => {
}).catch(console.error);
}
)
}
if (request.action === "set_template") {
let obj = {};
obj['template_' + request.url] = request.template;
chrome.storage.local.set(obj, function () {
console.log('Template for', request.url, 'saved:', request.template);
push_cached_template(request);
});
}
if (request.action === "clear-cache") {
await clear_object_stores(request.url)
}
if (request.action === "push_openai_to_background") {
let obj = {};
obj['openai'] = request.key;
chrome.storage.local.set(obj, function () {
console.log('OpenAI key set');
});
chrome.runtime.sendMessage({
action: "push_openai_to_popup",
openai: request.key
});
}
if (request.action === "setup_finished") {
chrome.storage.local.get('openai', function (result) {
let api_key = result['openai']
if (typeof api_key === "undefined") {
api_key = "insert OpenAI API key"
}
chrome.runtime.sendMessage({
action: "push_openai_to_popup",
openai: api_key
});
})
}
if (request.action === "generate") {
chrome.runtime.sendMessage({
action: "generation_initialized",
});
new Promise(async (resolve, reject) => {
try {
const original = await fetch_from_object_store(request.url, 'original');
chrome.storage.local.get(['template_' + request.url, 'openai'], async function (result) {
if (result['template_' + request.url]) {
let nodes = [];
for await (const node of generate(original, result['template_' + request.url], result['openai'], request.key)) {
nodes.push(node)
const xpath = node.xpath;
const html = node.html;
const func = function (xpath, html) {
const node = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (node) {
node.innerHTML = html;
} else {
console.log(`No element matches the provided XPath: ${xpath}`);
}
}
chrome.scripting.executeScript({
target: {tabId: request.id},
function: func,
args: [xpath, html]
});
}
if (nodes.length) {
await push_to_object_store(request.url, result['template_' + request.url].name, nodes)
}
console.log("Page rewritten!...");
resolve();
} else {
console.log('Cannot find required keys in local storage.');
reject('Cannot find required keys in local storage.');
}
});
} catch (error) {
console.log('Error in processing:', error);
reject(error);
}
}).then(() => {
chrome.storage.local.get(['template_' + request.url], async function (result) {
chrome.runtime.sendMessage({
action: "template_cached",
template_name: result['template_' + request.url].name
});
});
chrome.runtime.sendMessage({
action: "generation_completed",
});
}).catch(error => {
console.log(`Error during generation: ${error}`);
});
}
}
chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) {
await process_request(request, sender, sendResponse);
return true;
});