Skip to content

Commit

Permalink
Remember CSSOM tweaks importance
Browse files Browse the repository at this point in the history
  • Loading branch information
phgn0 committed Jun 27, 2022
1 parent c66f3cb commit c884079
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
16 changes: 12 additions & 4 deletions source/content-script/modifications/CSSOM/responsiveStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export default class ResponsiveStyleModifier implements PageModifier {
}
}

private expiredRulesOriginalStyles: object[] = [];
private expiredRulesOriginalStyles: { [key: string]: [string, boolean] }[] =
[];
private addedRules: CSSStyleRule[] = [];
enableResponsiveStyles() {
// disable desktop-only styles
Expand All @@ -85,7 +86,10 @@ export default class ResponsiveStyleModifier implements PageModifier {
// TODO measure performance of this
const obj = {};
for (const key of rule.style) {
obj[key] = rule.style.getPropertyValue(key);
obj[key] = [
rule.style.getPropertyValue(key),
rule.style.getPropertyPriority(key) === "important",
];
}
this.expiredRulesOriginalStyles.push(obj);

Expand All @@ -109,10 +113,14 @@ export default class ResponsiveStyleModifier implements PageModifier {

disableResponsiveStyles() {
this.expiredRules.map((rule, index) => {
for (const [key, value] of Object.entries(
for (const [key, [value, isImportant]] of Object.entries(
this.expiredRulesOriginalStyles[index]
)) {
rule.style.setProperty(key, value);
rule.style.setProperty(
key,
value,
isImportant ? "important" : ""
);
}
});

Expand Down
21 changes: 16 additions & 5 deletions source/content-script/modifications/CSSOM/stylePatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export default class StylePatchesModifier implements PageModifier {
}

private styleRuleTweaks: [CSSStyleRule, { [key: string]: string }][] = [];
private originalStyleValues: [CSSStyleRule, { [key: string]: string }][] =
[];
private originalStyleValues: [
CSSStyleRule,
{ [key: string]: [string, boolean] }
][] = [];
async prepare() {
// iterating the CSSOM can take time -- so run outside transitionIn()
// TODO maybe do in afterTransitionIn()?
Expand All @@ -32,8 +34,14 @@ export default class StylePatchesModifier implements PageModifier {

transitionOut() {
this.originalStyleValues.forEach(([rule, propertiesToSet]) => {
for (const [key, value] of Object.entries(propertiesToSet)) {
rule.style.setProperty(key, value);
for (const [key, [value, isImportant]] of Object.entries(
propertiesToSet
)) {
rule.style.setProperty(
key,
value,
isImportant ? "important" : ""
);
}
});
}
Expand Down Expand Up @@ -88,7 +96,10 @@ export default class StylePatchesModifier implements PageModifier {

const originalValues = {};
for (const [key] of Object.entries(propertiesToSet)) {
originalValues[key] = rule.style.getPropertyValue(key);
originalValues[key] = [
rule.style.getPropertyValue(key),
rule.style.getPropertyPriority(key) === "important",
];
}
this.originalStyleValues.push([rule, originalValues]);
}
Expand Down
20 changes: 16 additions & 4 deletions source/content-script/modifications/CSSOM/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ export default class ThemeModifier implements PageModifier {
return siteSupportsDarkMode;
}

private activeDarkModeStyleTweaks: [CSSStyleRule, object][] = [];
private activeDarkModeStyleTweaks: [
CSSStyleRule,
{ [key: string]: string }
][] = [];
private enableDarkModeStyleTweaks() {
// patch site stylesheet colors
this.cssomProvider.iterateRules((rule) => {
Expand All @@ -410,7 +413,10 @@ export default class ThemeModifier implements PageModifier {
// save properties for restoration later
const obj = {};
for (const key of rule.style) {
obj[key] = rule.style.getPropertyValue(key);
obj[key] = [
rule.style.getPropertyValue(key),
rule.style.getPropertyPriority(key) === "important",
];
}
this.activeDarkModeStyleTweaks.push([rule, obj]);

Expand All @@ -428,8 +434,14 @@ export default class ThemeModifier implements PageModifier {

private disableDarkModeStyleTweaks() {
this.activeDarkModeStyleTweaks.map(([rule, originalStyle]) => {
for (const [key, value] of Object.entries(originalStyle)) {
rule.style.setProperty(key, value);
for (const [key, [value, isImportant]] of Object.entries(
originalStyle
)) {
rule.style.setProperty(
key,
value,
isImportant ? "important" : ""
);
}
});
this.activeDarkModeStyleTweaks = [];
Expand Down

0 comments on commit c884079

Please sign in to comment.