Compare commits

...

3 Commits

Author SHA1 Message Date
b044a304e5 chore 2023-03-22 16:01:07 +08:00
fec81b11a3 feat: decoupling GM_Cookie utility class 2023-03-22 15:45:04 +08:00
b9a667a61d chore 2023-03-22 15:18:02 +08:00
7 changed files with 89 additions and 81 deletions

View File

@ -8,6 +8,7 @@
"wesbos/typescript" "wesbos/typescript"
], ],
"ignorePatterns": [ "ignorePatterns": [
"vite.config.ts" "vite.config.ts",
"dist"
] ]
} }

View File

@ -15,7 +15,7 @@
"@emotion/react": "^11.10.6", "@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6", "@emotion/styled": "^11.10.6",
"@mui/icons-material": "^5.11.11", "@mui/icons-material": "^5.11.11",
"@mui/material": "^5.11.13", "@mui/material": "^5.11.14",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
}, },

44
pnpm-lock.yaml generated
View File

@ -5,7 +5,7 @@ specifiers:
'@emotion/react': ^11.10.6 '@emotion/react': ^11.10.6
'@emotion/styled': ^11.10.6 '@emotion/styled': ^11.10.6
'@mui/icons-material': ^5.11.11 '@mui/icons-material': ^5.11.11
'@mui/material': ^5.11.13 '@mui/material': ^5.11.14
'@types/react': ^18.0.28 '@types/react': ^18.0.28
'@types/react-dom': ^18.0.11 '@types/react-dom': ^18.0.11
'@types/tampermonkey': ^4.0.10 '@types/tampermonkey': ^4.0.10
@ -21,8 +21,8 @@ dependencies:
'@emotion/cache': 11.10.5 '@emotion/cache': 11.10.5
'@emotion/react': 11.10.6_pmekkgnqduwlme35zpnqhenc34 '@emotion/react': 11.10.6_pmekkgnqduwlme35zpnqhenc34
'@emotion/styled': 11.10.6_oouaibmszuch5k64ms7uxp2aia '@emotion/styled': 11.10.6_oouaibmszuch5k64ms7uxp2aia
'@mui/icons-material': 5.11.11_4lyzeezzeeal3x6jtb4ni26w7u '@mui/icons-material': 5.11.11_apxknyxvl46cjjhrrp53pe5pxy
'@mui/material': 5.11.13_xqeqsl5kvjjtyxwyi3jhw3yuli '@mui/material': 5.11.14_xqeqsl5kvjjtyxwyi3jhw3yuli
react: 18.2.0 react: 18.2.0
react-dom: 18.2.0_react@18.2.0 react-dom: 18.2.0_react@18.2.0
@ -586,8 +586,8 @@ packages:
'@jridgewell/sourcemap-codec': 1.4.14 '@jridgewell/sourcemap-codec': 1.4.14
dev: true dev: true
/@mui/base/5.0.0-alpha.121_zula6vjvt3wdocc4mwcxqa6nzi: /@mui/base/5.0.0-alpha.122_zula6vjvt3wdocc4mwcxqa6nzi:
resolution: {integrity: sha512-8nJRY76UqlJV+q/Yzo0tgGfPWEOa+4N9rjO81fMmcJqP0I6m54hLDXsjvMg4tvelY5eKHXUK6Tb7en+GHfTqZA==} resolution: {integrity: sha512-IgZEFQyHa39J1+Q3tekVdhPuUm1fr3icddaNLmiAIeYTVXmR7KR5FhBAIL0P+4shlPq0liUPGlXryoTm0iCeFg==}
engines: {node: '>=12.0.0'} engines: {node: '>=12.0.0'}
peerDependencies: peerDependencies:
'@types/react': ^17.0.0 || ^18.0.0 '@types/react': ^17.0.0 || ^18.0.0
@ -610,11 +610,11 @@ packages:
react-is: 18.2.0 react-is: 18.2.0
dev: false dev: false
/@mui/core-downloads-tracker/5.11.13: /@mui/core-downloads-tracker/5.11.14:
resolution: {integrity: sha512-lx+GXBR9h/ApZsEP728tl0pyZyuajto+VnBgsoAzw1d5+CbmOo8ZWieKwVUGxZlPT1wMYNUYS5NtKzCli0xYjw==} resolution: {integrity: sha512-rfc08z6+3Fif+Gopx2/qmk+MEQlwYeA+gOcSK048BHkTty/ol/boHuVeL2BNC/cf9OVRjJLYHtVb/DeW791LSQ==}
dev: false dev: false
/@mui/icons-material/5.11.11_4lyzeezzeeal3x6jtb4ni26w7u: /@mui/icons-material/5.11.11_apxknyxvl46cjjhrrp53pe5pxy:
resolution: {integrity: sha512-Eell3ADmQVE8HOpt/LZ3zIma8JSvPh3XgnhwZLT0k5HRqZcd6F/QDHc7xsWtgz09t+UEFvOYJXjtrwKmLdwwpw==} resolution: {integrity: sha512-Eell3ADmQVE8HOpt/LZ3zIma8JSvPh3XgnhwZLT0k5HRqZcd6F/QDHc7xsWtgz09t+UEFvOYJXjtrwKmLdwwpw==}
engines: {node: '>=12.0.0'} engines: {node: '>=12.0.0'}
peerDependencies: peerDependencies:
@ -626,13 +626,13 @@ packages:
optional: true optional: true
dependencies: dependencies:
'@babel/runtime': 7.21.0 '@babel/runtime': 7.21.0
'@mui/material': 5.11.13_xqeqsl5kvjjtyxwyi3jhw3yuli '@mui/material': 5.11.14_xqeqsl5kvjjtyxwyi3jhw3yuli
'@types/react': 18.0.28 '@types/react': 18.0.28
react: 18.2.0 react: 18.2.0
dev: false dev: false
/@mui/material/5.11.13_xqeqsl5kvjjtyxwyi3jhw3yuli: /@mui/material/5.11.14_xqeqsl5kvjjtyxwyi3jhw3yuli:
resolution: {integrity: sha512-2CnSj43F+159LbGmTLLQs5xbGYMiYlpTByQhP7c7cMX6opbScctBFE1PuyElpAmwW8Ag9ysfZH1d1MFAmJQkjg==} resolution: {integrity: sha512-uoiUyybmo+M+nYARBygmbXgX6s/hH0NKD56LCAv9XvmdGVoXhEGjOvxI5/Bng6FS3NNybnA8V+rgZW1Z/9OJtA==}
engines: {node: '>=12.0.0'} engines: {node: '>=12.0.0'}
peerDependencies: peerDependencies:
'@emotion/react': ^11.5.0 '@emotion/react': ^11.5.0
@ -651,9 +651,9 @@ packages:
'@babel/runtime': 7.21.0 '@babel/runtime': 7.21.0
'@emotion/react': 11.10.6_pmekkgnqduwlme35zpnqhenc34 '@emotion/react': 11.10.6_pmekkgnqduwlme35zpnqhenc34
'@emotion/styled': 11.10.6_oouaibmszuch5k64ms7uxp2aia '@emotion/styled': 11.10.6_oouaibmszuch5k64ms7uxp2aia
'@mui/base': 5.0.0-alpha.121_zula6vjvt3wdocc4mwcxqa6nzi '@mui/base': 5.0.0-alpha.122_zula6vjvt3wdocc4mwcxqa6nzi
'@mui/core-downloads-tracker': 5.11.13 '@mui/core-downloads-tracker': 5.11.14
'@mui/system': 5.11.13_d2lgyfpecxdc2bsiwyag5wf7ti '@mui/system': 5.11.14_d2lgyfpecxdc2bsiwyag5wf7ti
'@mui/types': 7.2.3_@types+react@18.0.28 '@mui/types': 7.2.3_@types+react@18.0.28
'@mui/utils': 5.11.13_react@18.2.0 '@mui/utils': 5.11.13_react@18.2.0
'@types/react': 18.0.28 '@types/react': 18.0.28
@ -706,8 +706,8 @@ packages:
react: 18.2.0 react: 18.2.0
dev: false dev: false
/@mui/system/5.11.13_d2lgyfpecxdc2bsiwyag5wf7ti: /@mui/system/5.11.14_d2lgyfpecxdc2bsiwyag5wf7ti:
resolution: {integrity: sha512-OWP0Alp6C8ufnGm9+CZcl3d+OoRXL2PnrRT5ohaMLxvGL9OfNcL2t4JOjMmA0k1UAGd6E/Ygbu5lEPrZSDlvCg==} resolution: {integrity: sha512-/MBv5dUoijJNEKEGi5tppIszGN0o2uejmeISi5vl0CLcaQsI1cd+uBgK+JYUP1VWvI/MtkWRLVSWtF2FWhu5Nw==}
engines: {node: '>=12.0.0'} engines: {node: '>=12.0.0'}
peerDependencies: peerDependencies:
'@emotion/react': ^11.5.0 '@emotion/react': ^11.5.0
@ -1157,7 +1157,7 @@ packages:
hasBin: true hasBin: true
dependencies: dependencies:
caniuse-lite: 1.0.30001469 caniuse-lite: 1.0.30001469
electron-to-chromium: 1.4.334 electron-to-chromium: 1.4.335
node-releases: 2.0.10 node-releases: 2.0.10
update-browserslist-db: 1.0.10_browserslist@4.21.5 update-browserslist-db: 1.0.10_browserslist@4.21.5
dev: true dev: true
@ -1394,8 +1394,8 @@ packages:
domhandler: 5.0.3 domhandler: 5.0.3
dev: true dev: true
/electron-to-chromium/1.4.334: /electron-to-chromium/1.4.335:
resolution: {integrity: sha512-laZ1odk+TRen6q0GeyQx/JEkpD3iSZT7ewopCpKqg9bTjP1l8XRfU3Bg20CFjNPZkp5+NDBl3iqd4o/kPO+Vew==} resolution: {integrity: sha512-l/eowQqTnrq3gu+WSrdfkhfNHnPgYqlKAwxz7MTOj6mom19vpEDHNXl6dxDxyTiYuhemydprKr/HCrHfgk+OfQ==}
dev: true dev: true
/emoji-regex/9.2.2: /emoji-regex/9.2.2:
@ -2017,7 +2017,7 @@ packages:
imurmurhash: 0.1.4 imurmurhash: 0.1.4
is-glob: 4.0.3 is-glob: 4.0.3
is-path-inside: 3.0.3 is-path-inside: 3.0.3
js-sdsl: 4.3.0 js-sdsl: 4.4.0
js-yaml: 4.1.0 js-yaml: 4.1.0
json-stable-stringify-without-jsonify: 1.0.1 json-stable-stringify-without-jsonify: 1.0.1
levn: 0.4.1 levn: 0.4.1
@ -2527,8 +2527,8 @@ packages:
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==} resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
dev: true dev: true
/js-sdsl/4.3.0: /js-sdsl/4.4.0:
resolution: {integrity: sha512-mifzlm2+5nZ+lEcLJMoBK0/IH/bDg8XnJfd/Wq6IP+xoCjLZsTOnV2QpxlVbX9bMnkl5PdEjNtBJ9Cj1NjifhQ==} resolution: {integrity: sha512-FfVSdx6pJ41Oa+CF7RDaFmTnCaFhua+SNYQX74riGOpl96x+2jQCqEfQ2bnXu/5DPCqlRuiqyvTJM0Qjz26IVg==}
dev: true dev: true
/js-tokens/4.0.0: /js-tokens/4.0.0:

View File

@ -12,15 +12,20 @@ import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem'; import MenuItem from '@mui/material/MenuItem';
import Snackbar from '@mui/material/Snackbar'; import Snackbar from '@mui/material/Snackbar';
import { useState } from 'react'; import { useState } from 'react';
import useToast from './hooks/useToast'; import useToast from './hooks/use-toast';
import {
deleteCookie as gmDeleteCookie,
getCookie as gmGetCookie,
setCookie as gmSetCookie,
} from './utils/gm-cookie';
const Z_INDEX_MAX = 2 ** 31 - 1; const MAX_Z_INDEX = 2 ** 31 - 1;
const CustomButton = styled(IconButton)` const ToolButton = styled(IconButton)`
position: fixed; position: fixed;
right: 20px; right: 20px;
bottom: 20px; bottom: 20px;
z-index: ${Z_INDEX_MAX - 1}; z-index: ${MAX_Z_INDEX};
opacity: 0.75; opacity: 0.75;
transition: all 225ms linear; transition: all 225ms linear;
background-color: #c2e0ff; background-color: #c2e0ff;
@ -30,8 +35,8 @@ const CustomButton = styled(IconButton)`
} }
`; `;
const CustomMenu = styled(Menu)` const ToolMenu = styled(Menu)`
z-index: ${Z_INDEX_MAX}; z-index: ${MAX_Z_INDEX};
`; `;
const App = () => { const App = () => {
@ -57,46 +62,9 @@ const App = () => {
GM_setClipboard(text); GM_setClipboard(text);
}; };
const getCookie = () =>
new Promise<TampermonkeyBeta.Cookie[]>((resolve, reject) => {
GM_cookie('list', {}, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
const setCookie = (cookieObj: TampermonkeyBeta.Cookie) =>
new Promise<TampermonkeyBeta.Cookie[]>((resolve, reject) => {
GM_cookie('set', cookieObj, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
const deleteCookie = async () => {
const currentCookie = await getCookie();
return new Promise((resolve, reject) => {
currentCookie.forEach((cookieRecord) => {
GM_cookie('delete', { name: cookieRecord.name }, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
});
};
const getSession = async () => { const getSession = async () => {
try { try {
const cookie = await getCookie(); const cookie = await gmGetCookie();
const exportSessionData = JSON.stringify(cookie); const exportSessionData = JSON.stringify(cookie);
copyToClipboard(exportSessionData); copyToClipboard(exportSessionData);
handleToastOpen('Session 数据已复制到剪贴板', 'success'); handleToastOpen('Session 数据已复制到剪贴板', 'success');
@ -111,7 +79,7 @@ const App = () => {
prompt('在此处粘贴你的 Session 数据', '[{}]') || '[{}]'; prompt('在此处粘贴你的 Session 数据', '[{}]') || '[{}]';
const cookie = JSON.parse(importSessionData) as TampermonkeyBeta.Cookie[]; const cookie = JSON.parse(importSessionData) as TampermonkeyBeta.Cookie[];
cookie.forEach(async (item) => { cookie.forEach(async (item) => {
await setCookie(item); await gmSetCookie(item);
}); });
handleToastOpen('Session 数据已导入', 'success'); handleToastOpen('Session 数据已导入', 'success');
} catch (error) { } catch (error) {
@ -121,7 +89,7 @@ const App = () => {
const deleteSession = async () => { const deleteSession = async () => {
try { try {
await deleteCookie(); await gmDeleteCookie();
handleToastOpen('Session 数据已删除', 'success'); handleToastOpen('Session 数据已删除', 'success');
} catch (error) { } catch (error) {
handleToastOpen('删除 Cookie 失败', 'error'); handleToastOpen('删除 Cookie 失败', 'error');
@ -145,8 +113,7 @@ const App = () => {
return ( return (
<> <>
{/* 工具按钮 */} <ToolButton
<CustomButton
color="primary" color="primary"
id="custom-button" id="custom-button"
aria-controls={menuOpen ? 'custom-menu' : undefined} aria-controls={menuOpen ? 'custom-menu' : undefined}
@ -155,10 +122,9 @@ const App = () => {
onClick={handleMenuOpen} onClick={handleMenuOpen}
> >
<AutoFixHighIcon /> <AutoFixHighIcon />
</CustomButton> </ToolButton>
{/* 工具菜单 */} <ToolMenu
<CustomMenu
id="custom-menu" id="custom-menu"
anchorEl={anchorEl} anchorEl={anchorEl}
open={menuOpen} open={menuOpen}
@ -189,9 +155,8 @@ const App = () => {
</ListItemIcon> </ListItemIcon>
<ListItemText> Session </ListItemText> <ListItemText> Session </ListItemText>
</MenuItem> </MenuItem>
</CustomMenu> </ToolMenu>
{/* 提示 */}
<Snackbar <Snackbar
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }} anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
open={toastOpen} open={toastOpen}

41
src/utils/gm-cookie.ts Normal file
View File

@ -0,0 +1,41 @@
const getCookie = () =>
new Promise<TampermonkeyBeta.Cookie[]>((resolve, reject) => {
GM_cookie('list', {}, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
const setCookie = (cookieObj: TampermonkeyBeta.Cookie) =>
new Promise<TampermonkeyBeta.Cookie[]>((resolve, reject) => {
GM_cookie('set', cookieObj, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
const deleteCookie = async () => {
const currentCookie = await getCookie();
if (currentCookie.length === 0) {
return [];
}
return new Promise<TampermonkeyBeta.Cookie[]>((resolve, reject) => {
currentCookie.forEach((cookieRecord) => {
GM_cookie('delete', { name: cookieRecord.name }, (cookie, error) => {
if (error) {
reject(error);
} else {
resolve(cookie);
}
});
});
});
};
export { getCookie, setCookie, deleteCookie };

View File

@ -9,14 +9,15 @@ export default defineConfig({
monkey({ monkey({
entry: 'src/main.tsx', entry: 'src/main.tsx',
userscript: { userscript: {
name: 'Session Magician', name: 'Cookie Magician',
namespace: 'https://www.imbytecat.com/', namespace: 'https://www.imbytecat.com/',
icon: 'https://vitejs.dev/logo.svg', icon: 'https://vitejs.dev/logo.svg',
version: '3.1.0', version: '3.1.2',
description: 'Session Magician & Session Tools & Export/Import Sessions', description: 'Cookie Magician & Cookie Tools & Export/Import Cookies',
author: 'imbytecat', author: 'imbytecat',
match: ['*://*/*'], match: ['*://*/*'],
grant: ['GM_registerMenuCommand', 'GM_setClipboard', 'GM_notification'], grant: ['GM_setClipboard'],
license: 'GPL-3.0-or-later',
$extra: { $extra: {
grant: ['GM_cookie'], grant: ['GM_cookie'],
}, },