Skip to content

Commit

Permalink
fix: system appearance responsivness
Browse files Browse the repository at this point in the history
  • Loading branch information
hadnet committed Aug 16, 2023
1 parent 8e3e80e commit 605664d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ let isDark: boolean;
function setCSSTheme(isDark: boolean, tabId: number | undefined) {
nonNullable(tabId);
if (isDark) {
chrome.scripting.removeCSS({
files: ['a11y-light.css'],
target: {
tabId,
},
});
chrome.scripting.insertCSS(
{
files: ['a11y-dark.css'],
Expand All @@ -17,13 +23,13 @@ function setCSSTheme(isDark: boolean, tabId: number | undefined) {
console.log('Dark CSS inserted');
}
);
} else {
chrome.scripting.removeCSS({
files: ['a11y-light.css'],
files: ['a11y-dark.css'],
target: {
tabId,
},
});
} else {
chrome.scripting.insertCSS(
{
files: ['a11y-light.css'],
Expand All @@ -35,12 +41,6 @@ function setCSSTheme(isDark: boolean, tabId: number | undefined) {
console.log('Light CSS inserted');
}
);
chrome.scripting.removeCSS({
files: ['a11y-dark.css'],
target: {
tabId,
},
});
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/pages/Content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Actions, type Action } from '../../types';
let shimmer: HTMLDivElement;
let loading = false;

function convertToMarkdownAndHighlight(t: Element, color: Color) {
function convertToMarkdownAndHighlight(t: Element, isDark: boolean) {
if (t.textContent) {
const tweet = t.textContent;
// TODO: refactor this code in order to work with many code snippet chunks and not only one.
const [start, codeString, end] = tweet?.split('```');
if (!codeString) return;
const [language] = codeString.split('\n');
const color: Color = isDark ? 'white' : 'black';

const copyBtn = document.createElement('a');
copyBtn.innerHTML = getIcon('copy', color);
Expand Down Expand Up @@ -120,17 +121,16 @@ const isDark = theme.matches;
const observer = new MutationObserver((mutations) => {
const theme = window.matchMedia('(prefers-color-scheme: dark)');
const isDark = theme.matches;
const color = isDark ? 'white' : 'black';
if (isInit) {
const tweets = document.querySelectorAll('[data-testid="tweetText"]');
tweets.forEach((tweet) => convertToMarkdownAndHighlight(tweet, color));
tweets.forEach((tweet) => convertToMarkdownAndHighlight(tweet, isDark));
isInit = false;
}
for (let mutation of mutations) {
for (let node of mutation.addedNodes) {
if (!(node instanceof HTMLElement)) continue;
for (let tweet of node.querySelectorAll('[data-testid="tweetText"]')) {
convertToMarkdownAndHighlight(tweet, color);
convertToMarkdownAndHighlight(tweet, isDark);
}
}
}
Expand Down

0 comments on commit 605664d

Please sign in to comment.