Last active
October 3, 2017 01:10
-
-
Save peabnuts123/c5fde1494d32dcfe5512d6f4b2eaf89f to your computer and use it in GitHub Desktop.
Naively replace gendered pronouns with non-gendered versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
TITLE: | |
Them.js | |
VERSION: | |
0.1.0 (released 2017-10-03) | |
PURPOSE: | |
Replace gendered pronouns on web-pages with non-gendered versions in a non-intrusive way. | |
Not only do gendered pronouns subtly reinforce sexism and gender bias, I personally find that | |
it tends to make things harder to read when gendered pronouns are used without context. | |
I often find myself wondering if there were prior details wherein the gender of the subject | |
was established without my noticing, leading to confusion / backtracking etc. | |
ORIGINAL AUTHOR: | |
Jeff (@Peabnuts123) | |
LICENCE: | |
UNLICENSED. Do what you want with this script. Let me know if you do something cool with it 🙂 | |
BACKLOG: | |
- Detect ambiguous pronouns | |
- Capture DOM changes / refactor when page content changes (especially for SPAs) | |
- Make into Chrome Extension | |
- Use \b for word boundaries in Regex | |
*/ | |
(function() { | |
// Lookup for words that will be replaced | |
let replacements = { // SAMPLE | |
'he': 'they', // "He walked outside" | |
'him': 'them', // "It scared him" | |
'his': 'their', // "His bicycle was red" | |
// @NOTE: ambigous, 'theirs' is not possible to detect without greater logic | |
// 'his': 'theirs', // "The bicycle was his" | |
'himself': 'themselves', // "By himself" | |
'she': 'they', // "She walked outside" | |
'her': 'them', // "It scared her" | |
// @NOTE: ambiguous, 'their' not possible to detect without greater logic | |
// 'her': 'their', // "Her bicycle as red" | |
'hers': 'theirs', // "The bicycle was hers" | |
'herself': 'themselves', // "By herself" | |
}; | |
// Only do replacements in certain tags | |
// This is to not screw up the logic of the page by replacing | |
// the HTML of critical elements (<html>, for example) | |
let tagWhitelist = [ | |
'P', | |
'LI', | |
'SPAN', | |
]; | |
// Build Regex to detect words to be replaced | |
let targetWords = Object.keys(replacements); | |
let targetRegex = new RegExp(`(\\W|^)(${targetWords.join('|')})(\\W|$)`, 'g'); | |
// Find elements on page with whitelisted tags | |
let elements = document.querySelectorAll(tagWhitelist.join(', ')); | |
elements.forEach((element) => { | |
// Search element for target words | |
let textMatch = element.innerText.match(targetRegex); | |
let htmlMatch = element.innerHTML.match(targetRegex); | |
// If the element contains a TEXT match, | |
// do an HTML replace. If you replace innerText, you lose ALL previous | |
// HTML in the element. | |
if (textMatch) { | |
let newHtml = element.innerHTML.replace(targetRegex, (wholeMatch, pre, match, post) => { | |
// Look up replacement word | |
let replacement = replacements[match.toLowerCase()]; | |
// Replace the character before + after the match + the looked up replacement word | |
return `${pre}${replacement}${post}`; | |
}); | |
element.innerHTML = newHtml; | |
} | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Paste the following into a bookmark to make a button that performs this translation on the current page | |
javascript:(function(){var a={he:'they',him:'them',his:'their',himself:'themselves',she:'they',her:'them',hers:'theirs',herself:'themselves'},c=Object.keys(a),d=new RegExp('(\\W|^)('+c.join('|')+')(\\W|$)','g'),e=document.querySelectorAll(['P','LI','SPAN'].join(', '));e.forEach(function(f){var g=f.innerText.match(d),h=f.innerHTML.match(d);if(g){var i=f.innerHTML.replace(d,function(j,k,l,m){var n=a[l.toLowerCase()];return''+k+n+m});f.innerHTML=i}})})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment