Skip to content

Commit

Permalink
chore(webext): enhance the behavior of the add channel context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Nov 12, 2022
1 parent afc68dd commit ecece51
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion public/listener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(function () {
const channelUrlById = {};

function getChannelId(url) {
if (url.includes('youtube.com/watch?v=') || url.includes('youtu.be/')) {
return ytInitialPlayerResponse.microformat.playerMicroformatRenderer
Expand All @@ -15,7 +17,15 @@

function sendChannelId(data, delay = 0) {
setTimeout(() => {
const channelId = getChannelId(window.location.href);
const url = window.location.href;
let channelId = getChannelId(url);
if (channelId) {
if (!channelUrlById[channelId]) {
channelUrlById[channelId] = url;
} else if (url !== channelUrlById[channelId]) {
channelId = null;
}
}
window.postMessage({
from: 'page',
response: {
Expand Down
8 changes: 7 additions & 1 deletion src/ui/components/webext/Background/ContextMenus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ export default function ContextMenus(props: ContextMenusProps) {
const channels = useAppSelector(selectChannels);
const channelsRef = useStateRef(channels);
const channelIdsRef = useRef<{ [key: string]: string }>({}); // tab id: channel id
const portsRef = useRef<{ [key: string]: any }>({});
const portsRef = useRef<{ [key: string]: any }>({}); // tab id: port

const handleConnect = (port: any) => {
portsRef.current[port.sender.tab.id] = port;
port.onMessage.addListener((message: any) => {
// const { menuItemId, checked } = message.request;
const { channelId } = message.response;
const options: ContextMenuUpdateOptions = { enabled: false };
if (channelId) {
channelIdsRef.current[port.sender.tab.id] = channelId;
const found = channelsRef.current.find(
(channel) => channel.id === channelId,
);
options.enabled = !found;
}
browser.contextMenus.update('add_channel', options);
});
};

Expand Down

0 comments on commit ecece51

Please sign in to comment.