(
script: ScriptLoadInfo,
opts?: {
execOnce?: boolean;
cronJobOncePos?: number;
}
)
| 131 | |
| 132 | // 执行脚本 |
| 133 | async execScript( |
| 134 | script: ScriptLoadInfo, |
| 135 | opts?: { |
| 136 | execOnce?: boolean; |
| 137 | cronJobOncePos?: number; |
| 138 | } |
| 139 | ) { |
| 140 | const execOnce = opts?.execOnce ?? false; |
| 141 | const cronJobOncePos = opts?.cronJobOncePos ?? 0; |
| 142 | |
| 143 | if (cronJobOncePos >= 1) { |
| 144 | // 没有最后一次执行时间表示之前都没执行过,直接执行 |
| 145 | if (script.lastruntime) { |
| 146 | const now = new Date(); |
| 147 | const last = new Date(script.lastruntime); |
| 148 | // 根据once所在的位置去判断执行 |
| 149 | const timeDiff = now.getTime() - last.getTime(); |
| 150 | switch (cronJobOncePos) { |
| 151 | case 1: // 每分钟 |
| 152 | if (timeDiff < 2 * utime_1min && last.getMinutes() === now.getMinutes()) return; |
| 153 | break; |
| 154 | case 2: // 每小时 |
| 155 | if (timeDiff < 2 * utime_1hr && last.getHours() === now.getHours()) return; |
| 156 | break; |
| 157 | case 3: // 每天 |
| 158 | if (timeDiff < 2 * utime_1day && last.getDay() === now.getDay()) return; |
| 159 | break; |
| 160 | case 4: // 每月 |
| 161 | if (timeDiff < 62 * utime_1day && last.getMonth() === now.getMonth()) return; |
| 162 | break; |
| 163 | case 5: // 每周 |
| 164 | if (timeDiff < 14 * utime_1day && getISOWeek(last) === getISOWeek(now)) return; |
| 165 | break; |
| 166 | default: |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | const logger = this.logger.with({ uuid: script.uuid, name: script.name }); |
| 172 | if (this.execScriptMap.has(script.uuid)) { |
| 173 | // 释放掉资源 |
| 174 | // 暂未实现执行完成后立马释放,会在下一次执行时释放 |
| 175 | await this.stopScript(script.uuid); |
| 176 | } |
| 177 | const extensionEnv = await this.extensionEnvAsync; |
| 178 | |
| 179 | // 判断 run-in |
| 180 | const runIn = script.metadata?.["run-in"]?.[0]; |
| 181 | const inIncognitoContext = extensionEnv?.inIncognitoContext; |
| 182 | if (runIn && runIn !== "all" && typeof inIncognitoContext === "boolean") { |
| 183 | // 判断插件运行环境 |
| 184 | const contextType = inIncognitoContext ? "incognito-tabs" : "normal-tabs"; |
| 185 | if (runIn !== contextType) { |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | const exec = new BgExecScriptWarp(script, this.windowMessage, extensionEnv); |
no test coverage detected