Show Slur Metadata Feature - tattle-made/Uli GitHub Wiki
Show Slur Metadata on hover
Details about the feature - https://github.com/tattle-made/Uli/issues/544
The broader goal of the feature is to show relevant metadata associated with a slur word, when the user hovers on a slur.
The main logic and code of this feature can be found in browser-extension/plugin/src/transform-general.js and browser-extension/plugin/src/slur-metadata.js
Detailed Documentation for processNewlyAddedNodesGeneral2 and Associated Functions
The main objective is to detect slurs on a webpage, inject Uli icons next to them, and display relevant metadata on hover, sourced from a slur_metadata.json file.
Here’s a breakdown of the code and how each part works:
Main Function: processNewlyAddedNodesGeneral2
Purpose: This is the primary function that processes newly added nodes when the user enables the "Enable Slur Metadata" functionality from the frontend. The function identifies and processes text nodes in the firstBody of the document and injects icons and metadata next to any detected slurs.
Workflow:
- Target Words List:
targetWordsis an array of slurs to be identified on the webpage. You can either hardcode the list (as shown) or import it from an external source (likeslurList).
- uliStore:
- An array to store all valid text nodes from the document. These nodes will later be checked for slurs.
- Get All Text Nodes:
- The function
getAllTextNodesis called with the body of the document (firstBody). It collects all text nodes (i.e., content within HTML elements) and pushes them intouliStore.
- The function
- Locate and Highlight Slurs:
- The
locateSlurfunction is then called, which scans the text nodes inuliStorefor any slur from thetargetWordslist. If found, it injects aspanelement with a Uli icon next to the slur and updates the text node with this new HTML structure.
- The
- Metadata Injection:
- The
addMetaDatafunction is invoked, which searches for the injectedspanelements and adds hoverable metadata (sourced fromslur_metadata.json) next to each slur.
- The
Function Breakdown:
1. getAllTextNodes(node, uliStore)
Purpose: Recursively collects all text nodes (non-empty) under a specified HTML node.
- Node Traversal:
- The function recursively goes through every child node of the provided parent node (
firstBodyin this case). - If a text node (nodeType === 3) is found, it checks if it contains only whitespace characters. If valid, the node and its parent are stored in
uliStore.
- The function recursively goes through every child node of the provided parent node (
- Check for Empty/Whitespace Text:
checkFalseTextNodeensures that text nodes with only whitespace (\n, , or\t) are excluded.
2. locateSlur(uliStore, targetWords)
Purpose: Searches through the text nodes in uliStore to identify slur words. If a slur is found, the text is modified to include a span with a Uli icon and metadata.
- Find Slur Locations:
findPositionsfunction checks if the slur (fromtargetWords) is present in the text. If found, it stores the positions of the slur in the text node.
- Inject Icons:
- If a slur is found in the text node, the function creates a
spanelement and injects it next to the slur. - The slur and the
spanare wrapped inside atempParent(aspanelement), so it doesn’t affect the layout or styling of the webpage.
- If a slur is found in the text node, the function creates a
- Replace the Text Node:
- After injecting the Uli icon, the original text node is replaced with the new
spanthat includes the slur and its corresponding Uli icon.
- After injecting the Uli icon, the original text node is replaced with the new
3. addMetaData(targetWords)
Purpose: Once the slurs are highlighted, this function adds metadata to the slur words, which appears on hover over the Uli icon.
- Find and Iterate Over Injected Elements:
- It selects all the
spanelements injected by thelocateSlurfunction using the class name formaticon-container-{targetWord}.
- It selects all the
- Create and Style Metadata:
- For each detected slur, the function creates a
supelement containing an image (the Uli icon) and a hiddenspanfor the metadata. - The metadata
spancontains information such as Level of Severity, Categories, and reasons why the slur is problematic, which is fetched from theslur_metadata.json.
- For each detected slur, the function creates a
- Show/Hide Metadata on Hover:
- Event listeners are attached to the Uli icons. When the user hovers over the icon (
mouseover), the hidden metadataspanis shown. When the hover ends (mouseout), the metadata is hidden again.
- Event listeners are attached to the Uli icons. When the user hovers over the icon (
4. findPositions(word, text)
Purpose: Finds the positions of a specific word (slur) within a given text.
- Functionality:
- It uses
indexOfto search for the slur in the text and returns an object that holds the slur word and its location in the text. - The function keeps searching for multiple instances of the word and stores all the positions in an array.
- It uses
Summary of Operations:
- Text Node Collection: The entire webpage is scanned, and text nodes are gathered and stored in
uliStore. - Slur Detection: The function checks each text node for any slur words present in the
targetWordslist. - Icon Insertion: For each slur found, a
spanelement containing the slur and a Uli icon is injected into the HTML. - Metadata Attachment: Metadata related to each slur (retrieved from
slur_metadata.json) is added, and this metadata is shown when the user hovers over the Uli icon.
This code efficiently modifies the webpage content, injecting icons and metadata dynamically without affecting the overall layout.
Running Tests for for Enable Slur Metadata functions
cd browser-extension/plugin
npm run test:api \-t 'enable'