Skip to content
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

Ext: Disable context menu items if domain is blacklisted #9739

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 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,56 @@ 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) => {
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);
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, I just removed the fragment cause we're in a loop and the key is set on Citation so it was causing a React error.

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
Loading