Skip to content

Commit

Permalink
Ext: Disable context menu items if domain is blacklisted
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph committed Jan 3, 2025
1 parent 9fc6c81 commit 5cc4043
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
54 changes: 53 additions & 1 deletion extension/app/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PendingUpdate } from "@extension/lib/storage";
import { savePendingUpdate } from "@extension/lib/storage";
import { getStoredUser, savePendingUpdate } from "@extension/lib/storage";

import {
AUTH0_CLIENT_DOMAIN,
Expand Down Expand Up @@ -73,6 +73,58 @@ chrome.runtime.onInstalled.addListener(() => {
});
});

/**
* Util & listeners to disable context menu items based on the domain.
*/
const shouldDisableContextMenuForDomain = async (
url: string
): Promise<boolean> => {
if (url.startsWith("chrome://")) {
return true;
}

const user = await getStoredUser();
if (!user || !user.selectedWorkspace) {
return false;
}

const blacklistedDomains =
user.workspaces.find((w) => w.sId === user.selectedWorkspace)
?.blacklistedDomains || [];

return blacklistedDomains.some((d) => url.includes(d));
};

const toggleContextMenus = (isDisabled: boolean) => {
[
"ask_dust",
"add_tab_content",
"add_tab_screenshot",
"add_selection",
].forEach((menuId) => {
chrome.contextMenus.update(menuId, { enabled: !isDisabled });
});
};

// Add URL change listener to update context menu state.
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
console.log(tab.url);
if (changeInfo.status === "complete" && tab.url) {
const isDisabled = await shouldDisableContextMenuForDomain(tab.url);
toggleContextMenus(isDisabled);
}
});

// Also add URL change listener for active tab changes.
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId);
console.log(tab.url);
if (tab.url) {
const isDisabled = await shouldDisableContextMenuForDomain(tab.url);
toggleContextMenus(isDisabled);
}
});

chrome.runtime.onConnect.addListener((port) => {
if (port.name === "sidepanel-connection") {
console.log("Sidepanel is there");
Expand Down
44 changes: 21 additions & 23 deletions extension/app/src/components/input_bar/InputBarCitations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,29 @@ export function InputBarCitations({
const isImage = Boolean(blob.preview);

nodes.push(
<>
<Citation
disabled={disabled}
key={`cf-${blob.id}`}
className="w-48"
isLoading={blob.isUploading}
>
{isImage ? (
<>
<CitationImage imgSrc={blob.preview ?? ""} />
<CitationIcons>
<Icon visual={ImageIcon} />
</CitationIcons>
</>
) : (
<Citation
disabled={disabled}
key={`cf-${blob.id}`}
className="w-48"
isLoading={blob.isUploading}
>
{isImage ? (
<>
<CitationImage imgSrc={blob.preview ?? ""} />
<CitationIcons>
<Icon visual={DocumentIcon} />
<Icon visual={ImageIcon} />
</CitationIcons>
)}
<CitationTitle>{blob.id}</CitationTitle>
<CitationClose
onClick={() => fileUploaderService.removeFile(blob.id)}
/>
</Citation>
</>
</>
) : (
<CitationIcons>
<Icon visual={DocumentIcon} />
</CitationIcons>
)}
<CitationTitle>{blob.id}</CitationTitle>
<CitationClose
onClick={() => fileUploaderService.removeFile(blob.id)}
/>
</Citation>
);
}

Expand Down

0 comments on commit 5cc4043

Please sign in to comment.