Skip to content

Commit c40d8d2

Browse files
authored
Merge pull request #5 from kungfux/feature/apply_settings_in_runtime
Apply settings in runtime
2 parents 31d44e5 + 27426e1 commit c40d8d2

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/background/service-worker.ts renamed to src/background/background-service.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import type { Tabs, WebNavigation } from 'webextension-polyfill';
22
import { tabs, webNavigation, scripting } from 'webextension-polyfill';
3+
import { Settings } from '../settings';
34

4-
export class ServiceWorker {
5-
private readonly urls: string[];
6-
private readonly urlRegexs: RegExp[];
7-
8-
public constructor(urls: string[]) {
9-
this.urls = urls;
10-
this.urlRegexs = [];
11-
for (const url of this.urls) {
12-
this.urlRegexs.push(new RegExp(url, 'i'));
13-
}
14-
}
5+
export class BackgroundService {
6+
private readonly settings = new Settings();
7+
private urlRegexs: RegExp[] = [];
158

169
public init(): void {
10+
void this.getUrlsToListen();
11+
this.settings.onChanged(() => {
12+
void this.getUrlsToListen();
13+
});
1714
tabs.onUpdated.addListener((tabId, changeInfo, tabInfo) => {
1815
void this.onTabUpdated(tabId, changeInfo, tabInfo);
1916
});
@@ -22,7 +19,7 @@ export class ServiceWorker {
2219
});
2320
}
2421

25-
public async onTabUpdated(
22+
private async onTabUpdated(
2623
tabId: number,
2724
changeInfo: Tabs.OnUpdatedChangeInfoType,
2825
tabInfo: Tabs.Tab
@@ -32,14 +29,22 @@ export class ServiceWorker {
3229
}
3330
}
3431

35-
public async onNavigationCompleted(
32+
private async onNavigationCompleted(
3633
details: WebNavigation.OnCompletedDetailsType
3734
): Promise<void> {
3835
if (details.url.length > 0 && this.isJiraUrl(details.url)) {
3936
await this.injectContentScript(details.tabId);
4037
}
4138
}
4239

40+
private async getUrlsToListen(): Promise<void> {
41+
const { urls } = await this.settings.getSettings();
42+
this.urlRegexs = [];
43+
for (const url of urls) {
44+
this.urlRegexs.push(new RegExp(url, 'i'));
45+
}
46+
}
47+
4348
private isJiraUrl(url: string): boolean {
4449
for (const x of this.urlRegexs) {
4550
if (x.test(url)) {

src/background/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { runtime } from 'webextension-polyfill';
2-
import { Settings } from '../settings';
3-
import { ServiceWorker } from './service-worker';
2+
import { BackgroundService } from './background-service';
43

5-
runtime.onInstalled.addListener((details) => {
4+
new BackgroundService().init();
5+
6+
runtime.onInstalled.addListener(() => {
67
void runtime.openOptionsPage();
78
});
8-
9-
const settings = new Settings();
10-
const s = await settings.getSettings();
11-
const serviceWorker = new ServiceWorker(s.urls);
12-
serviceWorker.init();

src/content/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ declare global {
1111
return;
1212
}
1313
window.hasRun = true;
14-
new IssueExpert().print();
14+
new IssueExpert().init();
1515
})();

src/content/issue-expert.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import { Settings } from '../settings';
2+
13
export class IssueExpert {
2-
print(): void {
3-
console.log('Hello there');
4+
private readonly settings = new Settings();
5+
6+
init(): void {
7+
console.log('Injected!');
8+
this.settings.onChanged(() => {
9+
console.log('Settings changed');
10+
});
411
}
512
}

src/settings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ export class Settings {
1818
urls: settings.urls,
1919
});
2020
}
21+
22+
public onChanged(callback: () => void): void {
23+
if (!storage.onChanged.hasListener(callback)) {
24+
storage.onChanged.addListener(() => {
25+
callback();
26+
});
27+
}
28+
}
2129
}

0 commit comments

Comments
 (0)