diff --git a/browser-extension/plugin/src/transform-general.js b/browser-extension/plugin/src/transform-general.js index da192173..8a146221 100644 --- a/browser-extension/plugin/src/transform-general.js +++ b/browser-extension/plugin/src/transform-general.js @@ -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 }; + + +