-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension: Disable share buttons when domain is blackisted
- Loading branch information
Showing
3 changed files
with
100 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useCurrentDomain = () => { | ||
const [currentDomain, setCurrentDomain] = useState<string>(""); | ||
|
||
useEffect(() => { | ||
// Function to update domain from tab. | ||
const updateDomainFromTab = (tab: chrome.tabs.Tab) => { | ||
if (tab?.url) { | ||
try { | ||
const url = new URL(tab.url); | ||
if (url.protocol.startsWith("http")) { | ||
setCurrentDomain(url.hostname); | ||
} | ||
} catch (e) { | ||
console.error("Invalid URL:", e); | ||
setCurrentDomain(""); | ||
} | ||
} | ||
}; | ||
|
||
// Update domain when active tab changes. | ||
const handleTabActivated = (activeInfo: chrome.tabs.TabActiveInfo) => { | ||
chrome.tabs.get(activeInfo.tabId, (tab) => { | ||
updateDomainFromTab(tab); | ||
}); | ||
}; | ||
|
||
// Update domain when tab URL changes. | ||
const handleTabUpdated = ( | ||
tabId: number, | ||
changeInfo: chrome.tabs.TabChangeInfo, | ||
tab: chrome.tabs.Tab | ||
) => { | ||
if (changeInfo.status === "complete") { | ||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { | ||
if (tabs[0]?.id === tabId) { | ||
updateDomainFromTab(tab); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
// Get initial domain. | ||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { | ||
if (tabs[0]) { | ||
updateDomainFromTab(tabs[0]); | ||
} | ||
}); | ||
|
||
// Add listeners. | ||
chrome.tabs.onActivated.addListener(handleTabActivated); | ||
chrome.tabs.onUpdated.addListener(handleTabUpdated); | ||
|
||
// Cleanup listeners. | ||
return () => { | ||
chrome.tabs.onActivated.removeListener(handleTabActivated); | ||
chrome.tabs.onUpdated.removeListener(handleTabUpdated); | ||
}; | ||
}, []); | ||
|
||
return currentDomain; | ||
}; |