Skip to content

Commit c95d92a

Browse files
authored
[iOS] - Add exemption list for NightMode (#27798)
1 parent 0f008b2 commit c95d92a

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ class Tab: NSObject {
414414
didSet {
415415
var isNightModeEnabled = false
416416

417-
if let fetchedTabURL = fetchedURL, nightMode {
417+
if let fetchedTabURL = fetchedURL, nightMode,
418+
!DarkReaderScriptHandler.isNightModeBlockedURL(fetchedTabURL)
419+
{
418420
isNightModeEnabled = true
419421
}
420422

ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/ScriptHandlers/Sandboxed/DarkReaderScriptHandler.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,48 @@ public class DarkReaderScriptHandler: TabContentScript {
100100
Preferences.General.nightModeEnabled.value = enabled
101101

102102
for tab in tabManager.allTabs {
103-
if let fetchedTabURL = tab.fetchedURL {
103+
if let fetchedTabURL = tab.fetchedURL, !isNightModeBlockedURL(fetchedTabURL) {
104104
tab.nightMode = enabled
105105
}
106106
}
107107
}
108108
}
109+
110+
extension DarkReaderScriptHandler {
111+
// Check if the website is a night mode blocked site
112+
public static func isNightModeBlockedURL(_ url: URL) -> Bool {
113+
// The reason we use `normalizedHost` is because we want to keep the eTLD+1
114+
// IE: (search.brave.com instead of brave.com)
115+
guard let urlHost = url.normalizedHost(), let registry = url.publicSuffix else {
116+
return false
117+
}
118+
119+
// Remove the `registry` so we get `search.brave` instead of `search.brave.com`
120+
// We get amazon instead of amazon.co.uk
121+
// mail.proton instead of mail.proton.com
122+
let domainName = urlHost.dropLast(registry.count).trimmingCharacters(
123+
in: CharacterSet(charactersIn: ".")
124+
)
125+
126+
// Site domains that should NOT inject night mode
127+
let siteList = Set([
128+
"twitter", "youtube", "twitch",
129+
"soundcloud", "github", "netflix",
130+
"imdb", "mail.proton", "amazon",
131+
"x", "search.brave",
132+
133+
// Search Engines
134+
"search.brave", "google", "qwant",
135+
"startpage", "duckduckgo", "presearch",
136+
137+
// Dev sites
138+
"macrumors", "9to5mac", "developer.apple",
139+
140+
// Casual sites
141+
"wowhead", "xbox", "thegamer",
142+
"cineplex", "starwars",
143+
])
144+
145+
return siteList.contains(domainName)
146+
}
147+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
import TestHelpers
6+
import UIKit
7+
import XCTest
8+
9+
@testable import Brave
10+
11+
class DarkModeTests: XCTestCase {
12+
13+
func testNightModeBlockedURL() {
14+
let blockList = [
15+
"twitter.com",
16+
"m.twitter.com",
17+
"youtube.com",
18+
"m.youtube.com",
19+
"www.youtube.com",
20+
"amazon.co.uk",
21+
"amazon.com",
22+
"m.amazon.com",
23+
"m.amazon.co.uk",
24+
"x.com",
25+
"m.x.com",
26+
"search.brave.com",
27+
"m.search.brave.com",
28+
"www.search.brave.com",
29+
]
30+
31+
let allowList = [
32+
"twitter.brave.com",
33+
"m.twitter.brave.com",
34+
"m.search.amazon.com",
35+
"m.search.amazon.co.uk",
36+
"search.amazon.co.uk",
37+
"search.amazon.brave.com",
38+
"m.brave.com",
39+
"brave.com",
40+
"talk.brave.com",
41+
"bbc.co.uk",
42+
"news.bbc.co.uk",
43+
"bbc.example.co.uk",
44+
"example.bbc.foo.co.uk",
45+
"example.bbc.foo.com",
46+
]
47+
48+
for urlString in blockList {
49+
guard let url = URL(string: "https://\(urlString)") else {
50+
XCTFail("Invalid URL: \(urlString)")
51+
continue
52+
}
53+
54+
XCTAssertTrue(DarkReaderScriptHandler.isNightModeBlockedURL(url))
55+
}
56+
57+
for urlString in allowList {
58+
guard let url = URL(string: "https://\(urlString)") else {
59+
XCTFail("Invalid URL: \(urlString)")
60+
continue
61+
}
62+
63+
XCTAssertFalse(DarkReaderScriptHandler.isNightModeBlockedURL(url))
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)