Created
January 5, 2023 03:54
-
-
Save yuhui/f740392b3e3b82deb8f0c5cd44f179e6 to your computer and use it in GitHub Desktop.
Add titles to links based on the inner text content or image ALT text
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
/** | |
* Add "data-link-title" to all signup modal DIVs using the DIV's inner text (or first class name if there is no innertext) | |
*/ | |
const signupModals = document.querySelectorAll('a[data-toggle="modal"][data-target="#signup"]:not([data-link-title]), div[data-toggle="modal"][data-target="#signup"]:not([data-link-title])'); | |
signupModals.forEach((signupModal) => { | |
const signupModalText = (signupModal.innerText || signupModal.textContent).trim(); | |
if (!signupModalText) { | |
// inner text not found | |
// if there's an image, use its "alt" attribute | |
const imageElement = signupModal.querySelector('img'); | |
if (imageElement) { | |
signupModalText = imageElement.getAttribute('alt'); | |
} else { | |
// fallback: use the first classname | |
signupModalText = signupModal.classList[0].replace(/_mobile/, ''); | |
} | |
} | |
signupModalText = signupModalText.replace(/\n/g, ' ').replace(/\s+/g, ' '); | |
signupModal.setAttribute('data-link-title', signupModalText); | |
if (signupModal.tagName.toUpperCase() === 'DIV') { | |
const signupModalDivs = signupModal.querySelectorAll('div[class*="_locked"], div.video-listing-component__overlay'); | |
signupModalDivs.forEach((signupModalDiv) => { | |
signupModalDiv.setAttribute('data-link-title', signupModalText); | |
signupModalDiv.setAttribute('onclick', `s_objectID="${signupModalText}";`); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment