Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions background.js
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'}]});
69 changes: 40 additions & 29 deletions contentScript.js
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();
Comment on lines +38 to +41
Copy link
Owner Author

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.

});

appendAccessibilityInfo();
Copy link
Owner Author

Choose a reason for hiding this comment

The 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:

Content scripts running at "document_idle" do not need to listen for the window.onload event, they are guaranteed to run after the DOM is complete. If a script definitely needs to run after window.onload, the extension can check if onload has already fired by using the document.readyState property.

3 changes: 1 addition & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"content_scripts": [
{
"matches": ["https://*.github.com/*"],
"run_at": "document_idle",
"css": ["styles.css"],
"js": ["contentScript.js"]

Expand All @@ -16,5 +15,5 @@
"background": {
"scripts": ["background.js"]
},
"permissions": ["*://github.com/*", "tabs", "webNavigation"]
"permissions": ["*://*.github.com/*", "tabs", "webNavigation"]
}