1- import type {
2- MethodIndex ,
3- MethodMetadataWithExecutors ,
4- } from '../common/types' ;
1+ import type { MethodIndex , MethodMetadataWithExecutors } from '../common/types' ;
52import { generate_urls } from '../common/generate-urls' ;
63import { isPageDark } from '../utils/isPageDark' ;
74import { DISABLED_ID } from '../methods/methods' ;
@@ -21,46 +18,91 @@ export class DarkPageHandler {
2118 private disconnectAllObservers : ( ) => void ,
2219 ) { }
2320
24- async checkAndPersistDarkPage ( ) : Promise < void > {
21+ async disableDarkMode ( ) {
22+ this . disconnectAllObservers ( ) ;
23+ const urlKey = generate_urls ( window . document . documentURI ) [ 0 ] ;
24+ const mergedConfigured = window . merged_configured ;
25+ if ( ! mergedConfigured [ urlKey ] || mergedConfigured [ urlKey ] !== DISABLED_ID ) {
26+ const tabId = await this . tabIdPromise ;
27+ const configuredTabs = window . configured_tabs ;
28+ const url = configuredTabs ?. [ tabId as number ] ;
29+ await browser . runtime . sendMessage ( {
30+ action : 'set_configured_page' ,
31+ key : url ,
32+ value : DISABLED_ID ,
33+ } ) ;
34+ window . do_it ( { } , DISABLED_ID ) ;
35+ }
36+ }
37+
38+ async checkAndPersistDarkPage ( retry = false ) : Promise < void > {
2539 const isDefaultMethod =
2640 ( await this . methodResolver . isDefaultMethod ( window . document . documentURI ) )
2741 || ( await this . methodResolver . isDefaultMethod ( window . location . hostname ) ) ;
2842
29- if ( ! isDefaultMethod ) return ;
43+ if ( ! isDefaultMethod ) {
44+ return ;
45+ }
3046
31- const method = await this . methodResolver . getMethodForUrl (
47+ // Check if we have a previous log entry for this URL
48+ if ( window . location . hostname ) {
49+ let result = await browser . runtime . sendMessage ( {
50+ action : 'get_dark_page_logs' ,
51+ key : window . location . hostname ,
52+ } ) ;
53+ if ( result === undefined || result === null ) {
54+ // Try again with documentURI if no result
55+ result = await browser . runtime . sendMessage ( {
56+ action : 'get_dark_page_logs' ,
57+ key : window . document . documentURI ,
58+ } ) ;
59+ }
60+ if ( result === false ) {
61+ return ;
62+ }
63+ }
64+
65+ const currentMethod = await this . methodResolver . getMethodForUrl (
3266 window . document . documentURI ,
3367 ) ;
34- const sheets = document . querySelectorAll ( '.dblt-ykjmwcnxmi' ) ;
3568
36- for ( const sheet of sheets ) {
37- sheet ?. setAttribute ( 'media' , '(not all)' ) ;
38- }
69+ const method = await this . methodResolver . getMethodForUrl (
70+ window . document . documentURI ,
71+ ) ;
3972
40- if ( isPageDark ( method ) ) {
41- this . disconnectAllObservers ( ) ;
73+ if ( retry ) {
74+ // If not the initial check do a simple check
75+ if ( isPageDark ( method ) ) {
76+ await this . disableDarkMode ( ) ;
77+ }
78+ } else {
79+ const sheets = document . querySelectorAll ( '.dblt-ykjmwcnxmi' ) ;
4280 for ( const sheet of sheets ) {
43- sheet ?. setAttribute ( 'media' , '' ) ;
81+ sheet ?. setAttribute ( 'media' , '(not all) ' ) ;
4482 }
45- const urlKey = generate_urls ( window . document . documentURI ) [ 0 ] ;
46- const mergedConfigured = window . merged_configured ;
47- if (
48- ! mergedConfigured [ urlKey ]
49- || mergedConfigured [ urlKey ] !== DISABLED_ID
50- ) {
51- const tabId = await this . tabIdPromise ;
52- const configuredTabs = window . configured_tabs ;
53- const url = configuredTabs ?. [ tabId as number ] ;
54- await browser . runtime . sendMessage ( {
55- action : 'set_configured_page' ,
56- key : url ,
57- value : DISABLED_ID ,
83+ const keepFromBlinding = document . createElement ( 'style' ) ;
84+ keepFromBlinding . appendChild (
85+ document . createTextNode ( '* { background: #000 !important; }' ) ,
86+ ) ;
87+ document . body . appendChild ( keepFromBlinding ) ;
88+
89+ await window . do_it ( { } , DISABLED_ID , async ( ) => {
90+ requestAnimationFrame ( async ( ) => {
91+ if ( isPageDark ( method ) ) {
92+ await this . disableDarkMode ( ) ;
93+ } else {
94+ await browser . runtime . sendMessage ( {
95+ action : 'log_dark_page_check' ,
96+ key : window . location . hostname ,
97+ value : false ,
98+ } ) ;
99+ for ( const sheet of sheets ) {
100+ sheet ?. setAttribute ( 'media' , '' ) ;
101+ }
102+ await window . do_it ( { } , currentMethod . number ) ;
103+ }
104+ keepFromBlinding . remove ( ) ;
58105 } ) ;
59- window . do_it ( { } , DISABLED_ID ) ;
60- }
61- } else {
62- sheets . forEach ( ( link ) => {
63- link . setAttribute ( 'media' , '' ) ;
64106 } ) ;
65107 }
66108 }
0 commit comments