-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand Slur redaction feature to more websites #254
Comments
For expanding to other websites, a single code base won't work across all websites. We will require site-specific DOM handling. This is because we have been querying the For example, the Adminer breaks here and the button and checkbox disappear on slur replacement
Uli works here on the main content with the
The https://en.wikipedia.org/wiki/%22Hello,_World!%22_program
|
Hey @duggalsu, I had a breakthrough. Pasting working code that I was able to use to find and replace text content on wikipedia. // Traverse dom nodes to find leaf node that are text nodes and process
function dft(nodes){
if(nodes.childNodes.length===0 && nodes.nodeType === 3){
console.log("found leaf text node", nodes)
nodes.textContent = nodes.textContent.replace("g", "🔥")
}
else{
nodes.childNodes.forEach((nodes)=>dft(nodes))
}
}
// filtering logic to get the right set of dom elements
let body = document.getElementsByTagName("body")
let first_body = body[0]
let elements = first_body.children
let elements_array = Array.from(elements)
let relevant_elements = elements_array.filter((element)=>["P","DIV"].includes(element.nodeName))
for(const element of relevant_elements){
dft(element)
} Try pasting this in the console with Wikipedia open, it will replace all instances of the character "g" with the fire emoji. There might be edge cases in my logic but see if you can find any reason why this is not an acceptable solution. References
To Investigate
|
I called my function dft, because i thought i'm doing depth first traversal. Just realized this might be better named bft for breadth first traversal. |
Hey @dennyabrain , I was also toying with nodeType earlier but this code is working successfully with minor edge-case handling and optimization. It is also handling |
@duggalsu With respect to listening for continuous changes, I got this unoptimized code working on reddit function dft(nodes){
if(nodes.childNodes.length===0 && nodes.nodeType === 3){
console.log("found leaf text node", nodes)
nodes.textContent = nodes.textContent.replace("g", "🔥")
}
else{
nodes.childNodes.forEach((nodes)=>dft(nodes))
}
}
let body = document.getElementsByTagName("body")
let first_body = body[0]
const config = { attributes: true, childList: true, subtree: true };
const callback = (mutationList, observer) => {
let elements = first_body.children
let elements_array = Array.from(elements)
let relevant_elements = elements_array.filter((element)=>["P","DIV"].includes(element.nodeName))
for(const element of relevant_elements){
dft(element)
}
}
const observer = new MutationObserver(callback);
observer.observe(first_body, config); There is a lot of documentation on MutationObserver here that I haven't read fully. And its quite possible that in my code above, our on change callback gets called a lot more than it should be. Can you first evaluate if this is a good solution and if yes, see if we can reduce the number of processing from our side? |
No description provided.
The text was updated successfully, but these errors were encountered: