From 67c0052da22ab282c569873f6f14dba357f2cc4b Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Sun, 13 Feb 2022 20:04:11 -0700 Subject: [PATCH] Fix bug where headings get duplicated --- background.js | 7 +++-- contentScript.js | 69 ++++++++++++++++++++++++++++-------------------- manifest.json | 3 +-- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/background.js b/background.js index b1244c3..dc36763 100644 --- a/background.js +++ b/background.js @@ -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'}]}); diff --git a/contentScript.js b/contentScript.js index 681f325..f69ee28 100644 --- a/contentScript.js +++ b/contentScript.js @@ -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(); diff --git a/manifest.json b/manifest.json index c580cca..3259494 100644 --- a/manifest.json +++ b/manifest.json @@ -7,7 +7,6 @@ "content_scripts": [ { "matches": ["https://*.github.com/*"], - "run_at": "document_idle", "css": ["styles.css"], "js": ["contentScript.js"] @@ -16,5 +15,5 @@ "background": { "scripts": ["background.js"] }, - "permissions": ["*://github.com/*", "tabs", "webNavigation"] + "permissions": ["*://*.github.com/*", "tabs", "webNavigation"] }