Skip to content

Commit

Permalink
Merge pull request #3370 from amitsingh-007/f-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsingh-007 authored Dec 25, 2024
2 parents 28c174b + 7e9ad16 commit 9290f6f
Show file tree
Hide file tree
Showing 97 changed files with 1,009 additions and 1,234 deletions.
3 changes: 2 additions & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
},
"devDependencies": {
"@playwright/test": "1.49.1",
"@prefresh/core": "1.5.3",
"@prefresh/webpack": "4.0.1",
"@types/archiver": "6.0.3",
"@types/chrome": "0.0.287",
"@types/firefox-webext-browser": "120.0.4",
Expand Down Expand Up @@ -68,7 +70,6 @@
"postcss-preset-mantine": "1.17.0",
"postcss-simple-vars": "7.0.1",
"react-refresh": "0.16.0",
"react-refresh-typescript": "2.0.9",
"terser-webpack-plugin": "5.3.11",
"style-loader": "4.0.0",
"ts-loader": "9.5.1",
Expand Down
27 changes: 0 additions & 27 deletions apps/extension/src/BackgroundScript/bypass/bypassUtils.ts

This file was deleted.

20 changes: 0 additions & 20 deletions apps/extension/src/BackgroundScript/bypass/index.ts

This file was deleted.

27 changes: 2 additions & 25 deletions apps/extension/src/BackgroundScript/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { EExtensionState } from '@/constants';
import Logging from '@/logging';
import { getIsExtensionActive, setExtStateInStorage } from '@/utils/common';
import { getExtensionState } from '@helpers/fetchFromStorage';
import { bypass } from './bypass';
import turnOffInputSuggestions from './misc/turnOffInputSuggestions';
import { redirect } from './redirect';
import {
checkForUpdates,
isValidTabUrl,
isValidUrl,
setExtensionIcon,
} from './utils';
import { redirect } from './redirections';
import { isValidTabUrl, isValidUrl, setExtensionIcon } from './utils';
import { receiveRuntimeMessage } from './utils/receiveRuntimeMessage';
import { RuntimeInput } from '@/utils/sendRuntimeMessage';
import hearbeatFirefoxBackgroundPage from './utils/keepAliveSW';
Expand All @@ -19,10 +12,6 @@ if (!IS_CHROME) {
hearbeatFirefoxBackgroundPage();
}

Logging.init();

const red = '#FF6B6B';

// First time extension install
chrome.runtime.onInstalled.addListener(() => {
setExtStateInStorage(EExtensionState.ACTIVE);
Expand All @@ -39,15 +28,6 @@ chrome.runtime.onStartup.addListener(() => {
hasPendingPersons,
});
});
checkForUpdates().then((isUsingLatest) => {
if (!isUsingLatest) {
chrome.action.setBadgeText({ text: '!' });
chrome.action.setBadgeBackgroundColor({ color: red });
chrome.action.setTitle({
title: 'You are using older version of Bypass Links',
});
}
});
});

const onPageLoad = async (tabId: number, url: string) => {
Expand All @@ -66,9 +46,6 @@ const onPageLoad = async (tabId: number, url: string) => {
if (await isValidTabUrl(tabId)) {
turnOffInputSuggestions(tabId);
}
if (await isValidTabUrl(tabId)) {
bypass(tabId, new URL(url));
}
};

// Listen tab url change
Expand Down
49 changes: 38 additions & 11 deletions apps/extension/src/BackgroundScript/misc/forumPageLinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getWebistes } from '@/helpers/fetchFromStorage';
import scripting from '@/utils/scripting';

const getForumPageLinksFunc = () => {
const getForum_1_2_LinksFunc = () => {
const unreadRows = document.querySelectorAll(
'.block-row.block-row--separated:not(.block-row--alt).is-unread'
);
Expand All @@ -10,7 +11,7 @@ const getForumPageLinksFunc = () => {
);
};

const getForumWatchedThreadsLinksFunc = () => {
const getForum_1_2_WatchedThreadsLinksFunc = () => {
const unreadRows = document.querySelectorAll(
'.structItemContainer > .structItem.is-unread > .structItem-cell--main'
);
Expand All @@ -28,20 +29,46 @@ const getForumWatchedThreadsLinksFunc = () => {
});
};

const getForum_3_LinksFunc = () => {
const recentPostsNode = [
...document.querySelectorAll<HTMLUListElement>('.recent-posts'),
].at(-1);
const recentPostLinks =
recentPostsNode?.querySelectorAll<HTMLAnchorElement>('.post-thumb > a');
return [...(recentPostLinks || [])].map((link) => link.href);
};

export const getForumPageLinks = async (
tabId?: number,
url?: string
tabId: number,
url: string
): Promise<string[]> => {
if (!tabId || !url) {
throw new Error('No tabId/url found in getForumPageLinks()');
const websites = await getWebistes();
let executor: () => (string | undefined)[];

switch (true) {
case url.includes(websites.FORUM_1):
case url.includes(websites.FORUM_2): {
const { pathname } = new URL(url);
const isWatchThreadsPage = pathname === '/watched/threads';
executor = isWatchThreadsPage
? getForum_1_2_WatchedThreadsLinksFunc
: getForum_1_2_LinksFunc;
break;
}

case url.includes(websites.FORUM_3): {
executor = getForum_3_LinksFunc;
break;
}

default: {
throw new Error('Not a forum page');
}
}
const { pathname } = new URL(url);
const isWatchThreadsPage = pathname === '/watched/threads';

const [{ result }] = await scripting.executeScript({
target: { tabId },
func: isWatchThreadsPage
? getForumWatchedThreadsLinksFunc
: getForumPageLinksFunc,
func: executor,
});
return result?.filter(Boolean) ?? [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { trpcApi } from '@/apis/trpcApi';
import { startHistoryWatch } from '@/utils/history';
import { STORAGE_KEYS } from '@bypass/shared';
import { getMappedRedirections } from '@helpers/fetchFromStorage';
import { mapRedirections } from '../mapper/redirection';
import { mapRedirections } from './mapper';

export const redirect = async (tabId: number, url: URL) => {
// Firefox sometimes changes protocol to https
Expand Down
21 changes: 6 additions & 15 deletions apps/extension/src/BackgroundScript/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EExtensionState } from '@/constants';
import { getExtensionState } from '@helpers/fetchFromStorage';
import { trpcApi } from '../../apis/trpcApi';
import { getIsExtensionActive } from '../../utils/common';

const restrictedProtocols = new Set([
Expand Down Expand Up @@ -28,14 +27,6 @@ const restrictedHosts = new Set([
'addons.mozilla.org', // Firefox addon store
]);

export const isValidUrl = (_url?: string): boolean => {
if (!_url) return false;
const url = new URL(_url);
return (
!restrictedHosts.has(url.hostname) && !restrictedProtocols.has(url.protocol)
);
};

export const setExtensionIcon = async ({
extState,
hasPendingBookmarks,
Expand All @@ -57,12 +48,12 @@ export const setExtensionIcon = async ({
await chrome.action.setIcon({ path: icon });
};

export const checkForUpdates = async () => {
const { chrome: chromeData, firefox } =
await trpcApi.extension.latest.query();
const latestVersion = IS_CHROME ? chromeData.version : firefox.version;
const { version: currentVersion } = chrome.runtime.getManifest();
return latestVersion === currentVersion;
export const isValidUrl = (_url?: string): boolean => {
if (!_url) return false;
const url = new URL(_url);
return (
!restrictedHosts.has(url.hostname) && !restrictedProtocols.has(url.protocol)
);
};

export const isValidTabUrl = async (tabId: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const receiveRuntimeMessage = (
sendMessage: <T extends RuntimeKeys>(data: RuntimeOutput[T]) => void
) => {
switch (message.key) {
case 'forumPageLinks': {
case 'openWebsiteLinks': {
getForumPageLinks(message.tabId, message.url).then((forumPageLinks) => {
sendMessage<'forumPageLinks'>({ forumPageLinks });
sendMessage<'openWebsiteLinks'>({ forumPageLinks });
});
break;
}
Expand Down
6 changes: 6 additions & 0 deletions apps/extension/src/BackgroundScript/websites/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getWebistes } from '@/helpers/fetchFromStorage';

export const isForumPage = async (hostname: string) => {
const websites = await getWebistes();
return Object.values(websites).some((website) => hostname.includes(website));
};
21 changes: 21 additions & 0 deletions apps/extension/src/BackgroundScript/websites/storageSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { trpcApi } from '@/apis/trpcApi';
import { IWebsites, STORAGE_KEYS } from '@bypass/shared';

const getDecodedWebsites = (encodedWebsites: IWebsites) => {
return Object.entries(encodedWebsites).reduce((acc, [key, value]) => {
return {
...acc,
[key]: decodeURIComponent(atob(value)),
};
}, {});
};

export const syncWebsitesToStorage = async () => {
const response = await trpcApi.firebaseData.websitesGet.query();
const websitesData = getDecodedWebsites(response);
await chrome.storage.local.set({ [STORAGE_KEYS.websites]: websitesData });
};

export const resetWebsites = async () => {
await chrome.storage.local.remove(STORAGE_KEYS.websites);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@bypass/shared';
import { Button, Modal, Select, Stack, TextInput } from '@mantine/core';
import { useForm } from '@mantine/form';
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation } from 'wouter';
import { useShallow } from 'zustand/react/shallow';
import useBookmarkStore from '../store/useBookmarkStore';
Expand Down Expand Up @@ -37,7 +37,7 @@ interface IForm {

const validateHandler = (value: string) => (value?.trim() ? null : 'Required');

const BookmarkAddEditDialog = memo<Props>(({ curFolder, handleScroll }) => {
const BookmarkAddEditDialog = ({ curFolder, handleScroll }: Props) => {
const [, navigate] = useLocation();
const { bookmarkOperation, resetBookmarkOperation } = useBookmarkRouteStore(
useShallow((state) => ({
Expand Down Expand Up @@ -217,7 +217,6 @@ const BookmarkAddEditDialog = memo<Props>(({ curFolder, handleScroll }) => {
</form>
</Modal>
);
});
BookmarkAddEditDialog.displayName = 'BookmarkAddEditDialog';
};

export default BookmarkAddEditDialog;
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,5 @@ const BookmarkContextMenu = memo<Props>(
);
}
);
BookmarkContextMenu.displayName = 'BookmarkContextMenu';

export default BookmarkContextMenu;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ const BookmarkRow = memo<Omit<BookmarkProps, 'onOpenLink'>>((props) => {

return <Bookmark {...props} onOpenLink={onOpenLink} />;
});
BookmarkRow.displayName = 'BookmarkRow';

export default BookmarkRow;
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,5 @@ const BookmarksHeader = memo<Props>(({ onSearchChange, folderContext }) => {
</>
);
});
BookmarksHeader.displayName = 'BookmarksHeader';

export default BookmarksHeader;
Loading

1 comment on commit 9290f6f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Deploy preview for bypass-links ready!

✅ Preview
https://bypass-links-cmk6nehpb-amit-singhs-projects-c621efdb.vercel.app

Built with commit 9290f6f.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.