Skip to content

Commit

Permalink
chore: converted content script to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Jul 3, 2022
1 parent 3c763db commit e99be9f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"version": "1.2.1",
"description": "",
"scripts": {

"clean": "rm -rf dist",
"prestart": "npm run clean",
"start": "parcel watch src/manifest.json --host localhost --config @parcel/config-webextension",
"prebuild": "npm run clean",
"build": "parcel build src/manifest.json --config @parcel/config-webextension",
"postbuild": "zip -r -j dist/a11y-twitter.zip dist/*",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
23 changes: 10 additions & 13 deletions src/contentScript.js → src/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ function a11yCheck(event) {
const attachments = document.querySelector('[data-testid="attachments"]');

// Need to check for one or more descriptions.
attachments.querySelectorAll('[role="link"][aria-label="Add description"]');
const mediaAltTextLinks = attachments
? attachments.querySelectorAll(
`[role="link"][aria-label="${ADD_DESCRIPTION_LABEL}"], [role="link"][aria-label="${ADD_DESCRIPTIONS_LABEL}"]`,
)
: [];
const mediaAltTextLinks =
attachments?.querySelectorAll<HTMLElement>(
`[role="link"][aria-label="${ADD_DESCRIPTION_LABEL}"], [role="link"][aria-label="${ADD_DESCRIPTIONS_LABEL}"]`,
) ?? [];

const [missingAltTextLink] = [...mediaAltTextLinks].filter((link) => {
const linkTextElement = link.querySelector('[data-testid="altTextLabel"]');
const linkTextElement = link.querySelector<HTMLElement>(
'[data-testid="altTextLabel"]',
);

// Need to check for one or more descriptions.
return (
linkTextElement.innerText === ADD_DESCRIPTION_LABEL ||
linkTextElement.innerText === ADD_DESCRIPTIONS_LABEL
return [ADD_DESCRIPTION_LABEL, ADD_DESCRIPTIONS_LABEL].includes(
linkTextElement?.innerText ?? '',
);
});

Expand All @@ -43,9 +42,7 @@ function a11yCheck(event) {

askedOnce = true;

const shouldAddDescriptions = confirm(ADD_DESCRIPTIONS_MESSAGE);

if (shouldAddDescriptions) {
if (confirm(ADD_DESCRIPTIONS_MESSAGE)) {
event.preventDefault();
event.stopPropagation();
missingAltTextLink.click();
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
"matches": ["https://*.twitter.com/*"],
"run_at": "document_idle",
"js": ["contentScript.js"]
"js": ["contentScript.ts"]
}
]
}

0 comments on commit e99be9f

Please sign in to comment.