Skip to content

Commit

Permalink
feat: 添加功能开关配置
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit eb9a396ee29d2bf2ab9cc55228ad8c716e10e3c9
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 17:38:04 2023 +0800

    添加全部禁用和启用的快捷方式

commit e0866c30d69d9142c06415031d68b1fad657465f
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 15:12:29 2023 +0800

    配置题单页开关

commit b8c01e5dbf3a56b8c6ef61df47c209bd64c43ec7
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 14:48:39 2023 +0800

    配置题库页开关

commit 3c1025f6583f1a287e7d9fbd89d8a812ee2012e2
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 05:19:38 2023 +0800

    修复答题页

commit ddc090d856ca9ad26f16f17c0fa498f7f589b624
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 04:11:27 2023 +0800

    统一处理所有页面

commit cdd50a6b21b2f07b4afd538074f4c8586171ce21
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Mon Jan 30 03:47:06 2023 +0800

    配置竞赛排名页开关

commit b7d4526ab38fb11e39e269fca211946e23edd380
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Sun Jan 29 15:43:23 2023 +0800

    配置答题页开关

commit 93a8eba806a12cae7636b5db354912a05fb15b25
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Sun Jan 29 15:01:46 2023 +0800

    配置首页黑名单开关

commit a5f5c76422345c66e5f458a0a1711d14e4c9807e
Author: XYShaoKang <38753204+XYShaoKang@users.noreply.github.com>
Date:   Sun Jan 29 07:33:21 2023 +0800

    添加 options
  • Loading branch information
XYShaoKang committed Jan 30, 2023
1 parent f2d8551 commit 7e4029f
Show file tree
Hide file tree
Showing 54 changed files with 1,594 additions and 1,184 deletions.
7 changes: 7 additions & 0 deletions config/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const CopyPlugin = require('copy-webpack-plugin')
const common = {
entry: {
popup: path.join(__dirname, '../src/popup/index.tsx'),
options: path.join(__dirname, '../src/options/index.tsx'),
content: path.join(__dirname, '../src/content/index.tsx'),
'content-load': path.join(__dirname, '../src/content/load.ts'),
background: path.join(__dirname, '../src/background/index.ts'),
Expand Down Expand Up @@ -45,6 +46,12 @@ const common = {
],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, '../public', 'options.html'),
chunks: ['options'],
filename: 'options.html',
publicPath: '/',
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, '../public', 'popup.html'),
chunks: ['popup'],
Expand Down
5 changes: 5 additions & 0 deletions config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const development = env => ({
publicPath: 'http://localhost:9100/',
},
entry: {
options: [
'webpack/hot/dev-server.js',
'webpack-dev-server/client/index.js?hot=true&protocol=ws&hostname=localhost&port=9100',
path.join(__dirname, '../src/options/index.tsx'),
],
popup: [
'webpack/hot/dev-server.js',
'webpack-dev-server/client/index.js?hot=true&protocol=ws&hostname=localhost&port=9100',
Expand Down
33 changes: 19 additions & 14 deletions public/manifest-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"content_security_policy": {
"extension_pages": "default-src 'self' http://localhost:9100; script-src 'self' http://localhost:9100; script-src-elem 'self' http://localhost:9100; style-src 'self' 'unsafe-inline'; connect-src 'self' ws://localhost:9100 http://localhost:9100 https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com https://leetcode.cn https://lccn.lbao.site"
},
"permissions": [
"scripting"
],
"web_accessible_resources": [
{
"resources": [
Expand All @@ -30,14 +24,6 @@
]
}
],
"host_permissions": [
"https://leetcode-cn.com/*",
"https://leetcode.cn/*",
"http://localhost:9100/*",
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*",
"https://lccn.lbao.site/*"
],
"content_scripts": [
{
"matches": [
Expand All @@ -52,6 +38,25 @@
"background": {
"service_worker": "background.bundle.js"
},
"options_page": "options.html",
"action": {
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "default-src 'self' http://localhost:9100; script-src 'self' http://localhost:9100; script-src-elem 'self' http://localhost:9100; style-src 'self' 'unsafe-inline'; connect-src 'self' ws://localhost:9100 http://localhost:9100 https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com https://leetcode.cn https://lccn.lbao.site"
},
"permissions": [
"scripting",
"storage"
],
"host_permissions": [
"https://leetcode-cn.com/*",
"https://leetcode.cn/*",
"http://localhost:9100/*",
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*",
"https://lccn.lbao.site/*"
],
"externally_connectable": {
"matches": [
"https://leetcode-cn.com/*",
Expand Down
28 changes: 17 additions & 11 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"48": "icons/icon-48.png",
"128": "icons/icon-128.png"
},
"content_security_policy": {
"extension_pages": "default-src 'self'; script-src 'self'; script-src-elem 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com https://leetcode.cn https://lccn.lbao.site"
},
"permissions": [],
"web_accessible_resources": [
{
"resources": [
Expand All @@ -26,13 +22,6 @@
]
}
],
"host_permissions": [
"https://leetcode-cn.com/*",
"https://leetcode.cn/*",
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*",
"https://lccn.lbao.site/*"
],
"content_scripts": [
{
"matches": [
Expand All @@ -47,6 +36,23 @@
"background": {
"service_worker": "background.bundle.js"
},
"options_page": "options.html",
"action": {
"default_popup": "popup.html"
},
"content_security_policy": {
"extension_pages": "default-src 'self'; script-src 'self'; script-src-elem 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://leetcode-rating-predictor.herokuapp.com https://leetcode-predictor.herokuapp.com https://leetcode-cn.com https://leetcode.cn https://lccn.lbao.site"
},
"permissions": [
"storage"
],
"host_permissions": [
"https://leetcode-cn.com/*",
"https://leetcode.cn/*",
"https://leetcode-rating-predictor.herokuapp.com/*",
"https://leetcode-predictor.herokuapp.com/*",
"https://lccn.lbao.site/*"
],
"externally_connectable": {
"matches": [
"https://leetcode-cn.com/*",
Expand Down
12 changes: 12 additions & 0 deletions public/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Refined LeetCode</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
2 changes: 1 addition & 1 deletion public/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>LeetCode Extension</title>
<title>Refined LeetCode</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
24 changes: 24 additions & 0 deletions src/content/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FC } from 'react'
import { withRoot } from './hoc'

import Ranking from './pages/ranking/App'
import Home from './pages/home/App'
import Timer from './pages/problems/App'
import ShortcutKeyOption from './pages/problems/ShortcutKeyOption'
import Problemset from './pages/problemset/App'
import ProblemList from './pages/problem-list/App'

const App: FC = () => {
return (
<>
<Ranking />
<Home />
<Timer />
<ShortcutKeyOption />
<Problemset />
<ProblemList />
</>
)
}

export default withRoot(App)
9 changes: 5 additions & 4 deletions src/content/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { apiSlice } from '@/pages/ranking/rankSlice'
import postsReducer from '@/pages/home/postsSlice'
import blockUsersReducer from '@/pages/home/blockUsersSlice'
import globalDataReducer, { fetchGlobalData } from '@/pages/global/globalSlice'
import optionReducer from '@/pages/global/optionSlice'
import optionsReducer from '@/pages/global/optionsSlice'
import favoritesReducer from '@/pages/problem-list/favoriteSlice'
import questionsReducer from '@/pages/problemset/questionsSlice'

Expand All @@ -36,6 +36,7 @@ const config = {
'blockUsers/unSetBlockUser',
'blockUsers/toggleBlockUser',
'global/fetchGlobalData/fulfilled',
'options/toggleContestProblemShortcutKeyOption',
],
}

Expand Down Expand Up @@ -82,12 +83,12 @@ const persistedQuestionsReducer = persistReducer(
},
questionsReducer
)
const persistedOptionReducer = persistReducer(
const persistedOptionsReducer = persistReducer(
{
key: 'refined-leetcode-option',
storage: localforage,
},
optionReducer
optionsReducer
)

const store = configureStore({
Expand All @@ -96,7 +97,7 @@ const store = configureStore({
posts: postsReducer,
blockUsers: persistedUsersReducer,
global: globalDataReducer,
option: persistedOptionReducer,
options: persistedOptionsReducer,
favorites: favoritesReducer,
questions: persistedQuestionsReducer,
},
Expand Down
62 changes: 62 additions & 0 deletions src/content/components/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ChangeEventHandler, FC } from 'react'
import { css } from 'styled-components/macro'
import {
CheckBoxCheckedIcon,
CheckBoxIndeterminateIcon,
CheckBoxUncheckedIcon,
} from './icons'

interface CheckboxProps {
checked?: boolean
indeterminate?: boolean
onChange?: ChangeEventHandler<HTMLInputElement>
size?: number
color?: string
}

const Checkbox: FC<CheckboxProps> = ({
checked,
indeterminate,
onChange,
size,
color,
}) => {
const Icon = indeterminate
? CheckBoxIndeterminateIcon
: checked
? CheckBoxCheckedIcon
: CheckBoxUncheckedIcon
const chandleChange: ChangeEventHandler<HTMLInputElement> = e => {
onChange?.(e)
}
return (
<span
css={css`
cursor: pointer;
position: relative;
display: flex;
align-items: center;
`}
>
<input
type="checkbox"
checked={checked}
onChange={chandleChange}
css={css`
opacity: 0;
position: absolute;
margin: 0;
padding: 0;
left: 0;
top: 0;
height: 100%;
width: 100%;
cursor: pointer;
`}
/>
<Icon width={size} height={size} color={color} />
</span>
)
}

export default Checkbox
46 changes: 46 additions & 0 deletions src/content/components/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useRef, useState } from 'react'

interface SwitchProps {
enable?: boolean
height?: number
width?: number
onToggle?: () => void
}

const Switch: React.FC<SwitchProps> = ({
enable: enableProp,
height,
width,
onToggle,
}) => {
const { current: isControlled } = useRef(enableProp !== undefined)
const [state, setState] = useState<boolean>()
const handleToggle = () => {
setState(!state)
if (onToggle) onToggle()
}
if (height === undefined && width === undefined) height = 24
const enable = isControlled ? enableProp : state
return (
<svg
viewBox="0 0 24 24"
style={{ width, height, cursor: 'pointer' }}
onClick={handleToggle}
>
{enable ? (
<path
d="M17 7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h10c2.76 0 5-2.24 5-5s-2.24-5-5-5zm0 8c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3z"
fill="currentColor"
color="rgb(144, 202, 249)"
/>
) : (
<path
d="M17 7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h10c2.76 0 5-2.24 5-5s-2.24-5-5-5zM7 15c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3z"
fill="currentColor"
/>
)}
</svg>
)
}

export default Switch
25 changes: 25 additions & 0 deletions src/content/components/icons/CheckBoxIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SvgIcon, { SvgIconProps } from '../SvgIcon'

export const CheckBoxCheckedIcon: React.FC<SvgIconProps> = props => {
return (
<SvgIcon {...props}>
<path d="M19 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.11 0 2-.9 2-2V5c0-1.1-.89-2-2-2zm-9 14-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z" />
</SvgIcon>
)
}

export const CheckBoxUncheckedIcon: React.FC<SvgIconProps> = props => {
return (
<SvgIcon {...props}>
<path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z" />
</SvgIcon>
)
}

export const CheckBoxIndeterminateIcon: React.FC<SvgIconProps> = props => {
return (
<SvgIcon {...props}>
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-2 10H7v-2h10v2z" />
</SvgIcon>
)
}
1 change: 1 addition & 0 deletions src/content/components/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as EditIcon } from './EditIcon'
export { default as RemoveIcon } from './RemoveIcon'
export { default as PublicIcon } from './PublicIcon'
export { default as PrivateIcon } from './PrivateIcon'
export * from './CheckBoxIcon'
1 change: 1 addition & 0 deletions src/content/hoc/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './withRoot'
export * from './withPage'
17 changes: 17 additions & 0 deletions src/content/hoc/withPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ComponentType, forwardRef } from 'react'

import { PageName } from 'src/options/options'
import { useAppSelector } from '@/hooks'
import { selectCurrentPage } from '@/pages/global/globalSlice'

export const withPage =
(pageName: PageName) =>
<T extends ComponentType<any>>(Component: T): T => {
const App = forwardRef(function App(props: any, ref: any) {
const currentPage = useAppSelector(selectCurrentPage)
if (currentPage !== pageName) return null
return <Component ref={ref} {...props} />
})

return App as any
}
2 changes: 1 addition & 1 deletion src/content/hoc/withRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getTheme, isBetaUI } from '@/utils'
const Loading = () => <></>

export const withRoot = <T extends ComponentType<any>>(Component: T): T => {
const Root = forwardRef(function App(props: any, ref: any) {
const Root = forwardRef(function Root(props: any, ref: any) {
const [theme, setTheme] = useState(getTheme())
useEffect(() => {
// 跟随力扣的明暗主题进行切换
Expand Down
Loading

0 comments on commit 7e4029f

Please sign in to comment.