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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx pretty-quick --staged
15 changes: 9 additions & 6 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
console.log("onHistoryStateUpdated", details);
chrome.tabs.sendMessage(details.tabId, {
type: 'navigation'
});
}, {url: [{hostSuffix: 'github.com'}]});
chrome.webNavigation.onHistoryStateUpdated.addListener(
function (details) {
console.log("onHistoryStateUpdated", details);
chrome.tabs.sendMessage(details.tabId, {
type: "navigation",
});
},
{ url: [{ hostSuffix: "github.com" }] }
);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing changed here just re-formatting

104 changes: 61 additions & 43 deletions contentScript.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,103 @@
/* Default. Places heading at end of line */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing changed here just re-formatting

function addHeadingToBack(heading, headingPrefix) {
headingPrefix.classList.add('github-a11y-heading-prefix', 'github-a11y-heading-prefix-after');
headingPrefix.classList.add(
"github-a11y-heading-prefix",
"github-a11y-heading-prefix-after"
);
headingPrefix.textContent = ` ${heading.tagName.toLowerCase()}`;
heading.classList.add('github-a11y-heading', 'github-a11y-heading-after');
heading.classList.add("github-a11y-heading", "github-a11y-heading-after");
heading.append(headingPrefix);
}

/* Places heading in front of line */
function addHeadingToFront(heading, headingPrefix) {
headingPrefix.textContent = `${heading.tagName.toLowerCase()} `;
headingPrefix.classList.add('github-a11y-heading-prefix');
heading.classList.add('github-a11y-heading');
headingPrefix.classList.add("github-a11y-heading-prefix");
heading.classList.add("github-a11y-heading");
heading.insertBefore(headingPrefix, heading.firstChild);
}

/* Append accessibility info to DOM */
function appendAccessibilityInfo() {
const outdatedElements = document.querySelectorAll('.github-a11y-heading-prefix, .github-a11y-img-caption')
const outdatedElements = document.querySelectorAll(
".github-a11y-heading-prefix, .github-a11y-img-caption"
);
for (const element of outdatedElements) {
element.remove();
}

document.querySelectorAll('.markdown-body').forEach(function(commentBody) {
commentBody.querySelectorAll('img').forEach(function(image) {
const parent = image.closest('a');
if (!parent || image.closest('animated-image')) return
document.querySelectorAll(".markdown-body").forEach(function (commentBody) {
commentBody.querySelectorAll("img").forEach(function (image) {
const parent = image.closest("a");
if (!parent || image.closest("animated-image")) return;

validateImages(parent, image)
validateImages(parent, image);
});

commentBody.querySelectorAll('animated-image').forEach(function(animatedImage) {
validateImagesInsideAnimatedPlayer(animatedImage)
});

commentBody.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(function(heading) {
const headingPrefix = document.createElement('span');
headingPrefix.setAttribute('aria-hidden', 'true');
commentBody
.querySelectorAll("animated-image")
.forEach(function (animatedImage) {
validateImagesInsideAnimatedPlayer(animatedImage);
});

addHeadingToBack(heading, headingPrefix); // Swappable with `addHeadingToFront`
});
commentBody
.querySelectorAll("h1, h2, h3, h4, h5, h6")
.forEach(function (heading) {
const headingPrefix = document.createElement("span");
headingPrefix.setAttribute("aria-hidden", "true");

addHeadingToBack(heading, headingPrefix); // Swappable with `addHeadingToFront`
});
});
}

function validateImages(parent, image) {
const altText = image.getAttribute('alt') ? image.getAttribute('alt').trim() : "";
const parentAriaLabel = parent.getAttribute('aria-label') && parent.getAttribute('aria-label').trim()

if (!image.hasAttribute('alt') || altText === "" && !parentAriaLabel) {
image.classList.add('github-a11y-img-missing-alt')
const altText = image.getAttribute("alt")
? image.getAttribute("alt").trim()
: "";
const parentAriaLabel =
parent.getAttribute("aria-label") &&
parent.getAttribute("aria-label").trim();

if (!image.hasAttribute("alt") || (altText === "" && !parentAriaLabel)) {
image.classList.add("github-a11y-img-missing-alt");
} else {
const subtitle = createSubtitleElement();
parent.classList.add('github-a11y-img-container');
parent.classList.add("github-a11y-img-container");

if (parentAriaLabel) {
subtitle.textContent = parentAriaLabel;
} else {
subtitle.textContent = altText;
}

image.insertAdjacentElement('afterend', subtitle);
image.insertAdjacentElement("afterend", subtitle);
}
}

function createSubtitleElement() {
const subtitle = document.createElement('span');
subtitle.setAttribute('aria-hidden', 'true');
subtitle.classList.add('github-a11y-img-caption', 'github-a11y-img-caption-with-alt');

return subtitle
const subtitle = document.createElement("span");
subtitle.setAttribute("aria-hidden", "true");
subtitle.classList.add(
"github-a11y-img-caption",
"github-a11y-img-caption-with-alt"
);

return subtitle;
}

function validateImagesInsideAnimatedPlayer(animatedImage) {
const image = animatedImage.querySelector('img');
const altText = image.getAttribute('alt') ? image.getAttribute('alt').trim() : "";
const image = animatedImage.querySelector("img");
const altText = image.getAttribute("alt")
? image.getAttribute("alt").trim()
: "";

if (!image.hasAttribute('alt') || altText === "") {
animatedImage.classList.add('github-a11y-img-missing-alt')
if (!image.hasAttribute("alt") || altText === "") {
animatedImage.classList.add("github-a11y-img-missing-alt");
} else {
const subtitle = createSubtitleElement();
subtitle.textContent = altText;
animatedImage.classList.add('github-a11y-img-container');
animatedImage.classList.add("github-a11y-img-container");
animatedImage.appendChild(subtitle);
}
}
Expand All @@ -93,27 +111,27 @@ appendAccessibilityInfo();

/* Debounce to avoid redundant appendAccessibilityInfo calls */
let timer;
let observer = new MutationObserver(function(mutationList) {
let observer = new MutationObserver(function (mutationList) {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
for (const mutation of mutationList) {
observer.disconnect();
if (mutation.target.closest('.markdown-body, .js-commit-preview')) {
if (mutation.target.closest(".markdown-body, .js-commit-preview")) {
appendAccessibilityInfo();
}
observe();
}
}, 100);
})
});

const observe = ()=> {
const observe = () => {
observer.observe(document.body, {
childList: true,
subtree: true
subtree: true,
});
};

document.addEventListener('turbo:load', () => {
document.addEventListener("turbo:load", () => {
appendAccessibilityInfo();
observe();
})
});
Loading