Skip to content

Commit

Permalink
⚡️ 删除GM_getCookieStore使用GM_Cookie('store')操作
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 27, 2021
1 parent fa9dcbc commit 1ae5096
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 132 deletions.
96 changes: 42 additions & 54 deletions src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class BackgroundGrant {
let isScriptcat = false;
const requestHeaders: chrome.webRequest.HttpHeader[] = [];
const unsafeHeader: { [key: string]: string } = {};
data.requestHeaders?.forEach((val, key) => {
data.requestHeaders?.forEach((val) => {
const lowerCase = val.name.toLowerCase();
switch (lowerCase) {
case 'x-cat-' + this.rand + '-cookie': {
Expand Down Expand Up @@ -387,7 +387,7 @@ export class BackgroundGrant {

}

protected dealXhr(config: GM_Types.XHRDetails, xhr: XMLHttpRequest): GM_Types.XHRResponse {
protected dealXhr(config: GMSend.XHRDetails, xhr: XMLHttpRequest): GM_Types.XHRResponse {
const removeXCat = new RegExp('x-cat-' + this.rand + '-', 'g');
const respond: GM_Types.XHRResponse = {
finalUrl: xhr.responseURL || config.url,
Expand All @@ -405,7 +405,7 @@ export class BackgroundGrant {
respond.response = URL.createObjectURL(xhr.response);
}
setTimeout(() => {
URL.revokeObjectURL(respond.response);
URL.revokeObjectURL(<string>respond.response);
}, 60e3)
} else if (config.responseType == 'json') {
try {
Expand Down Expand Up @@ -472,26 +472,25 @@ export class BackgroundGrant {
if (config.responseType != 'json') {
xhr.responseType = config.responseType || '';
}
const _this = this;

function deal(event: string) {
const respond = _this.dealXhr(config, xhr);
const deal = (event: string) => {
const respond = this.dealXhr(config, xhr);
grant.data = { type: event, data: respond };
post.postMessage(grant);
}
xhr.onload = (event) => {
xhr.onload = () => {
deal('load');
}
xhr.onloadstart = (event) => {
xhr.onloadstart = () => {
deal('onloadstart');
}
xhr.onloadend = (event) => {
xhr.onloadend = () => {
deal('onloadstart');
}
xhr.onabort = (event) => {
xhr.onabort = () => {
deal('onabort');
}
xhr.onerror = (event) => {
xhr.onerror = () => {
deal('onerror');
}
xhr.onprogress = (event) => {
Expand All @@ -505,7 +504,7 @@ export class BackgroundGrant {
grant.data = { type: 'onprogress', data: respond };
post.postMessage(grant);
}
xhr.onreadystatechange = (event) => {
xhr.onreadystatechange = () => {
deal('onreadystatechange');
}
xhr.ontimeout = () => {
Expand Down Expand Up @@ -545,61 +544,41 @@ export class BackgroundGrant {
if (config.overrideMimeType) {
xhr.overrideMimeType(config.overrideMimeType);
}
if ((<any>config).dataType && (<any>config).dataType == 'FormData') {
if (config.dataType == 'FormData') {
const data = new FormData();
for (const key in config.data) {
const val = config.data[key];
if (val.type == 'file') {
data.append(val.key, base64ToBlob(val.val), val.filename);
} else {
data.append(val.key, val.val);
}
if (config.data && config.data instanceof Array) {
config.data.forEach((val: GMSend.XHRFormData) => {
if (val.type == 'file') {
data.append(val.key, base64ToBlob(val.val), val.filename);
} else {
data.append(val.key, val.val);
}
});
xhr.send(data);
}
xhr.send(data);
} else {
xhr.send(config.data);
xhr.send(<string>config.data);
}
return resolve(undefined);
});
}

@BackgroundGrant.GMFunction({
background: true
})
protected GM_getCookieStore(grant: Grant, post: IPostMessage): Promise<any> {
return new Promise((resolve, reject) => {
const tabid = grant.params[0];
if (!tabid) {
return reject('tabis is null');
}
chrome.cookies.getAllCookieStores(s => {
for (let i = 0; i < s.length; i++) {
for (let n = 0; n < s[i].tabIds.length; n++) {
if (s[i].tabIds[n] == tabid) {
grant.data = { type: 'done', data: s[i].id };
post.postMessage(grant);
return resolve(undefined);
}
}
}
reject('not found');
});
});
}

@BackgroundGrant.GMFunction({
confirm: (grant: Grant, script: Script) => {
return new Promise((resolve, reject) => {
if (grant.params[0] == 'store') {
resolve(true);
}
const detail = <GM_Types.CookieDetails>grant.params[1];
if ((!detail.url && !detail.domain)) {
return reject('there must be one of url or domain');
}
let url: any = {};
let url: URL = <URL>{};
if (detail.url) {
url = new URL(detail.url);
} else {
url.host = detail.domain;
url.hostname = detail.domain;
url.host = detail.domain || '';
url.hostname = detail.domain || '';
}
let flag = false;
if (script.metadata['connect']) {
Expand Down Expand Up @@ -635,19 +614,28 @@ export class BackgroundGrant {
if (param.length != 2) {
return reject('there must be two parameters');
}
const detail = <GM_Types.CookieDetails>grant.params[1];
if (param[0] == 'store') {
chrome.cookies.getAllCookieStores(res => {
const data: any[] = [];
res.forEach(val => {
data.push({ storeId: val.id });
if (detail.tabId) {
for (let n = 0; n < val.tabIds.length; n++) {
if (val.tabIds[n] == detail.tabId) {
data.push({ storeId: val.id });
break;
}
}
} else {
data.push({ storeId: val.id });
}
});
grant.data = { type: 'done', data: data };
post.postMessage(grant);
});
return;
}
const detail = <GM_Types.CookieDetails>grant.params[1];
// url或者域名不能为空,且必须有name
// url或者域名不能为空
if (detail.url) {
detail.url = detail.url.trim();
}
Expand All @@ -674,7 +662,7 @@ export class BackgroundGrant {
break;
}
case 'delete': {
if (!detail.url) {
if (!detail.url || !detail.name) {
return reject('delete operation must have url and name');
}
chrome.cookies.remove({
Expand All @@ -688,7 +676,7 @@ export class BackgroundGrant {
break;
}
case 'set': {
if (!detail.url) {
if (!detail.url || !detail.name) {
return reject('set operation must have url or domain, and the name must exist');
}
chrome.cookies.set({
Expand Down
6 changes: 3 additions & 3 deletions src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class FrontendGrant implements ScriptContext {

this.postRequest('GM_xmlhttpRequest', [param], (grant: Grant) => {
if (grant.error) {
details.onerror && details.onerror(grant.errorMsg);
details.onerror && details.onerror(grant.errorMsg || '');
return;
}
switch (grant.data.type) {
Expand All @@ -228,7 +228,7 @@ export class FrontendGrant implements ScriptContext {
details.ontimeout && details.ontimeout();
break;
case 'onerror':
details.onerror && details.onerror();
details.onerror && details.onerror('');
break;
case 'onabort':
details.onabort && details.onabort();
Expand Down Expand Up @@ -298,7 +298,7 @@ export class FrontendGrant implements ScriptContext {
}

@FrontendGrant.GMFunction()
public GM_log(message: string, level?: GM_Types.LOGGER_LEVEL): void {
public GM_log(message: string, level?: GM_Types.LoggerLevel): void {
this.postRequest('GM_log', [message, level]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/apps/script/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ export class ScriptController {
ret.grantMap = {};

ret.metadata['grant']?.forEach((val: string) => {
ret.grantMap![val] = 'ok';
ret.grantMap[val] = 'ok';
});

resolve(ret);
Expand Down
2 changes: 1 addition & 1 deletion src/model/do/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface Config {
export type UserConfig = { [key: string]: { [key: string]: Config } };

export interface ScriptCache extends Script {
grantMap?: { [key: string]: string }
grantMap: { [key: string]: string }
value: { [key: string]: Value }
flag?: string
resource: { [key: string]: Resource }
Expand Down
35 changes: 22 additions & 13 deletions src/types/main.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare let sandbox: any;

declare module "@App/tampermonkey.d.ts";
declare module "*.tpl";
declare module '@App/tampermonkey.d.ts';
declare module '*.tpl';

interface ITabItem {
tabKey: string | number;
Expand All @@ -15,7 +15,7 @@ interface ITabItem {
message?: string;
beforeChange?: (tabPane: TabPane) => Promise<boolean>;
beforeRemove?: (tabPane: TabPane) => Promise<boolean>;
template?: "normal" | "crontab" | "background";
template?: 'normal' | 'crontab' | 'background';
}

interface IChangeTitle {
Expand All @@ -32,13 +32,13 @@ interface IEditScript {


interface ICreateScript {

//
}

interface INewScript {
scriptId: number;
tabKey: string | number;
template?: "normal" | "crontab" | "background";
template?: 'normal' | 'crontab' | 'background';
}

interface IUpdateMeta {
Expand All @@ -62,25 +62,25 @@ interface ICodeChange {

declare const ScriptFlag;

declare module chrome {
declare module clipboard {
declare namespace chrome {
declare namespace clipboard {
declare function setImageData(
imageData: ArrayBuffer,
type: ImageType,
additionalItems: AdditionalDataItem[],
callback: function,
);

type DataItemType = "textPlain" | "textHtml";
type ImageType = "png" | "jpeg";
type DataItemType = 'textPlain' | 'textHtml';
type ImageType = 'png' | 'jpeg';
declare interface AdditionalDataItem {
data: string;
type: DataItemType;
}
}
}

declare var top: Window;
declare const top: Window;

interface Userinfo {
id: number;
Expand All @@ -90,20 +90,29 @@ interface Userinfo {

declare namespace GMSend {
interface XHRDetails {
method?: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
url: string
headers?: { [key: string]: string }
data?: string | Array
data?: string | Array<XHRFormData>
cookie?: string
binary?: boolean
timeout?: number
context?: CONTEXT_TYPE
responseType?: "arraybuffer" | "blob" | "json"
responseType?: 'arraybuffer' | 'blob' | 'json'
overrideMimeType?: string,
anonymous?: boolean,
fetch?: boolean,
user?: string,
password?: string,
nocache?: boolean
dataType?: 'FormData'
}

interface XHRFormData {
type?: 'file'
key: string
val: string
filename?: string
}
}

Loading

0 comments on commit 1ae5096

Please sign in to comment.