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