Skip to content

Commit 1a04221

Browse files
committed
reorganize background script logic; show notification after converting tag filters to keyword filters
1 parent f902c40 commit 1a04221

9 files changed

Lines changed: 136 additions & 82 deletions

File tree

app/_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,8 @@
218218
"content" : "$1"
219219
}
220220
}
221+
},
222+
"TagFiltersMigratedNotificationMessage": {
223+
"message": "Your old tag filters have been converted to new keyword filters automatically.\n\nClick this notification to view your keyword filters."
221224
}
222225
}

app/scripts/background.js

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,16 @@
1-
import semverLT from 'semver/functions/lt';
2-
import semverValid from 'semver/functions/valid';
3-
4-
import { GetCategories } from './background/categories';
5-
import { ImportFilters } from './background/filters';
6-
import { MENUS, OnMenuClicked, OnMenuShown } from './background/menus';
1+
import { InitMenus } from './background/menus';
72
import { OnRuntimeMessage } from './background/messages';
8-
import { OnLocalStorageChanged } from './background/storage';
9-
10-
import { REGEX } from './constants/url';
11-
12-
browser.runtime.onInstalled.addListener(async (details) => {
13-
const { previousVersion } = details;
14-
15-
if (semverValid(previousVersion) && semverLT(previousVersion, '6.0.0')) {
16-
const data = await browser.storage.local.get('tags');
17-
if (data?.tags) {
18-
console.warn('Converting tag filters to keyword filters');
19-
await ImportFilters(data);
20-
// TODO: is it appropriate to delete tag filters from previous versions?
21-
// await browser.storage.local.remove('tags');
22-
}
23-
}
24-
25-
// fetch and store the latest category paths
26-
await GetCategories();
27-
});
28-
29-
/* Page Action */
30-
browser.pageAction.onClicked.addListener(async (tab) => {
31-
const MANAGEMENT_URL = browser.runtime.getURL('pages/manage.html');
32-
33-
const tabs = await browser.tabs.query({
34-
'currentWindow': true,
35-
'url': MANAGEMENT_URL
36-
});
3+
import { OnNotificationClicked } from './background/notifications';
4+
import { OnInstalled } from './background/runtime';
5+
import { OnStorageChanged } from './background/storage';
6+
import { OpenOrShowURL, OnTabUpdate } from './background/tabs';
377

38-
if (tabs.length) {
39-
return browser.tabs.update(tabs[0].id, {
40-
'active': true
41-
});
42-
}
438

44-
return browser.tabs.create({
45-
'url': MANAGEMENT_URL
46-
});
47-
});
48-
49-
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
50-
if (REGEX.test(tab.url)) {
51-
browser.pageAction.show(tabId);
52-
} else {
53-
chrome.pageAction.hide(tabId);
54-
}
55-
});
56-
57-
58-
/* Runtime Messages */
9+
browser.notifications.onClicked.addListener(OnNotificationClicked);
10+
browser.pageAction.onClicked.addListener(() => OpenOrShowURL(browser.runtime.getURL('pages/manage.html')));
11+
browser.runtime.onInstalled.addListener(OnInstalled);
5912
browser.runtime.onMessage.addListener(OnRuntimeMessage);
13+
browser.storage.onChanged.addListener(OnStorageChanged);
14+
browser.tabs.onUpdated.addListener(OnTabUpdate);
6015

61-
62-
/* Storage */
63-
browser.storage.onChanged.addListener((changes, areaName) => {
64-
switch (areaName) {
65-
case 'local':
66-
OnLocalStorageChanged(changes);
67-
break;
68-
}
69-
});
70-
71-
72-
/* Context Menus */
73-
try {
74-
MENUS.forEach(menu => browser.contextMenus.remove(menu.id).finally(browser.contextMenus.create(menu)));
75-
browser.contextMenus.onClicked.addListener(OnMenuClicked);
76-
} catch (ex) {
77-
console.error('Failed to setup context menus', ex);
78-
}
79-
80-
try {
81-
browser.contextMenus.onShown.addListener(OnMenuShown);
82-
} catch (ex) {
83-
// chrome doesn't support the onShown event, but we don't use it for major functionality, so just ignore it
84-
void(ex);
85-
}
16+
InitMenus();

app/scripts/background/menus.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ export const MENUS = [
99
}
1010
];
1111

12+
/**
13+
* Initializes menus and event handlers
14+
*/
15+
export const InitMenus = () => {
16+
try {
17+
MENUS.forEach(menu => browser.contextMenus.remove(menu.id).finally(browser.contextMenus.create(menu)));
18+
browser.contextMenus.onClicked.addListener(OnMenuClicked);
19+
} catch (ex) {
20+
console.error('Failed to setup context menus', ex);
21+
}
22+
23+
try {
24+
browser.contextMenus.onShown.addListener(OnMenuShown);
25+
} catch (ex) {
26+
// chrome doesn't support the onShown event, but we don't use it for major functionality, so just ignore it
27+
void (ex);
28+
}
29+
};
30+
1231
/**
1332
* Event handler for when a menu item is clicked
1433
* @param {object} info menu info

app/scripts/background/messages.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,13 @@ export const OnRuntimeMessage = (message) => {
5050
export const SendMessageToAllTabs = async (action, data) => {
5151
const tabs = await browser.tabs.query({ 'url': '*://*.deviantart.com/*' });
5252
for (const tab of tabs) {
53-
console.debug(tab);
5453
browser.tabs.sendMessage(tab.id, { action, data });
5554
}
5655
};
5756

5857
export const SendMessageToScriptPage = async (page, action, data) => {
5958
const tabs = await browser.tabs.query({ 'url': browser.runtime.getURL(page) });
6059
for (const tab of tabs) {
61-
console.debug(tab);
6260
browser.tabs.sendMessage(tab.id, { action, data });
6361
}
6462
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { OpenOrShowURL } from './tabs';
2+
3+
import { TAG_FILTERS_MIGRATED } from '../constants/notifications';
4+
5+
/**
6+
* Event handler for notification clicks
7+
* @param {string} notificationId the ID of the clicked notification
8+
*/
9+
export const OnNotificationClicked = (notificationId) => {
10+
switch (notificationId) {
11+
case TAG_FILTERS_MIGRATED:
12+
OpenOrShowURL(browser.runtime.getURL('pages/manage.html#/keywords'));
13+
break;
14+
}
15+
16+
return browser.notifications.clear(notificationId);
17+
};

app/scripts/background/runtime.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import semverLT from 'semver/functions/lt';
2+
import semverValid from 'semver/functions/valid';
3+
4+
import { GetCategories } from './categories';
5+
import { ImportFilters } from './filters';
6+
7+
import { TAG_FILTERS_MIGRATED } from '../constants/notifications';
8+
9+
export const OnInstalled = async (details) => {
10+
const { previousVersion } = details;
11+
12+
if (semverValid(previousVersion) && semverLT(previousVersion, '6.0.0')) {
13+
const data = await browser.storage.local.get('tags');
14+
if (data?.tags) {
15+
console.warn('Converting tag filters to keyword filters');
16+
await ImportFilters(data);
17+
18+
// TODO: is it appropriate to delete tag filters from previous versions?
19+
// await browser.storage.local.remove('tags');
20+
21+
browser.notifications.create(TAG_FILTERS_MIGRATED, {
22+
'type': 'basic',
23+
'iconUrl': browser.extension.getURL('images/icon-64.png'),
24+
'title': browser.i18n.getMessage('ExtensionName'),
25+
'message': browser.i18n.getMessage('TagFiltersMigratedNotificationMessage'),
26+
});
27+
}
28+
}
29+
30+
// fetch and store the latest category paths
31+
await GetCategories();
32+
};

app/scripts/background/storage.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ export const MONITORED_STORAGE_KEYS = [
88
'users',
99
];
1010

11+
/**
12+
* Event handler for all storage changes
13+
* @param {object} changes the storage changes
14+
* @param {string} areaName the name of the changed storage area
15+
*/
16+
export const OnStorageChanged = (changes, areaName) => {
17+
switch (areaName) {
18+
case 'local':
19+
OnLocalStorageChanged(changes);
20+
break;
21+
}
22+
};
23+
24+
/**
25+
* Event handler for local storage changes
26+
* @param {object} changes the storage changes
27+
*/
1128
export const OnLocalStorageChanged = (changes) => {
1229
console.time('OnLocalStorageChanged()');
1330
for (const key of Object.keys(changes)) {

app/scripts/background/tabs.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { REGEX } from '../constants/url';
2+
3+
/**
4+
* Focuses the first tab matching the specified URL or opens it in a new tab
5+
* @param {string} url the URL to focus/show
6+
*/
7+
export const OpenOrShowURL = async (url) => {
8+
const tabs = await browser.tabs.query({
9+
'currentWindow': true,
10+
'url': url
11+
});
12+
13+
if (tabs.length) {
14+
return browser.tabs.update(tabs[0].id, {
15+
'active': true
16+
});
17+
}
18+
19+
return browser.tabs.create({
20+
'url': url
21+
});
22+
};
23+
24+
/**
25+
* Event handler for tab updates
26+
* @param {number} tabId the ID of the tab that was updated
27+
* @param {object} changeInfo properties about the tab's changes
28+
* @param {tab} tab the new state of the tab
29+
*/
30+
export const OnTabUpdate = (tabId, changeInfo, tab) => {
31+
if (REGEX.test(tab.url)) {
32+
browser.pageAction.show(tabId);
33+
} else {
34+
browser.pageAction.hide(tabId);
35+
}
36+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const TAG_FILTERS_MIGRATED = 'tag-filters-converted';

0 commit comments

Comments
 (0)