-
Notifications
You must be signed in to change notification settings - Fork 5
Fix bug where headings get duplicated #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) { | ||
| chrome.tabs.executeScript(details.tabId,{file:"contentScript.js"}); | ||
| }, {url: [{hostEquals: 'github.com'}]}); | ||
| console.log("onHistoryStateUpdated", details); | ||
| chrome.tabs.sendMessage(details.tabId, { | ||
| type: 'navigation' | ||
| }); | ||
| }, {url: [{hostSuffix: 'github.com'}]}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,44 @@ | ||
| document.querySelectorAll('.markdown-body').forEach(function(commentBody) { | ||
| // Adds alt image overlay. This is hidden from accesibility tree. | ||
| commentBody.querySelectorAll('img').forEach(function(image) { | ||
| const altText = image.getAttribute('alt'); | ||
| if (!altText) { | ||
| image.classList.add('github-a11y-img-missing-alt') | ||
| } else { | ||
| const closestParagraph = image.closest('p'); | ||
| if (!closestParagraph) return; // TODO: handle when image is nested in elements like a table cell. | ||
|
|
||
| closestParagraph.classList.add('github-a11y-img-container'); | ||
|
|
||
| const subtitle = document.createElement('span'); | ||
| subtitle.setAttribute('aria-hidden', 'true'); | ||
| subtitle.textContent = altText; | ||
| subtitle.classList.add('github-a11y-img-caption'); | ||
|
|
||
| image.insertAdjacentElement('afterend', subtitle); | ||
| } | ||
| function appendAccessibilityInfo() { | ||
| document.querySelectorAll('.markdown-body').forEach(function(commentBody) { | ||
| // Adds alt image overlay. This is hidden from accesibility tree. | ||
| commentBody.querySelectorAll('img').forEach(function(image) { | ||
| const altText = image.getAttribute('alt'); | ||
| if (!altText) { | ||
| image.classList.add('github-a11y-img-missing-alt') | ||
| } else { | ||
| const closestParagraph = image.closest('p'); | ||
| if (!closestParagraph) return; // TODO: handle when image is nested in elements like a table cell. | ||
|
|
||
| closestParagraph.classList.add('github-a11y-img-container'); | ||
|
|
||
| const subtitle = document.createElement('span'); | ||
| subtitle.setAttribute('aria-hidden', 'true'); | ||
| subtitle.textContent = altText; | ||
| subtitle.classList.add('github-a11y-img-caption'); | ||
|
|
||
| image.insertAdjacentElement('afterend', subtitle); | ||
| } | ||
| }); | ||
|
|
||
| // Appends heading level to headings. This is hidden from accesibility tree. | ||
| commentBody.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(function(heading) { | ||
| heading.classList.add('github-a11y-heading'); | ||
| const headingPrefix = document.createElement('span'); | ||
|
|
||
| headingPrefix.setAttribute('aria-hidden', 'true'); | ||
| headingPrefix.classList.add('github-a11y-heading-prefix'); | ||
| headingPrefix.textContent = ` ${heading.tagName.toLowerCase()}`; | ||
|
|
||
| heading.append(headingPrefix); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Appends heading level to headings. This is hidden from accesibility tree. | ||
| commentBody.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(function(heading) { | ||
| heading.classList.add('github-a11y-heading'); | ||
| const headingPrefix = document.createElement('span'); | ||
|
|
||
| headingPrefix.setAttribute('aria-hidden', 'true'); | ||
| headingPrefix.classList.add('github-a11y-heading-prefix'); | ||
| headingPrefix.textContent = ` ${heading.tagName.toLowerCase()}`; | ||
|
|
||
| heading.append(headingPrefix); | ||
| }); | ||
| chrome.runtime.onMessage.addListener(async (message) => { | ||
| document.querySelectorAll(".github-a11y-img-caption").forEach(el => el.remove()); | ||
| document.querySelectorAll(".github-a11y-heading-prefix").forEach(el => el.remove()); | ||
| appendAccessibilityInfo(); | ||
| }); | ||
|
|
||
| appendAccessibilityInfo(); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method will run on initial page load after DOM is complete. From Content Script Chrome Docs:
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We add event listener to listen for navigation events and clean up DOM & append elements again.