-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
151 lines (129 loc) · 3.83 KB
/
Copy pathcontent.js
File metadata and controls
151 lines (129 loc) · 3.83 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
let isEnabled = true;
const defaultSettings = {
highlightEnabled: true,
positiveColor: '#d4edda',
negativeColor: '#f8d7da',
neutralColor: '#e2e3e5',
intensity: 0.5
};
chrome.runtime.sendMessage({ action: "getSettings" }, (settings) => {
isEnabled = settings.highlightEnabled !== false;
if (isEnabled) {
analyzeAndHighlightPage(settings || defaultSettings);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "updateHighlights") {
isEnabled = request.settings.highlightEnabled !== false;
if (isEnabled) {
analyzeAndHighlightPage(request.settings);
} else {
removeHighlights();
}
}
});
function analyzeAndHighlightPage(settings) {
removeHighlights();
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{
acceptNode: function (node) {
return node.nodeValue.trim().length > 0 &&
!isInsideIgnoredElement(node) ?
NodeFilter.FILTER_ACCEPT :
NodeFilter.FILTER_REJECT;
}
},
false
);
const textNodes = [];
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
processNodesInChunks(textNodes, settings, 0, 5); // smaller chunk to avoid rate limit
}
function isInsideIgnoredElement(node) {
const ignoredTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'CODE', 'PRE'];
let parent = node.parentNode;
while (parent && parent !== document.body) {
if (ignoredTags.includes(parent.tagName) ||
parent.classList.contains('code') ||
parent.classList.contains('no-highlight')) {
return true;
}
parent = parent.parentNode;
}
return false;
}
async function analyzeSentiment(text) {
try {
const response = await fetch('huggingface link here', {
method: 'POST',
headers: {
'Authorization': 'add proper thing here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ inputs: text })
});
const result = await response.json();
if (!Array.isArray(result) || !result[0]) return 0;
const label = result[0][0]?.label;
switch (label) {
case 'LABEL_2': return 1; // positive
case 'LABEL_0': return -1; // negative
case 'LABEL_1': return 0; // neutral
default: return 0;
}
} catch (e) {
console.error("Sentiment API error:", e);
return 0;
}
}
async function processNodesInChunks(nodes, settings, start, chunkSize) {
const end = Math.min(start + chunkSize, nodes.length);
for (let i = start; i < end; i++) {
const node = nodes[i];
const text = node.nodeValue.trim();
if (text.length > 0) {
const sentiment = await analyzeSentiment(text);
highlightNode(node, sentiment, settings);
}
}
if (end < nodes.length) {
setTimeout(() => {
processNodesInChunks(nodes, settings, end, chunkSize);
}, 200); // Delay to respect API rate limits
}
}
function highlightNode(node, score, settings) {
const parent = node.parentNode;
if (!parent || parent.tagName === 'SCRIPT' || parent.tagName === 'STYLE') {
return;
}
let color;
const intensity = settings.intensity || 0.5;
if (score > 0.1) {
color = settings.positiveColor || '#d4edda';
} else if (score < -0.1) {
color = settings.negativeColor || '#f8d7da';
} else {
color = settings.neutralColor || '#e2e3e5';
}
const span = document.createElement('span');
span.className = 'sentiment-highlight';
span.style.backgroundColor = color;
span.style.opacity = intensity;
parent.replaceChild(span, node);
span.appendChild(node);
}
function removeHighlights() {
const highlights = document.querySelectorAll('.sentiment-highlight');
highlights.forEach(highlight => {
const parent = highlight.parentNode;
if (parent) {
parent.replaceChild(highlight.firstChild, highlight);
parent.normalize();
}
});
}