-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to block URLs navigated by browser history
- add "tabs" permission Note: "history" permission isn't suitable as it is global (per any tab), doesn't have knowledge of tabId - optimize execution time by having prepared __enabled and __blocked (thus not loading every time from storage)
- Loading branch information
Showing
4 changed files
with
85 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,18 +1,56 @@ | ||
import initStorage from "./storage/init"; | ||
import blockSite from "./helpers/block-site"; | ||
import storage from "./storage"; | ||
import createContextMenu from "./helpers/create-context-menu"; | ||
import blockSite from "./helpers/block-site"; | ||
|
||
let __enabled: boolean; | ||
let __blocked: string[]; | ||
|
||
initStorage(() => { | ||
createContextMenu(); | ||
|
||
storage.get(["enabled", "blocked"], ({ enabled, blocked }) => { | ||
__enabled = enabled; | ||
__blocked = blocked; | ||
}); | ||
|
||
initStorage(createContextMenu); | ||
chrome.storage.local.onChanged.addListener((changes) => { | ||
if (changes["enabled"]) { | ||
__enabled = changes["enabled"].newValue as boolean; | ||
} | ||
|
||
if (changes["blocked"]) { | ||
__blocked = changes["blocked"].newValue as string[]; | ||
} | ||
}); | ||
}); | ||
|
||
chrome.action.onClicked.addListener(() => { | ||
chrome.runtime.openOptionsPage(); | ||
}); | ||
|
||
chrome.webNavigation.onBeforeNavigate.addListener((details) => { | ||
const { tabId, url, timeStamp, frameId } = details; | ||
if (!__enabled || !__blocked.length) { | ||
return; | ||
} | ||
|
||
const { tabId, url, frameId } = details; | ||
if (!url || !url.startsWith("http") || frameId !== 0) { | ||
return; | ||
} | ||
|
||
blockSite({ tabId, url, timeStamp }); | ||
blockSite({ blocked: __blocked, tabId, url }); | ||
}); | ||
|
||
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { | ||
if (!tabId || !__enabled || !__blocked.length) { | ||
return; | ||
} | ||
|
||
const { status, url } = changeInfo; | ||
if (status !== "loading" || !url || !url.startsWith("http")) { | ||
return; | ||
} | ||
|
||
blockSite({ blocked: __blocked, tabId, url }); | ||
}); |
This file contains 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 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