Skip to content

Commit 7a8fa28

Browse files
authored
fix: slur metadata bug (#676)
1 parent b0f65be commit 7a8fa28

File tree

4 files changed

+172
-173
lines changed

4 files changed

+172
-173
lines changed

browser-extension/plugin/manifest.firefox.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "uli",
44
"description": "Moderate your Twitter Feed",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"author": "tattlemade|cis",
77
"content_security_policy": "default-src 'none'; connect-src https://uli-community.tattle.co.in/ https://uli-media.tattle.co.in/; font-src https://fonts.gstatic.com; object-src 'none'; script-src 'self' ; style-src https://fonts.googleapis.com 'self' 'unsafe-inline'; img-src https://uli-media.tattle.co.in/; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; report-uri 'none';",
88
"permissions": [

browser-extension/plugin/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "uli",
44
"description": "Moderate your Twitter Feed",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"author": "tattlemade|cis",
77
"content_security_policy": {
88
"extension_pages": "default-src 'none'; connect-src https://uli-community.tattle.co.in/ https://uli-media.tattle.co.in/; font-src https://fonts.gstatic.com; object-src 'none'; script-src 'self'; style-src https://fonts.googleapis.com 'self' 'unsafe-inline'; img-src https://uli-media.tattle.co.in/; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; report-uri 'none';"

browser-extension/plugin/src/transform-general.js

+112-113
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ function setCaretPosition(element, offset) {
5050
}
5151

5252
const processNewlyAddedNodesGeneral2 = function (firstBody, jsonData) {
53-
let targetWords = [];
54-
jsonData.forEach(slur => {
53+
let targetWords = [];
54+
jsonData.forEach((slur) => {
5555
const slurWord = Object.keys(slur)[0];
56-
targetWords.push(slurWord);
57-
targetWords.push(slurWord.charAt(0).toUpperCase() + slurWord.slice(1));
56+
targetWords.push(slurWord);
57+
// targetWords.push(slurWord.charAt(0).toUpperCase() + slurWord.slice(1));
5858
});
59-
6059
let uliStore = [];
6160
getAllTextNodes(firstBody, uliStore);
6261
abc = locateSlur(uliStore, targetWords);
@@ -89,7 +88,7 @@ const processNewlyAddedNodesGeneral = function (firstBody) {
8988
function checkFalseTextNode(text, actualLengthOfText) {
9089
let totalNewlineAndWhitespaces = 0;
9190
for (let i = 0; i < text.length; i++) {
92-
if (text[i] === "\n" || text[i] === " " || text[i] === "\t") {
91+
if (text[i] === '\n' || text[i] === ' ' || text[i] === '\t') {
9392
totalNewlineAndWhitespaces++;
9493
}
9594
}
@@ -98,70 +97,72 @@ function checkFalseTextNode(text, actualLengthOfText) {
9897

9998
// Function to recursively get all text nodes for a given node
10099
function getAllTextNodes(node, uliStore) {
101-
if (node.nodeType === 3) {
100+
if (node.nodeType === 3) {
102101
if (!checkFalseTextNode(node.data, node.length)) {
103102
uliStore.push({ node: node, parent: node.parentNode });
104103
}
105104
} else {
106105
let children = Array.from(node.childNodes);
107-
children.forEach(child => getAllTextNodes(child, uliStore));
106+
children.forEach((child) => getAllTextNodes(child, uliStore));
108107
}
109108
}
110109

111110
function findPositions(word, text) {
112111
let positions = {};
113-
let len = word.length
114-
let loc = []
112+
let len = word.length;
113+
let loc = [];
115114
let index = text.toString().indexOf(word);
116115
while (index !== -1) {
117116
let obj = {};
118117
loc.push([index, index + len]);
119118
index = text.toString().indexOf(word, index + 1);
120119
}
121120
if (loc.length !== 0) {
122-
positions.slurText = word
121+
positions.slurText = word;
123122
positions.slurLocation = loc;
124123
}
125124
return positions;
126125
}
127126

128-
129-
130127
function locateSlur(uliStore, targetWords) {
131128
let n = uliStore.length;
132129

133130
for (let i = 0; i < n; i++) {
134-
let store = uliStore[i];
135-
let parentNode = store.parent
136-
let textnode = store.node
137-
let text = store.node.textContent
138-
let tempParent = document.createElement("span");
131+
let store = uliStore[i];
132+
let parentNode = store.parent;
133+
let textnode = store.node;
134+
let text = store.node.textContent;
135+
let tempParent = document.createElement('span');
139136
tempParent.textContent = text;
140137
let slurs = [];
141138
let slurPresentInTempParent = false;
142-
targetWords.forEach(targetWord => {
139+
targetWords.forEach((targetWord) => {
143140
let slurWord = targetWord;
144141
let pos = findPositions(slurWord, text);
145142
if (Object.keys(pos).length !== 0) {
146-
slurs.push(pos)
143+
slurs.push(pos);
147144
}
148145

149146
if (tempParent.innerHTML.includes(targetWord)) {
150147
const className = `icon-container-${targetWord}`;
151-
const slurClass = `slur-container-${targetWord}`
148+
const slurClass = `slur-container-${targetWord}`;
149+
150+
// if (!tempParent.innerHTML.includes(`class="${slurClass}"`)) {
152151
const parts = tempParent.innerHTML.split(targetWord);
153-
// console.log("PARTS: ",parts)
154-
const replacedHTML = parts.join(`<span class="${slurClass}"><span class="slur">${targetWord}</span></span>`);
155-
tempParent.innerHTML = replacedHTML
152+
const replacedHTML = parts.join(
153+
`<span class="${slurClass}"><span class="slur">${targetWord}</span></span>`
154+
);
155+
tempParent.innerHTML = replacedHTML;
156156
slurPresentInTempParent = true;
157+
// }
157158
}
158-
})
159-
uliStore[i].nodeToParent = tempParent
159+
});
160+
uliStore[i].nodeToParent = tempParent;
160161
uliStore[i].slurs = slurs;
161162

162163
//O(1) complexity
163164
if (slurPresentInTempParent) {
164-
textnode.replaceWith(tempParent)
165+
textnode.replaceWith(tempParent);
165166
}
166167

167168
// console.log("TEMPParent: ",tempParent)
@@ -170,87 +171,89 @@ function locateSlur(uliStore, targetWords) {
170171
}
171172

172173
function addMetaData(targetWords, jsonData) {
173-
// console.log(targetWords)
174-
targetWords.forEach(targetWord => {
175-
const className = `slur-container-${targetWord}`
176-
const elements = Array.from(document.querySelectorAll(`.${className}`))
174+
targetWords.forEach((targetWord) => {
175+
const className = `slur-container-${targetWord}`;
176+
const elements = Array.from(document.querySelectorAll(`.${className}`));
177177
// console.log("ELEMENTS are: ",elements)
178-
elements.forEach(element => {
179-
178+
elements.forEach((element) => {
180179
// console.log("ELements InnerHTML:",element.innerHTML)
181180

182181
// element.innerHTML = element.innerHTML.replace(/<img[^>]*>/g, '')
183182

184-
let sup = document.createElement("span");
185-
let img = document.createElement("img");
186-
img.style.height = "0.6em"
187-
img.style.width = "0.6em"
188-
img.style.border = "1px solid black"
189-
img.style.cursor = "pointer"
190-
img.style.marginBottom="0.4em"
191-
192-
img.src = "https://raw.githubusercontent.com/tattle-made/Uli/main/uli-website/src/images/favicon-32x32.png"
193-
img.alt = "altText"
194-
195-
let span = document.createElement("span")
196-
span.style.display = "none"
197-
span.style.position = "absolute"
198-
span.style.marginLeft ="2px"
199-
span.style.marginTop ="2px"
200-
span.style.backgroundColor = "antiquewhite"
201-
span.style.border = "1px solid black"
202-
span.style.borderRadius = "12px"
203-
span.style.padding = "2px 6px"
204-
span.style.width = "16rem"
205-
span.style.textAlign = "justify"
206-
span.style.fontWeight = "lighter"
207-
span.style.color = "black"
208-
span.style.zIndex = "1000000000"; // This ensures it appears above other elements
209-
span.style.fontSize = "14px"
210-
span.style.textDecoration = "none"
211-
span.style.fontStyle = "normal"
212-
213-
span.innerHTML = `${targetWord} is an offensive word`
214-
215-
216-
jsonData.forEach(slur => {
217-
const slurWord = Object.keys(slur)[0];
183+
let sup = document.createElement('span');
184+
let img = document.createElement('img');
185+
img.style.height = '0.6em';
186+
img.style.width = '0.6em';
187+
img.style.border = '1px solid black';
188+
img.style.cursor = 'pointer';
189+
img.style.marginBottom = '0.4em';
190+
191+
img.src =
192+
'https://raw.githubusercontent.com/tattle-made/Uli/main/uli-website/src/images/favicon-32x32.png';
193+
img.alt = 'altText';
194+
195+
let span = document.createElement('span');
196+
span.style.display = 'none';
197+
span.style.position = 'absolute';
198+
span.style.marginLeft = '2px';
199+
span.style.marginTop = '2px';
200+
span.style.backgroundColor = 'antiquewhite';
201+
span.style.border = '1px solid black';
202+
span.style.borderRadius = '12px';
203+
span.style.padding = '2px 6px';
204+
span.style.width = '16rem';
205+
span.style.textAlign = 'justify';
206+
span.style.fontWeight = 'lighter';
207+
span.style.color = 'black';
208+
span.style.zIndex = '1000000000'; // This ensures it appears above other elements
209+
span.style.fontSize = '14px';
210+
span.style.textDecoration = 'none';
211+
span.style.fontStyle = 'normal';
212+
213+
span.innerHTML = `${targetWord} is an offensive word`;
214+
215+
jsonData.forEach((slur) => {
216+
const slurWord = Object.keys(slur)[0];
218217
if (slurWord.toLowerCase() === targetWord.toLowerCase()) {
219-
const slurDetails = slur[slurWord];
220-
let levelOfSeverity = slurDetails["Level of Severity"] ;
221-
let casual = slurDetails["Casual"] ;
222-
let approapriated = slurDetails["Appropriated"] ;
223-
let reason = slurDetails["If, Appropriated, Is it by Community or Others?"] ;
224-
let problematic = slurDetails["What Makes it Problematic?"];
225-
let categories = slurDetails["Categories"] ;
226-
let htmlContent = `` ;
227-
if(levelOfSeverity){
228-
htmlContent += `<p><span class="label"><b>Level of Severity:</b></span> ${levelOfSeverity}</p>`
218+
const slurDetails = slur[slurWord];
219+
let levelOfSeverity = slurDetails['Level of Severity'];
220+
let casual = slurDetails['Casual'];
221+
let approapriated = slurDetails['Appropriated'];
222+
let reason =
223+
slurDetails[
224+
'If, Appropriated, Is it by Community or Others?'
225+
];
226+
let problematic = slurDetails['What Makes it Problematic?'];
227+
let categories = slurDetails['Categories'];
228+
let htmlContent = ``;
229+
if (levelOfSeverity) {
230+
htmlContent += `<p><span class="label"><b>Level of Severity:</b></span> ${levelOfSeverity}</p>`;
229231
}
230-
if(casual){
231-
htmlContent += `<p><span class="label"><b>Casual:</b></span> ${casual}</p>`
232+
if (casual) {
233+
htmlContent += `<p><span class="label"><b>Casual:</b></span> ${casual}</p>`;
232234
}
233-
if(approapriated){
234-
htmlContent += `<p><span class="label"><b>Appropriated:</b></span> ${approapriated}</p>`
235+
if (approapriated) {
236+
htmlContent += `<p><span class="label"><b>Appropriated:</b></span> ${approapriated}</p>`;
235237
}
236-
if(reason){
237-
htmlContent += `<p><span class="label"><b>If, Appropriated, Is it by Community or Others?:</b></span> ${reason}</p>`
238+
if (reason) {
239+
htmlContent += `<p><span class="label"><b>If, Appropriated, Is it by Community or Others?:</b></span> ${reason}</p>`;
238240
}
239-
if(problematic){
240-
htmlContent += `<p><span class="label"><b>What Makes it Problematic?:</b></span> ${problematic}</p>`
241+
if (problematic) {
242+
htmlContent += `<p><span class="label"><b>What Makes it Problematic?:</b></span> ${problematic}</p>`;
241243
}
242-
if(categories.length > 0){
243-
htmlContent += `<p><span class="label"><b>Categories:</b></span> ${slurDetails["Categories"].join(', ')}</p>`
244+
if (categories.length > 0) {
245+
htmlContent += `<p><span class="label"><b>Categories:</b></span> ${slurDetails[
246+
'Categories'
247+
].join(', ')}</p>`;
244248
}
245249
span.innerHTML = htmlContent;
246250
}
247251
});
248-
252+
249253

250254

251255
// sup.appendChild(span)
252256

253-
254257
// element.append(sup)
255258
// element.append(img)
256259
// let sups = element.children[0]
@@ -265,49 +268,45 @@ function addMetaData(targetWords, jsonData) {
265268
// sups.children[0].style.display = "none"
266269
// });
267270

268-
269-
sup.appendChild(span)
271+
sup.appendChild(span);
270272

271273
// console.log("Element first child",element.children[0])
272274
// console.log("Element last child",element.children[element.children.length-1])
273275
// console.log("SUP: ",sup)
274276
// console.log("ELEMENT IS: ",element)
275277
// console.log("ELEMENT INNERHTML: ",element.innerHTML)
276-
277-
element.append(span)
278+
279+
element.append(span);
278280

279281
// console.log("ELEMENT AFTER IS: ",element)
280282
// element.append(img)
281-
let slur = element.children[0]
282-
slur.style.backgroundColor="#ffde2155"
283-
slur.style.boxShadow="0px 0px 5px #ffde21"
284-
slur.style.cursor = "pointer"
285-
286-
let metabox = element.children[1]
283+
let slur = element.children[0];
284+
slur.style.backgroundColor = '#ffde2155';
285+
slur.style.boxShadow = '0px 0px 5px #ffde21';
286+
slur.style.cursor = 'pointer';
287+
288+
let metabox = element.children[1];
287289
// console.log("METABOX IS: ",metabox)
288-
let spans = element.children[0].children[1]
290+
let spans = element.children[0].children[1];
289291
// const svgs = element.children[0].children[0];
290-
const svgs = element.children[element.children.length-1];
292+
const svgs = element.children[element.children.length - 1];
291293
slur.addEventListener('mouseover', function () {
292-
metabox.style.display = "inline-block"
294+
metabox.style.display = 'inline-block';
293295
});
294296

295297
slur.addEventListener('mouseout', function () {
296-
metabox.style.display = "none"
298+
metabox.style.display = 'none';
297299
});
298-
})
299-
})
300+
});
301+
});
300302
}
301303

302304
export default {
303305
processNewlyAddedNodesGeneral,
304306
processNewlyAddedNodesGeneral2,
305-
addMetaData,
306-
locateSlur,
307-
findPositions,
308-
getAllTextNodes,
309-
checkFalseTextNode
307+
addMetaData,
308+
locateSlur,
309+
findPositions,
310+
getAllTextNodes,
311+
checkFalseTextNode
310312
};
311-
312-
313-

0 commit comments

Comments
 (0)