This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show alternative image text below images when available (#101)
- Loading branch information
1 parent
9942300
commit 6bd28ec
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export default async () => { | ||
const imgContainers = document.querySelectorAll('.AdaptiveMedia-photoContainer, .Gallery-media'); | ||
|
||
for (const imgContainer of imgContainers) { | ||
// Exit if it already exists | ||
// Test on content because a same container can be reused (Gallery) | ||
if (imgContainer.querySelector('.refined-twitter_image-alt')) { | ||
continue; | ||
} | ||
|
||
const imgs = imgContainer.querySelectorAll('img'); | ||
for (const img of imgs) { | ||
const imgAlt = img.getAttribute('alt'); | ||
|
||
if (!imgAlt) { | ||
continue; | ||
} | ||
|
||
imgContainer.classList.add('refined-twitter_image-alt_container'); | ||
|
||
if (imgContainer.classList.contains('AdaptiveMedia-photoContainer')) { | ||
const ancestor1 = imgContainer.parentNode; | ||
ancestor1.classList.add('refined-twitter_image-alt_photocontainer'); | ||
if (ancestor1.parentNode.classList.contains('AdaptiveMedia-container')) { | ||
const ancestor2 = ancestor1.parentNode; | ||
if (ancestor2.parentNode.classList.contains('is-square')) { | ||
ancestor2.parentNode.classList.add('refined-twitter_image-alt_ancestor-not-square'); | ||
} | ||
} | ||
} | ||
|
||
const altDiv = document.createElement('div'); | ||
altDiv.textContent = imgAlt; | ||
|
||
if (imgContainer.classList.contains('Gallery-media')) { | ||
altDiv.className = 'refined-twitter_image-alt refined-twitter_image-alt_top'; | ||
img.parentNode.prepend(altDiv); | ||
} else { | ||
altDiv.className = 'refined-twitter_image-alt refined-twitter_image-alt_bottom'; | ||
img.parentNode.append(altDiv); | ||
} | ||
|
||
img.classList.add('refined-twitter_image-alt_img'); | ||
} | ||
} | ||
}; |
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