17
17
*/
18
18
import browser from 'webextension-polyfill' ;
19
19
20
- import { type AddUrlToTrustedMessage , MessageType } from '../../common/messages' ;
20
+ import {
21
+ type AddUrlToTrustedMessage ,
22
+ type BadfilterRuleAsTrustedMessage ,
23
+ MessageType ,
24
+ } from '../../common/messages' ;
21
25
import { DocumentBlockApi , TabsApi } from '../api' ;
22
26
import { engine } from '../engine' ;
23
27
import { messageHandler } from '../message-handler' ;
@@ -34,9 +38,31 @@ export class DocumentBlockService {
34
38
public static async init ( ) : Promise < void > {
35
39
await DocumentBlockApi . init ( ) ;
36
40
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
+ }
38
49
}
39
50
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
+
40
66
/**
41
67
* Listener for the event of adding a domain to trusted domains.
42
68
*
@@ -49,10 +75,21 @@ export class DocumentBlockService {
49
75
await DocumentBlockApi . setTrustedDomain ( url ) ;
50
76
await engine . update ( ) ;
51
77
52
- const tab = await TabsApi . getActive ( ) ;
78
+ DocumentBlockService . updateActiveTab ( url ) ;
79
+ }
53
80
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 ) ;
57
94
}
58
95
}
0 commit comments