-
Notifications
You must be signed in to change notification settings - Fork 153
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
9 changed files
with
229 additions
and
20 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
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,18 @@ | ||
// ==UserScript== | ||
// @name 重试示例 | ||
// @namespace https://bbs.tampermonkey.net.cn/ | ||
// @version 0.1.0 | ||
// @description try to take over the world! | ||
// @author You | ||
// @crontab * * once * * | ||
// @grant GM_notification | ||
// ==/UserScript== | ||
|
||
return new Promise((resolve, reject) => { | ||
// Your code here... | ||
GM_notification({ | ||
title: "retry", | ||
text: "10秒后重试" | ||
}); | ||
reject(new CATRetryError("xxx错误", 10)); | ||
}); |
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
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,91 @@ | ||
/* eslint-disable func-names */ | ||
/* eslint-disable max-classes-per-file */ | ||
import { MessageManager } from "@App/app/message/message"; | ||
import { ScriptRunResouce } from "@App/app/repo/scripts"; | ||
import ExecScript from "./exec_script"; | ||
|
||
export class CATRetryError { | ||
msg: string; | ||
|
||
time: Date; | ||
|
||
constructor(msg: string, time: number | Date) { | ||
this.msg = msg; | ||
if (typeof time === "number") { | ||
this.time = new Date(Date.now() + time * 1000); | ||
} else { | ||
this.time = time; | ||
} | ||
} | ||
} | ||
|
||
export class BgExecScriptWarp extends ExecScript { | ||
setTimeout: Map<number, boolean>; | ||
|
||
setInterval: Map<number, boolean>; | ||
|
||
constructor(scriptRes: ScriptRunResouce, message: MessageManager) { | ||
const thisContext: { [key: string]: any } = {}; | ||
const setTimeout = new Map<number, any>(); | ||
const setInterval = new Map<number, any>(); | ||
thisContext.setTimeout = function ( | ||
handler: () => void, | ||
timeout: number | undefined, | ||
...args: any | ||
) { | ||
const t = global.setTimeout( | ||
function () { | ||
setTimeout.delete(t); | ||
if (typeof handler === "function") { | ||
handler(); | ||
} | ||
}, | ||
timeout, | ||
...args | ||
); | ||
setTimeout.set(t, true); | ||
return t; | ||
}; | ||
thisContext.clearTimeout = function (t: number | undefined) { | ||
global.clearTimeout(t); | ||
}; | ||
thisContext.setInterval = function ( | ||
handler: () => void, | ||
timeout: number | undefined, | ||
...args: any | ||
) { | ||
const t = global.setInterval( | ||
function () { | ||
setInterval.delete(t); | ||
if (typeof handler === "function") { | ||
handler(); | ||
} | ||
}, | ||
timeout, | ||
...args | ||
); | ||
setInterval.set(t, true); | ||
return t; | ||
}; | ||
thisContext.clearInterval = function (t: number | undefined) { | ||
global.clearInterval(t); | ||
}; | ||
// @ts-ignore | ||
thisContext.CATRetryError = CATRetryError; | ||
super(scriptRes, message, undefined, thisContext); | ||
this.setTimeout = setTimeout; | ||
this.setInterval = setInterval; | ||
} | ||
|
||
stop() { | ||
this.setTimeout.forEach((_, t) => { | ||
global.clearTimeout(t); | ||
}); | ||
this.setTimeout.clear(); | ||
this.setInterval.forEach((_, t) => { | ||
global.clearInterval(t); | ||
}); | ||
this.setInterval.clear(); | ||
return super.stop(); | ||
} | ||
} |
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
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