Skip to content

Commit

Permalink
uliStore v 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hardik-pratap-singh committed Jul 18, 2024
1 parent 6c4bb35 commit 1c22a94
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions browser-extension/plugin/src/transform-general.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,124 @@ const processNewlyAddedNodesGeneral = function (firstBody) {
observer.observe(firstBody, config);
};


// Code inserted below is for uliStore

let uliStore = []
let body = document.querySelector("body");
const targetWords = ['crazy', 'stupid', 'mad', 'insane']; // Replace with your list of target words


/* indexTree()/getAllTextNodes() STARTS HERE */

function checkFalseTextNode(text, actualLengthOfText) {
let n = text.length;
let totalNewlineAndWhitespaces = 0;
for (let i = 0; i < n; i++) {
if (text[i] === "\n") {
totalNewlineAndWhitespaces += 1;
}

else if (text[i] === " ") {
totalNewlineAndWhitespaces += 1;
}

}
if (totalNewlineAndWhitespaces === actualLengthOfText) {
//False Text Node Confirmed
return true;
}
else {
//True Text Node Confirmed
return false;
}
}


function getAllTextNodes(node) {
if (node.nodeType === 3) {
//If node.data contains just whitespaces and \n, then its a false text node

// let whitespaces = (node.data.split(" ").length - 1);
// console.log(node.data) ;
if (checkFalseTextNode(node.data, node.length) === false) {
uliStore.push({ node: node, parent: node.parentNode });
}
// textNodes.push({ node: node, parent: node.parentNode });
return;
}

let children = Array.from(node.childNodes);
for (let i = 0; i < children.length; i++) {
getAllTextNodes(children[i]);
}
}


/* indexTree()/getAllTextNodes() ENDS HERE */
// getAllTextNodes(body);







/* locateSlur() STARTS HERE */

// locateSlur(uliStore)
function findPositions(word, text) {
let positions = {};

let len = word.length
let loc = []
let index = text.toString().indexOf(word);
while (index !== -1) {
let obj = {} ;
loc.push([index , index + len]);
index = text.toString().indexOf(word, index + 1);
}


if(loc.length !== 0){
positions.slurText = word
positions.slurLocation = loc ;
}
return positions;
}


function locateSlur(uliStore){
let n = uliStore.length ;

for(let i = 0 ; i < n ; i++){
let store = uliStore[i] ; //This will contain the textNode
let parentNode = store.parent
let text = store.node.textContent
//We have to look into this store for all the slurWords
let slurs = [] ;

targetWords.forEach(targetWord => {
let slurWord = targetWord ;
let pos = findPositions(slurWord , text) ;
if(Object.keys(pos).length !== 0){
slurs.push(pos)
}
})
uliStore[i].slurs = slurs ;
}
}


/* locateSlur() ENDS HERE */


// console.log("uliStore" , uliStore)


export default {
processNewlyAddedNodesGeneral
};



0 comments on commit 1c22a94

Please sign in to comment.