Skip to content

Commit ead9c4f

Browse files
authored
Refactor: convert isMV3 to compilation config __MV3__
1 parent fe3c5fa commit ead9c4f

12 files changed

Lines changed: 28 additions & 16 deletions

File tree

src/background/extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import createStaticStylesheet from '../generators/static-theme';
1717
import {createSVGFilterStylesheet, getSVGFilterMatrixValue, getSVGReverseFilterMatrixValue} from '../generators/svg-filter';
1818
import type {ExtensionData, FilterConfig, News, Shortcuts, UserSettings, TabInfo, TabData} from '../definitions';
1919
import {isSystemDarkModeEnabled} from '../utils/media-query';
20-
import {isFirefox, isMV3, isThunderbird} from '../utils/platform';
20+
import {isFirefox, isThunderbird} from '../utils/platform';
2121
import {MessageType} from '../utils/message';
2222
import {logInfo, logWarn} from '../utils/log';
2323
import {PromiseBarrier} from '../utils/promise-barrier';
@@ -37,6 +37,7 @@ interface SystemColorState {
3737
}
3838

3939
declare const __DEBUG__: boolean;
40+
declare const __MV3__: boolean;
4041

4142
export class Extension implements ExtensionState {
4243
config: ConfigManager;
@@ -97,7 +98,7 @@ export class Extension implements ExtensionState {
9798
}
9899

99100
private async MV3initSystemColorStateManager(isDark: boolean | null): Promise<void> {
100-
if (!isMV3) {
101+
if (!__MV3__) {
101102
return;
102103
}
103104
if (!this.systemColorStateManager) {
@@ -115,7 +116,7 @@ export class Extension implements ExtensionState {
115116
}
116117

117118
private async MV3saveSystemColorStateManager(): Promise<void> {
118-
if (!isMV3) {
119+
if (!__MV3__) {
119120
return;
120121
}
121122
if (!this.systemColorStateManager) {
@@ -155,7 +156,7 @@ export class Extension implements ExtensionState {
155156
break;
156157
}
157158
case 'system':
158-
if (isMV3) {
159+
if (__MV3__) {
159160
isAutoDark = this.isDark;
160161
if (this.isDark === null) {
161162
logWarn('System color scheme is unknown. Defaulting to Dark.');

src/background/make-chromium-happy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {MessageType} from '../utils/message';
22
import type {Message} from '../definitions';
3-
import {isChromium, isMV3} from '../utils/platform';
3+
import {isChromium} from '../utils/platform';
4+
5+
declare const __MV3__: boolean;
46

57
// This function exists to prevent Chrome from logging an error about
68
// closed conduit. It just sends a dummy message in response to incomming message
79
// to utilise open conduit. This response message is not even used on the other side.
810
export function makeChromiumHappy() {
9-
if (isMV3 || !isChromium) {
11+
if (__MV3__ || !isChromium) {
1012
return;
1113
}
1214
chrome.runtime.onMessage.addListener((message: Message, _, sendResponse) => {

src/background/tab-manager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import {canInjectScript} from '../background/utils/extension-api';
22
import {createFileLoader} from './utils/network';
33
import type {FetchRequestParameters} from './utils/network';
44
import type {Message} from '../definitions';
5-
import {isFirefox, isMV3, isOpera, isThunderbird} from '../utils/platform';
5+
import {isFirefox, isOpera, isThunderbird} from '../utils/platform';
66
import {MessageType} from '../utils/message';
77
import {logWarn} from '../utils/log';
88
import {StateManager} from './utils/state-manager';
99

10+
declare const __MV3__: boolean;
11+
1012
async function queryTabs(query: chrome.tabs.QueryInfo) {
1113
return new Promise<chrome.tabs.Tab[]>((resolve) => {
1214
chrome.tabs.query(query, (tabs) => resolve(tabs));
@@ -97,7 +99,7 @@ export default class TabManager {
9799

98100
switch (message.type) {
99101
case MessageType.CS_FRAME_CONNECT: {
100-
if (isMV3) {
102+
if (__MV3__) {
101103
onColorSchemeChange(message.data.isDark);
102104
}
103105
await this.stateManager.loadState();
@@ -164,7 +166,7 @@ export default class TabManager {
164166
break;
165167
}
166168
case MessageType.CS_FRAME_RESUME: {
167-
if (isMV3) {
169+
if (__MV3__) {
168170
onColorSchemeChange(message.data.isDark);
169171
}
170172
await this.stateManager.loadState();
@@ -253,7 +255,7 @@ export default class TabManager {
253255
.filter((tab) => !Boolean(this.tabs[tab.id]))
254256
.forEach((tab) => {
255257
if (!tab.discarded) {
256-
if (isMV3) {
258+
if (__MV3__) {
257259
chrome.scripting.executeScript({
258260
target: {
259261
tabId: tab.id,

src/inject/dynamic-theme/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import type {FilterConfig, DynamicThemeFix} from '../../definitions';
1616
import {generateUID} from '../../utils/uid';
1717
import type {AdoptedStyleSheetManager} from './adopted-style-manger';
1818
import {createAdoptedStyleSheetOverride} from './adopted-style-manger';
19-
import {isFirefox, isMV3} from '../../utils/platform';
19+
import {isFirefox} from '../../utils/platform';
2020
import {injectProxy} from './stylesheet-proxy';
2121
import {parse} from '../../utils/color';
2222
import {parsedURLCache} from '../../utils/url';
2323
import {variablesStore} from './variables';
2424

2525
declare const __TEST__: boolean;
26+
declare const __MV3__: boolean;
2627
const INSTANCE_ID = generateUID();
2728
const styleManagers = new Map<StyleElement, StyleManager>();
2829
const adoptedStyleManagers = [] as AdoptedStyleSheetManager[];
@@ -149,7 +150,7 @@ function createStaticStyleOverrides() {
149150
document.head.insertBefore(rootVarsStyle, variableStyle.nextSibling);
150151

151152
const injectProxyArg = !(fixes && fixes.disableStyleSheetsProxy);
152-
if (isMV3) {
153+
if (__MV3__) {
153154
injectProxyScriptMV3(injectProxyArg);
154155
// Notify dedicated injector of the data
155156
document.dispatchEvent(new CustomEvent('__darkreader__stylesheetProxy__arg', {detail: injectProxyArg}));

src/inject/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {isSystemDarkScheme, runColorSchemeChangeDetector, stopColorSchemeChangeD
77
import {collectCSS} from './dynamic-theme/css-collection';
88
import type {Message} from '../definitions';
99
import {MessageType} from '../utils/message';
10-
import {isMV3, isThunderbird} from '../utils/platform';
10+
import {isThunderbird} from '../utils/platform';
1111

1212
let unloaded = false;
1313

14+
declare const __MV3__: boolean;
15+
1416
// TODO: Use background page color scheme watcher when browser bugs fixed.
1517
runColorSchemeChangeDetector((isDark) => {
1618
logInfo('Media query was changed');
@@ -41,7 +43,7 @@ function sendMessage(message: Message) {
4143
}
4244
};
4345

44-
if (isMV3) {
46+
if (__MV3__) {
4547
/*
4648
* Background can be unreachable if:
4749
* - extension was disabled

src/utils/platform.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ export const isXMLHttpRequestSupported = typeof XMLHttpRequest === 'function';
7171

7272
export const isFetchSupported = typeof fetch === 'function';
7373

74-
export const isMV3 = (globalThis as any).chrome && (globalThis as any).chrome.runtime && (globalThis as any).chrome.runtime.getManifest && (globalThis as any).chrome.runtime.getManifest().manifest_version === 3;
75-
7674
export const isCSSColorSchemePropSupported = (() => {
7775
if (typeof document === 'undefined') {
7876
return false;

tasks/bundle-api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ async function bundleAPI({debug}) {
3030
rollupPluginReplace({
3131
preventAssignment: true,
3232
'__DEBUG__': 'false',
33+
'__MV3__': 'false',
3334
'__TEST__': 'false',
3435
}),
3536
].filter((x) => x)

tasks/bundle-js.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ async function bundleJS(/** @type {JSEntry} */entry, {debug, watch}) {
153153
rollupPluginReplace({
154154
preventAssignment: true,
155155
'__DEBUG__': debug ? 'true' : 'false',
156+
'__MV3__': entry.platform === PLATFORM.CHROME_MV3,
156157
'__PORT__': watch ? String(PORT) : '-1',
157158
'__TEST__': 'false',
158159
'__WATCH__': watch ? 'true' : 'false',

tests/browser/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const config = {
1414
globals: {
1515
'ts-jest': {tsconfig: '<rootDir>/tests/browser/tsconfig.json'},
1616
__DEBUG__: false,
17+
__MV3__: false,
1718
__TEST__: true,
1819
product: 'chrome',
1920
},

tests/inject/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function configureKarma(config, env) {
5252
rollupPluginReplace({
5353
preventAssignment: true,
5454
'__DEBUG__': 'false',
55+
'__MV3__': 'false',
5556
'__PORT__': '-1',
5657
'__TEST__': 'true',
5758
'__WATCH__': 'false',

0 commit comments

Comments
 (0)