-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
376 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import IoC from "@App/app/ioc"; | ||
import { Script } from "@App/app/repo/scripts"; | ||
import { Value, ValueDAO } from "@App/app/repo/value"; | ||
import ValueManager from "@App/app/service/value/manager"; | ||
|
||
export type ExportCookies = { | ||
[key: string]: any; | ||
domain?: string; | ||
url?: string; | ||
cookie: chrome.cookies.Cookie[]; | ||
}; | ||
|
||
export type ExportParams = { | ||
[key: string]: any; | ||
exportValue: string; | ||
exportCookie: string; | ||
overwriteValue: boolean; | ||
overwriteCookie: boolean; | ||
}; | ||
|
||
export default interface CloudScript { | ||
exportCloud( | ||
script: Script, | ||
values: Value[], | ||
cookies: ExportCookies[] | ||
): Promise<void>; | ||
} | ||
|
||
function getCookies( | ||
detail: chrome.cookies.GetAllDetails | ||
): Promise<chrome.cookies.Cookie[]> { | ||
return new Promise((resolve) => { | ||
chrome.cookies.getAll(detail, (cookies) => { | ||
resolve(cookies); | ||
}); | ||
}); | ||
} | ||
|
||
// 解析导出cookie表达式生成导出的cookie | ||
export function parseExportCookie( | ||
exportCookie: string | ||
): Promise<ExportCookies[]> { | ||
const lines = exportCookie.split("\n"); | ||
const result = []; | ||
for (let i = 0; i < lines.length; i += 1) { | ||
const line = lines[i]; | ||
const detail: ExportCookies = { | ||
cookie: [], | ||
}; | ||
if (line.trim()) { | ||
line.split(";").forEach((param) => { | ||
const s = param.split("="); | ||
if (s.length !== 2) { | ||
return; | ||
} | ||
detail[s[0].trim()] = s[1].trim(); | ||
}); | ||
if (!detail.url && !detail.domain) { | ||
result.push( | ||
new Promise<ExportCookies>((resolve) => { | ||
detail.cookies = getCookies(detail); | ||
resolve(detail); | ||
}) | ||
); | ||
} | ||
} | ||
} | ||
return Promise.all(result); | ||
} | ||
|
||
// 解析value表达式生成导出的value | ||
export async function parseExportValue( | ||
script: Script, | ||
exportValue: string | ||
): Promise<Value[]> { | ||
const lines = exportValue.split("\n"); | ||
const result = []; | ||
const valueManager = IoC.instance(ValueManager) as ValueManager; | ||
const values = await valueManager.getValues(script); | ||
for (let i = 0; i < lines.length; i += 1) { | ||
const line = lines[i]; | ||
if (line.trim()) { | ||
const s = line.split(","); | ||
for (let n = 0; n < s.length; n += 1) { | ||
const key = s[n].trim(); | ||
if (key && values[key]) { | ||
result.push(values[key]); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ExtVersion } from "@App/app/const"; | ||
import { Script } from "@App/app/repo/scripts"; | ||
import { Value } from "@App/app/repo/value"; | ||
import JSZip from "jszip"; | ||
import packageTpl from "@App/template/cloudcat-package/package.tpl"; | ||
import utilsTpl from "@App/template/cloudcat-package/utils.tpl"; | ||
import indexTpl from "@App/template/cloudcat-package/index.tpl"; | ||
import CloudScript, { ExportCookies, ExportParams } from "./cloudscript"; | ||
|
||
// 导出到本地,一个可执行到npm包 | ||
export default class LocalCloudScript implements CloudScript { | ||
zip: JSZip; | ||
|
||
params: ExportParams; | ||
|
||
constructor(params: ExportParams) { | ||
this.zip = params.zip! as JSZip; | ||
this.params = params; | ||
} | ||
|
||
exportCloud( | ||
script: Script, | ||
values: Value[], | ||
cookies: ExportCookies[] | ||
): Promise<void> { | ||
this.zip.file("userScript.js", script.code); | ||
this.zip.file("cookies.js", `exports.cookies = ${JSON.stringify(cookies)}`); | ||
this.zip.file("values.js", `exports.values = ${JSON.stringify(values)}`); | ||
this.zip.file( | ||
"config.js", | ||
`export default ${JSON.stringify({ | ||
version: ExtVersion, | ||
uuid: script.uuid, | ||
overwrite: { | ||
value: this.params.overwriteValue, | ||
cookie: this.params.overwriteCookie, | ||
}, | ||
})}` | ||
); | ||
this.zip.file("package.json", <string>packageTpl); | ||
this.zip.file("utils.js", <string>utilsTpl); | ||
this.zip.file("index.js", <string>indexTpl); | ||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.