|
| 1 | +import IoC from "@App/app/ioc"; |
| 2 | +import { Script } from "@App/app/repo/scripts"; |
| 3 | +import { Value, ValueDAO } from "@App/app/repo/value"; |
| 4 | +import ValueManager from "@App/app/service/value/manager"; |
| 5 | + |
| 6 | +export type ExportCookies = { |
| 7 | + [key: string]: any; |
| 8 | + domain?: string; |
| 9 | + url?: string; |
| 10 | + cookie: chrome.cookies.Cookie[]; |
| 11 | +}; |
| 12 | + |
| 13 | +export type ExportParams = { |
| 14 | + [key: string]: any; |
| 15 | + exportValue: string; |
| 16 | + exportCookie: string; |
| 17 | + overwriteValue: boolean; |
| 18 | + overwriteCookie: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +export default interface CloudScript { |
| 22 | + exportCloud( |
| 23 | + script: Script, |
| 24 | + values: Value[], |
| 25 | + cookies: ExportCookies[] |
| 26 | + ): Promise<void>; |
| 27 | +} |
| 28 | + |
| 29 | +function getCookies( |
| 30 | + detail: chrome.cookies.GetAllDetails |
| 31 | +): Promise<chrome.cookies.Cookie[]> { |
| 32 | + return new Promise((resolve) => { |
| 33 | + chrome.cookies.getAll(detail, (cookies) => { |
| 34 | + resolve(cookies); |
| 35 | + }); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +// 解析导出cookie表达式生成导出的cookie |
| 40 | +export function parseExportCookie( |
| 41 | + exportCookie: string |
| 42 | +): Promise<ExportCookies[]> { |
| 43 | + const lines = exportCookie.split("\n"); |
| 44 | + const result = []; |
| 45 | + for (let i = 0; i < lines.length; i += 1) { |
| 46 | + const line = lines[i]; |
| 47 | + const detail: ExportCookies = { |
| 48 | + cookie: [], |
| 49 | + }; |
| 50 | + if (line.trim()) { |
| 51 | + line.split(";").forEach((param) => { |
| 52 | + const s = param.split("="); |
| 53 | + if (s.length !== 2) { |
| 54 | + return; |
| 55 | + } |
| 56 | + detail[s[0].trim()] = s[1].trim(); |
| 57 | + }); |
| 58 | + if (!detail.url && !detail.domain) { |
| 59 | + result.push( |
| 60 | + new Promise<ExportCookies>((resolve) => { |
| 61 | + detail.cookies = getCookies(detail); |
| 62 | + resolve(detail); |
| 63 | + }) |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return Promise.all(result); |
| 69 | +} |
| 70 | + |
| 71 | +// 解析value表达式生成导出的value |
| 72 | +export async function parseExportValue( |
| 73 | + script: Script, |
| 74 | + exportValue: string |
| 75 | +): Promise<Value[]> { |
| 76 | + const lines = exportValue.split("\n"); |
| 77 | + const result = []; |
| 78 | + const valueManager = IoC.instance(ValueManager) as ValueManager; |
| 79 | + const values = await valueManager.getValues(script); |
| 80 | + for (let i = 0; i < lines.length; i += 1) { |
| 81 | + const line = lines[i]; |
| 82 | + if (line.trim()) { |
| 83 | + const s = line.split(","); |
| 84 | + for (let n = 0; n < s.length; n += 1) { |
| 85 | + const key = s[n].trim(); |
| 86 | + if (key && values[key]) { |
| 87 | + result.push(values[key]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return result; |
| 93 | +} |
0 commit comments