-
Notifications
You must be signed in to change notification settings - Fork 38
Add Built-in Adblocker #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { browser } from "@/index"; | ||
| import { debugPrint } from "@/modules/output"; | ||
| import { getSettingValueById, onSettingsCached, settingsEmitter } from "@/saving/settings"; | ||
| import { ElectronBlocker } from "@ghostery/adblocker-electron"; | ||
| import { Session } from "electron"; | ||
|
|
||
| type BlockerInstanceType = "all" | "adsAndTrackers" | "adsOnly"; | ||
|
|
||
| /** | ||
| * ContentBlocker class manages ad and tracking content blocking functionality | ||
| */ | ||
| class ContentBlocker { | ||
| private blockerInstancePromise: Promise<ElectronBlocker> | undefined = undefined; | ||
| private blockerInstanceType: BlockerInstanceType | undefined = undefined; | ||
| private blockedSessions: Session[] = []; | ||
|
|
||
| /** | ||
| * Creates or returns existing blocker instance of the specified type | ||
| */ | ||
| private async createBlockerInstance(type: BlockerInstanceType): Promise<ElectronBlocker> { | ||
| if (this.blockerInstancePromise && this.blockerInstanceType === type) { | ||
| return this.blockerInstancePromise; | ||
| } | ||
|
|
||
| if (this.blockerInstancePromise) { | ||
| await this.disableBlocker(); | ||
| } | ||
|
|
||
| debugPrint("CONTENT_BLOCKER", "Creating blocker instance:", type); | ||
| switch (type) { | ||
| case "all": | ||
| this.blockerInstancePromise = ElectronBlocker.fromPrebuiltFull(); | ||
| break; | ||
| case "adsAndTrackers": | ||
| this.blockerInstancePromise = ElectronBlocker.fromPrebuiltAdsAndTracking(); | ||
| break; | ||
| case "adsOnly": | ||
| this.blockerInstancePromise = ElectronBlocker.fromPrebuiltAdsOnly(); | ||
| break; | ||
| } | ||
|
|
||
| this.blockerInstancePromise.then((blocker) => { | ||
| blocker.on("request-blocked", (request) => { | ||
| debugPrint("CONTENT_BLOCKER", "Request blocked:", request.url); | ||
| }); | ||
| }); | ||
|
|
||
| this.blockerInstanceType = type; | ||
| return this.blockerInstancePromise as Promise<ElectronBlocker>; | ||
| } | ||
iamEvanYT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Disables content blocking on all sessions | ||
| */ | ||
| private async disableBlocker(): Promise<void> { | ||
| if (!this.blockerInstancePromise) return; | ||
|
|
||
| const blocker = await this.blockerInstancePromise; | ||
| for (const session of this.blockedSessions) { | ||
| blocker.disableBlockingInSession(session); | ||
| } | ||
|
|
||
| this.blockedSessions = []; | ||
| this.blockerInstancePromise = undefined; | ||
| this.blockerInstanceType = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Enables content blocking for a specific session | ||
| */ | ||
| private async enableBlockerForSession(blockerType: BlockerInstanceType, session: Session): Promise<void> { | ||
| const blocker = await this.createBlockerInstance(blockerType); | ||
| if (!blocker) return; | ||
|
|
||
| // check if session is already blocked | ||
| if (this.blockedSessions.includes(session)) return; | ||
|
|
||
| // add session to blocked sessions | ||
| this.blockedSessions.push(session); | ||
|
|
||
| // enable blocking in session | ||
| blocker.enableBlockingInSession(session); | ||
| } | ||
|
|
||
| /** | ||
| * Updates content blocker configuration based on user settings | ||
| */ | ||
| public async updateConfig(): Promise<void> { | ||
| if (!browser) return; | ||
|
|
||
| const contentBlocker = getSettingValueById("contentBlocker") as string | undefined; | ||
| const profiles = browser.getLoadedProfiles(); | ||
|
|
||
| switch (contentBlocker) { | ||
| case "all": | ||
| case "adsAndTrackers": | ||
| case "adsOnly": | ||
| for (const profile of profiles) { | ||
| this.enableBlockerForSession(contentBlocker as BlockerInstanceType, profile.session); | ||
| } | ||
| break; | ||
| default: | ||
| this.disableBlocker(); | ||
| } | ||
|
|
||
| debugPrint("CONTENT_BLOCKER", "Content blocker configuration updated:", contentBlocker); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes content blocker and sets up event listeners | ||
| */ | ||
| public async initialize(): Promise<void> { | ||
| // Initial configuration | ||
| await this.updateConfig(); | ||
|
|
||
| // Listen for setting changes | ||
| settingsEmitter.on("settings-changed", () => { | ||
| this.updateConfig(); | ||
| }); | ||
|
|
||
| // Listen for profile changes | ||
| browser?.on("profile-loaded", () => { | ||
| this.updateConfig(); | ||
| }); | ||
| } | ||
iamEvanYT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Export singleton instance | ||
| export const contentBlocker = new ContentBlocker(); | ||
|
|
||
| // Initialize content blocker when module is loaded | ||
| onSettingsCached().then(() => { | ||
| debugPrint("CONTENT_BLOCKER", "Initializing content blocker"); | ||
| contentBlocker.initialize(); | ||
| }); | ||
iamEvanYT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.