Skip to content

Commit b5670c9

Browse files
committed
🐛 修复iframe中运行不在popup页显示的问题与添加脚本运行次数 #154
1 parent 4d6eafe commit b5670c9

8 files changed

Lines changed: 59 additions & 20 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dist-ssr
1313
*.local
1414

1515
# Editor directories and files
16-
.vscode/*
16+
.vscode
1717
!.vscode/extensions.json
1818
.idea
1919
.DS_Store

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptcat",
3-
"version": "0.11.3",
3+
"version": "0.11.4",
44
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
55
"author": "CodFrm",
66
"license": "GPLv3",

src/app/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const ExtVersion = "0.11.3";
1+
export const ExtVersion = "0.11.4";
22

33
export const ExtServer = "https://ext.scriptcat.org/";
44

src/app/service/system/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class SystemManager extends Manager {
4848
checkUpdate();
4949
setInterval(() => {
5050
checkUpdate();
51-
}, 7200 * 1000);
51+
}, 3600 * 1000 * 6);
5252

5353
if (process.env.NODE_ENV === "production") {
5454
chrome.runtime.onInstalled.addListener((details) => {

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "ScriptCat",
4-
"version": "0.11.3",
4+
"version": "0.11.4",
55
"author": "CodFrm",
66
"description": "脚本猫,一个用户脚本管理器,支持后台脚本、定时脚本、页面脚本,可编写脚本每天帮你自动处理事务.",
77
"options_ui": {

src/pages/components/ScriptMenuList/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ const ScriptMenuList: React.FC<{
8484
onClick={(e) => {
8585
e.stopPropagation();
8686
}}
87+
title={
88+
item.enable ? `该脚本运行了${item.runNum}次` : "该脚本未开启"
89+
}
8790
>
8891
<Space>
8992
<Switch

src/runtime/background/runtime.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type ScriptMenu = {
4646
updatetime: number;
4747
hasUserConfig: boolean;
4848
runStatus?: SCRIPT_RUN_STATUS;
49+
runNum: number;
4950
menus?: ScriptMenuItem[];
5051
};
5152

@@ -252,17 +253,45 @@ export default class Runtime extends Manager {
252253
}
253254
});
254255

256+
// 记录运行次数与iframe运行
257+
const runScript = new Map<
258+
number,
259+
Map<number, { script: Script; runNum: number }>
260+
>();
261+
const addRunScript = (tabId: number, script: Script) => {
262+
let scripts = runScript.get(tabId);
263+
if (!scripts) {
264+
scripts = new Map();
265+
runScript.set(tabId, scripts);
266+
}
267+
let scriptNum = scripts.get(script.id);
268+
if (!scriptNum) {
269+
scriptNum = { script, runNum: 0 };
270+
scripts.set(script.id, scriptNum);
271+
}
272+
if (script.status === SCRIPT_STATUS_ENABLE) {
273+
scriptNum.runNum += 1;
274+
}
275+
};
276+
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
277+
if (changeInfo.status === "loading") {
278+
runScript.delete(tabId);
279+
}
280+
});
281+
chrome.tabs.onRemoved.addListener((tabId) => {
282+
runScript.delete(tabId);
283+
});
255284
// 给popup页面获取运行脚本,与菜单
256285
this.message.setHandler(
257286
"queryPageScript",
258-
(action: string, { url, tabId }: any) => {
287+
(action: string, { tabId }: any) => {
259288
const tabMap = scriptMenu.get(tabId);
260-
const matchScripts = this.matchUrl(url);
289+
const matchScripts = runScript.get(tabId) || [];
261290
const scriptList: ScriptMenu[] = [];
262291
matchScripts.forEach((item) => {
263292
const menus: ScriptMenuItem[] = [];
264293
if (tabMap) {
265-
tabMap.get(item.id)?.forEach((scriptItem) => {
294+
tabMap.get(item.script.id)?.forEach((scriptItem) => {
266295
menus.push({
267296
name: scriptItem.request.params[1],
268297
accessKey: scriptItem.request.params[2],
@@ -273,11 +302,12 @@ export default class Runtime extends Manager {
273302
});
274303
}
275304
scriptList.push({
276-
id: item.id,
277-
name: item.name,
278-
enable: item.status === SCRIPT_STATUS_ENABLE,
279-
updatetime: item.updatetime || item.createtime,
280-
hasUserConfig: !!item.config,
305+
id: item.script.id,
306+
name: item.script.name,
307+
enable: item.script.status === SCRIPT_STATUS_ENABLE,
308+
updatetime: item.script.updatetime || item.script.createtime,
309+
hasUserConfig: !!item.script.config,
310+
runNum: item.runNum,
281311
menus,
282312
});
283313
});
@@ -303,6 +333,7 @@ export default class Runtime extends Manager {
303333
updatetime: item.updatetime || item.createtime,
304334
runStatus: item.runStatus,
305335
hasUserConfig: !!item.config,
336+
runNum: item.status === SCRIPT_STATUS_ENABLE ? 1 : 0,
306337
menus,
307338
});
308339
});
@@ -328,11 +359,16 @@ export default class Runtime extends Manager {
328359
const filter: ScriptRunResouce[] = this.matchUrl(
329360
sender.url,
330361
(script) => {
331-
// 开启并且不是iframe
332-
return (
333-
script.status !== SCRIPT_STATUS_ENABLE ||
334-
(sender.frameId !== undefined && !!script.metadata.noframes)
335-
);
362+
// 如果是iframe,判断是否允许在iframe里运行
363+
if (sender.frameId !== undefined) {
364+
if (script.metadata.noframes) {
365+
return false;
366+
}
367+
addRunScript(sender.tabId!, script);
368+
return script.status === SCRIPT_STATUS_ENABLE;
369+
}
370+
addRunScript(sender.tabId!, script);
371+
return script.status === SCRIPT_STATUS_ENABLE;
336372
}
337373
);
338374

0 commit comments

Comments
 (0)