Skip to content

Commit

Permalink
[iOS] - Add exemption list for NightMode (#27798)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T authored Feb 26, 2025
1 parent 0f008b2 commit c95d92a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ class Tab: NSObject {
didSet {
var isNightModeEnabled = false

if let fetchedTabURL = fetchedURL, nightMode {
if let fetchedTabURL = fetchedURL, nightMode,
!DarkReaderScriptHandler.isNightModeBlockedURL(fetchedTabURL)
{
isNightModeEnabled = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,48 @@ public class DarkReaderScriptHandler: TabContentScript {
Preferences.General.nightModeEnabled.value = enabled

for tab in tabManager.allTabs {
if let fetchedTabURL = tab.fetchedURL {
if let fetchedTabURL = tab.fetchedURL, !isNightModeBlockedURL(fetchedTabURL) {
tab.nightMode = enabled
}
}
}
}

extension DarkReaderScriptHandler {
// Check if the website is a night mode blocked site
public static func isNightModeBlockedURL(_ url: URL) -> Bool {
// The reason we use `normalizedHost` is because we want to keep the eTLD+1
// IE: (search.brave.com instead of brave.com)
guard let urlHost = url.normalizedHost(), let registry = url.publicSuffix else {
return false
}

// Remove the `registry` so we get `search.brave` instead of `search.brave.com`
// We get amazon instead of amazon.co.uk
// mail.proton instead of mail.proton.com
let domainName = urlHost.dropLast(registry.count).trimmingCharacters(
in: CharacterSet(charactersIn: ".")
)

// Site domains that should NOT inject night mode
let siteList = Set([
"twitter", "youtube", "twitch",
"soundcloud", "github", "netflix",
"imdb", "mail.proton", "amazon",
"x", "search.brave",

// Search Engines
"search.brave", "google", "qwant",
"startpage", "duckduckgo", "presearch",

// Dev sites
"macrumors", "9to5mac", "developer.apple",

// Casual sites
"wowhead", "xbox", "thegamer",
"cineplex", "starwars",
])

return siteList.contains(domainName)
}
}
66 changes: 66 additions & 0 deletions ios/brave-ios/Tests/ClientTests/DarkModeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import TestHelpers
import UIKit
import XCTest

@testable import Brave

class DarkModeTests: XCTestCase {

func testNightModeBlockedURL() {
let blockList = [
"twitter.com",
"m.twitter.com",
"youtube.com",
"m.youtube.com",
"www.youtube.com",
"amazon.co.uk",
"amazon.com",
"m.amazon.com",
"m.amazon.co.uk",
"x.com",
"m.x.com",
"search.brave.com",
"m.search.brave.com",
"www.search.brave.com",
]

let allowList = [
"twitter.brave.com",
"m.twitter.brave.com",
"m.search.amazon.com",
"m.search.amazon.co.uk",
"search.amazon.co.uk",
"search.amazon.brave.com",
"m.brave.com",
"brave.com",
"talk.brave.com",
"bbc.co.uk",
"news.bbc.co.uk",
"bbc.example.co.uk",
"example.bbc.foo.co.uk",
"example.bbc.foo.com",
]

for urlString in blockList {
guard let url = URL(string: "https://\(urlString)") else {
XCTFail("Invalid URL: \(urlString)")
continue
}

XCTAssertTrue(DarkReaderScriptHandler.isNightModeBlockedURL(url))
}

for urlString in allowList {
guard let url = URL(string: "https://\(urlString)") else {
XCTFail("Invalid URL: \(urlString)")
continue
}

XCTAssertFalse(DarkReaderScriptHandler.isNightModeBlockedURL(url))
}
}
}

0 comments on commit c95d92a

Please sign in to comment.