Skip to content

Commit

Permalink
🐛 修复在同tab上切换url不会清空掉老脚本导致错误加载的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Feb 13, 2022
1 parent d919e8a commit 37b88fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/apps/script/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,18 +577,21 @@ export class ScriptManager extends Manager {
}

public stopScript(msg: any): Promise<boolean> {
return new Promise(async resolve => {
const script = await this.scriptModel.findById(msg.scriptId);
if (!script) {
return resolve(false);
}
if (script.type == SCRIPT_TYPE_CRONTAB || script.type == SCRIPT_TYPE_BACKGROUND) {
await this.background.stopScript(script, msg.isdebug);
this.setRunComplete(script.id)
resolve(true);
} else {
resolve(false);
return new Promise(resolve => {
const handler = async () => {
const script = await this.scriptModel.findById(msg.scriptId);
if (!script) {
return resolve(false);
}
if (script.type == SCRIPT_TYPE_CRONTAB || script.type == SCRIPT_TYPE_BACKGROUND) {
await this.background.stopScript(script, msg.isdebug);
void this.setRunComplete(script.id)
resolve(true);
} else {
resolve(false);
}
}
void handler();
});
}

Expand Down Expand Up @@ -655,7 +658,7 @@ export class ScriptManager extends Manager {
const filter: ScriptCache[] = [];

let matchScript = runMatchScript.get(detail.tab.id);
if (!matchScript) {
if (!matchScript || !detail.frameId) {
matchScript = new Map();
runMatchScript.set(detail.tab.id, matchScript);
}
Expand All @@ -672,7 +675,7 @@ export class ScriptManager extends Manager {
filter.push(script);
});
// 注入框架
chrome.tabs.executeScript(detail.tab.id, {
void chrome.tabs.executeScript(detail.tab.id, {
frameId: detail.frameId,
code: `(function(){
let temp = document.createElement('script');
Expand All @@ -693,13 +696,13 @@ export class ScriptManager extends Manager {
chrome.browserAction.getBadgeText({
tabId: detail.tab?.id,
}, res => {
chrome.browserAction.setBadgeText({
void chrome.browserAction.setBadgeText({
text: (filter.length + (parseInt(res) || 0)).toString(),
tabId: detail.tab?.id,
});
});

chrome.browserAction.setBadgeBackgroundColor({
void chrome.browserAction.setBadgeBackgroundColor({
color: [255, 0, 0, 255],
tabId: detail.tab?.id,
});
Expand All @@ -725,7 +728,7 @@ export class ScriptManager extends Manager {
runAt = 'document_idle';
break;
}
chrome.tabs.executeScript(detail.tab!.id!, {
void chrome.tabs.executeScript(detail.tab!.id!, {
frameId: detail.frameId,
code: `(function(){
let temp = document.createElement('script');
Expand Down
1 change: 1 addition & 0 deletions tests/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('UrlMatch2', () => {
url.add('*://*', 'ok2');
expect(url.match('http://www.http.com')).toEqual(['ok1', 'ok2']);
expect(url.match('https://pan.baidu.com/disk/home?from=newversion&stayAtHome=true#/all?path=%2F&vmode=list')).toEqual(['ok1', 'ok2']);
expect(url.match('https://github.com/CodFrm')).toEqual(['ok1', 'ok2']);
});
it('port', () => {
const url = new UrlMatch<string>();
Expand Down

0 comments on commit 37b88fb

Please sign in to comment.