Skip to content

Commit 47b1980

Browse files
committed
add blocking page for mv3
1 parent 01f1cb5 commit 47b1980

File tree

8 files changed

+107
-19
lines changed

8 files changed

+107
-19
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Send last filters update time during issue reporting [#3055].
13+
- Blocked by `$document` rules blocking page for MV3.
1314

1415
### Changed
1516

16-
- Updated Safebrowsing and blocked by rules blocking pages.
17+
- Updated blocking pages for MV2 — Safebrowsing and blocked by `$document` rules.
1718

1819
### Removed
1920

Extension/pages/blocking/blocked/page-handler.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,20 @@ const updatePlaceholders = ({ url, filterName, rule }: PlaceholdersData): void =
110110
/**
111111
* Adds listener to handle "Proceed Anyway" button click.
112112
*
113-
* @param url URL to add to trusted.
113+
* @param url In MV2 — URL to add to trusted, in MV3 — URL to proceed to.
114+
* @param rule In MV2 — not needed, in MV3 — rule that blocked the page and should be badfiltered.
114115
*/
115-
const addProceedAnywayListener = (url: string): void => {
116+
const addProceedAnywayListener = (url: string, rule: string): void => {
116117
const proceedAnywayBtn = getElementById(BLOCKED_PROCEED_ANYWAY_BTN_ID);
117118

118119
proceedAnywayBtn.addEventListener('click', (e: Event) => {
119120
e.preventDefault();
120121

121-
messenger.addUrlToTrusted(url);
122+
if (__IS_MV3__) {
123+
messenger.badfilterRuleAsTrusted(rule, url);
124+
} else {
125+
messenger.addUrlToTrusted(url);
126+
}
122127
});
123128
};
124129

@@ -175,7 +180,7 @@ const runInit = ({
175180

176181
updateTheme(theme);
177182
updatePlaceholders({ url, filterName, rule });
178-
addProceedAnywayListener(url);
183+
addProceedAnywayListener(url, rule);
179184
addAddToAllowlistListener(url);
180185
addGoBackButtonListener(BLOCKED_GO_BACK_BTN_ID);
181186
};

Extension/src/background/api/document-block.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,32 @@ export class DocumentBlockApi {
6161
}
6262

6363
/**
64-
* Adds the domain to the list of trusted domains with DocumentBlockApi#TRUSTED_TTL_MS timeout.
64+
* Stores trusted domain in the storage.
65+
* Removes expired domains and duplicates.
6566
*
66-
* @param url A trusted domain to add.
67+
* @param input A trusted domain to add.
6768
*/
68-
public static async setTrustedDomain(url: string): Promise<void> {
69-
const { hostname } = new URL(url);
70-
69+
public static async storeTrustedDomain(input: string): Promise<void> {
7170
const now = Date.now();
7271

7372
// remove expired and duplicates
7473
const data = trustedDomainsStorage
7574
.getData()
76-
.filter(({ expires, domain }) => (now < expires) && (domain !== hostname));
75+
.filter(({ expires, domain }) => (now < expires) && (domain !== input));
7776

78-
data.push({ domain: hostname, expires: DocumentBlockApi.TRUSTED_TTL_MS + now });
77+
data.push({ domain: input, expires: DocumentBlockApi.TRUSTED_TTL_MS + now });
7978

8079
await trustedDomainsStorage.setData(data);
8180
}
81+
82+
/**
83+
* Adds the domain to the list of trusted domains with DocumentBlockApi#TRUSTED_TTL_MS timeout.
84+
*
85+
* @param url A trusted domain to add.
86+
*/
87+
public static async setTrustedDomain(url: string): Promise<void> {
88+
const { hostname } = new URL(url);
89+
90+
DocumentBlockApi.storeTrustedDomain(hostname);
91+
}
8292
}

Extension/src/background/engine/engine-mv3.ts

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
filteringLogApi,
4141
CommonFilterApi,
4242
iconsApi,
43+
DocumentBlockApi,
4344
} from '../api';
4445
import { RulesLimitsService, rulesLimitsService } from '../services/rules-limits/rules-limits-service-mv3';
4546
import { UserRulesService } from '../services/userrules';
@@ -240,6 +241,8 @@ export class Engine implements TsWebExtensionEngine {
240241
// };
241242
// }));
242243

244+
const blockingTrustedRules = await DocumentBlockApi.getTrustedDomains();
245+
243246
return {
244247
declarativeLogEnabled: filteringLogApi.isOpen(),
245248
// TODO: revert to actual customFilters when their support will be added back
@@ -253,6 +256,7 @@ export class Engine implements TsWebExtensionEngine {
253256
settings,
254257
filtersPath: 'filters/',
255258
ruleSetsPath: 'filters/declarative',
259+
blockingTrustedRules,
256260
};
257261
}
258262

Extension/src/background/keep-alive.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ export class KeepAlive {
162162
}
163163
return;
164164
} catch (e) {
165-
logger.error(`[KeepAlive.executeScriptOnTab] Tab ${tab.id} error: ${e}`);
165+
// use debug level to avoid extension errors when blocking pages is loading
166+
logger.debug(`[KeepAlive.executeScriptOnTab] Tab ${tab.id} error: ${e}`);
166167
}
167168
}
168169
}

Extension/src/background/services/document-block.ts

+43-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
*/
1818
import browser from 'webextension-polyfill';
1919

20-
import { type AddUrlToTrustedMessage, MessageType } from '../../common/messages';
20+
import {
21+
type AddUrlToTrustedMessage,
22+
type BadfilterRuleAsTrustedMessage,
23+
MessageType,
24+
} from '../../common/messages';
2125
import { DocumentBlockApi, TabsApi } from '../api';
2226
import { engine } from '../engine';
2327
import { messageHandler } from '../message-handler';
@@ -34,9 +38,31 @@ export class DocumentBlockService {
3438
public static async init(): Promise<void> {
3539
await DocumentBlockApi.init();
3640

37-
messageHandler.addListener(MessageType.AddUrlToTrusted, DocumentBlockService.onAddUrlToTrusted);
41+
if (__IS_MV3__) {
42+
messageHandler.addListener(
43+
MessageType.BadfilterRuleAsTrusted,
44+
DocumentBlockService.onBadfilterRuleAsTrusted,
45+
);
46+
} else {
47+
messageHandler.addListener(MessageType.AddUrlToTrusted, DocumentBlockService.onAddUrlToTrusted);
48+
}
3849
}
3950

51+
/**
52+
* Updates the active tab with the provided URL.
53+
*
54+
* @param url The URL to update the active tab with.
55+
*/
56+
private static updateActiveTab = async (url: string): Promise<void> => {
57+
const tab = await TabsApi.getActive();
58+
59+
if (!tab?.id) {
60+
return;
61+
}
62+
63+
await browser.tabs.update(tab.id, { url });
64+
};
65+
4066
/**
4167
* Listener for the event of adding a domain to trusted domains.
4268
*
@@ -49,10 +75,21 @@ export class DocumentBlockService {
4975
await DocumentBlockApi.setTrustedDomain(url);
5076
await engine.update();
5177

52-
const tab = await TabsApi.getActive();
78+
DocumentBlockService.updateActiveTab(url);
79+
}
5380

54-
if (tab?.id) {
55-
await browser.tabs.update(tab.id, { url });
56-
}
81+
/**
82+
* Listener for the event of adding a domain to trusted domains.
83+
*
84+
* @param message Message of type {@link AddUrlToTrustedMessage}.
85+
* @param message.data Contains string url domain.
86+
*/
87+
private static async onBadfilterRuleAsTrusted({ data }: BadfilterRuleAsTrustedMessage): Promise<void> {
88+
const { rule, url } = data;
89+
90+
await DocumentBlockApi.storeTrustedDomain(rule);
91+
await engine.update();
92+
93+
DocumentBlockService.updateActiveTab(url);
5794
}
5895
}

Extension/src/common/messages/constants.ts

+13
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export enum MessageType {
123123
SaveCookieLogEvent = 'saveCookieRuleEvent',
124124
LoadSettingsJson = 'loadSettingsJson',
125125
AddUrlToTrusted = 'addUrlToTrusted',
126+
BadfilterRuleAsTrusted = 'badfilterRuleAsTrusted',
126127
SetPreserveLogState = 'setPreserveLogState',
127128
GetUserRulesEditorData = 'getUserRulesEditorData',
128129
GetEditorStorageContent = 'getEditorStorageContent',
@@ -536,6 +537,14 @@ export type AddUrlToTrustedMessage = {
536537
};
537538
};
538539

540+
export type BadfilterRuleAsTrustedMessage = {
541+
type: MessageType.BadfilterRuleAsTrusted;
542+
data: {
543+
rule: string;
544+
url: string;
545+
};
546+
};
547+
539548
export type SetNotificationViewedMessage = {
540549
type: MessageType.SetNotificationViewed;
541550
data: {
@@ -897,6 +906,10 @@ export type MessageMap = {
897906
message: AddUrlToTrustedMessage;
898907
response: void;
899908
};
909+
[MessageType.BadfilterRuleAsTrusted]: {
910+
message: BadfilterRuleAsTrustedMessage;
911+
response: void;
912+
};
900913
[MessageType.CurrentLimitsMv3]: {
901914
message: CurrentLimitsMv3Message;
902915
response: Mv3LimitsCheckResult;

Extension/src/pages/services/messenger.ts

+17
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import {
5959
type SetNotificationViewedMessage,
6060
type UpdateFullscreenUserRulesThemeMessage,
6161
type AddUrlToTrustedMessage,
62+
type BadfilterRuleAsTrustedMessage,
6263
type ExtractedMessage,
6364
type OpenSafebrowsingTrustedMessage,
6465
} from '../../common/messages';
@@ -965,6 +966,22 @@ class Messenger {
965966
return this.sendMessage(MessageType.AddUrlToTrusted, { url });
966967
}
967968

969+
/**
970+
* Sends a message to the background page to badfilter specific rule as trusted temporarily
971+
* and url is needed to update the tab url after blocking page display.
972+
*
973+
* @param rule Rule to badfilter.
974+
* @param url Url to update for the tab.
975+
*
976+
* @returns Promise that resolves after the message is sent.
977+
*/
978+
async badfilterRuleAsTrusted(
979+
rule: BadfilterRuleAsTrustedMessage['data']['rule'],
980+
url: BadfilterRuleAsTrustedMessage['data']['url'],
981+
): Promise<ExtractMessageResponse<MessageType.BadfilterRuleAsTrusted>> {
982+
return this.sendMessage(MessageType.BadfilterRuleAsTrusted, { rule, url });
983+
}
984+
968985
/**
969986
* Sends a message to the background page to get user rules editor data.
970987
*

0 commit comments

Comments
 (0)