-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganize background script logic; show notification after convertin…
…g tag filters to keyword filters
- Loading branch information
Showing
9 changed files
with
136 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,16 @@ | ||
import semverLT from 'semver/functions/lt'; | ||
import semverValid from 'semver/functions/valid'; | ||
|
||
import { GetCategories } from './background/categories'; | ||
import { ImportFilters } from './background/filters'; | ||
import { MENUS, OnMenuClicked, OnMenuShown } from './background/menus'; | ||
import { InitMenus } from './background/menus'; | ||
import { OnRuntimeMessage } from './background/messages'; | ||
import { OnLocalStorageChanged } from './background/storage'; | ||
|
||
import { REGEX } from './constants/url'; | ||
|
||
browser.runtime.onInstalled.addListener(async (details) => { | ||
const { previousVersion } = details; | ||
|
||
if (semverValid(previousVersion) && semverLT(previousVersion, '6.0.0')) { | ||
const data = await browser.storage.local.get('tags'); | ||
if (data?.tags) { | ||
console.warn('Converting tag filters to keyword filters'); | ||
await ImportFilters(data); | ||
// TODO: is it appropriate to delete tag filters from previous versions? | ||
// await browser.storage.local.remove('tags'); | ||
} | ||
} | ||
|
||
// fetch and store the latest category paths | ||
await GetCategories(); | ||
}); | ||
|
||
/* Page Action */ | ||
browser.pageAction.onClicked.addListener(async (tab) => { | ||
const MANAGEMENT_URL = browser.runtime.getURL('pages/manage.html'); | ||
|
||
const tabs = await browser.tabs.query({ | ||
'currentWindow': true, | ||
'url': MANAGEMENT_URL | ||
}); | ||
import { OnNotificationClicked } from './background/notifications'; | ||
import { OnInstalled } from './background/runtime'; | ||
import { OnStorageChanged } from './background/storage'; | ||
import { OpenOrShowURL, OnTabUpdate } from './background/tabs'; | ||
|
||
if (tabs.length) { | ||
return browser.tabs.update(tabs[0].id, { | ||
'active': true | ||
}); | ||
} | ||
|
||
return browser.tabs.create({ | ||
'url': MANAGEMENT_URL | ||
}); | ||
}); | ||
|
||
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { | ||
if (REGEX.test(tab.url)) { | ||
browser.pageAction.show(tabId); | ||
} else { | ||
chrome.pageAction.hide(tabId); | ||
} | ||
}); | ||
|
||
|
||
/* Runtime Messages */ | ||
browser.notifications.onClicked.addListener(OnNotificationClicked); | ||
browser.pageAction.onClicked.addListener(() => OpenOrShowURL(browser.runtime.getURL('pages/manage.html'))); | ||
browser.runtime.onInstalled.addListener(OnInstalled); | ||
browser.runtime.onMessage.addListener(OnRuntimeMessage); | ||
browser.storage.onChanged.addListener(OnStorageChanged); | ||
browser.tabs.onUpdated.addListener(OnTabUpdate); | ||
|
||
|
||
/* Storage */ | ||
browser.storage.onChanged.addListener((changes, areaName) => { | ||
switch (areaName) { | ||
case 'local': | ||
OnLocalStorageChanged(changes); | ||
break; | ||
} | ||
}); | ||
|
||
|
||
/* Context Menus */ | ||
try { | ||
MENUS.forEach(menu => browser.contextMenus.remove(menu.id).finally(browser.contextMenus.create(menu))); | ||
browser.contextMenus.onClicked.addListener(OnMenuClicked); | ||
} catch (ex) { | ||
console.error('Failed to setup context menus', ex); | ||
} | ||
|
||
try { | ||
browser.contextMenus.onShown.addListener(OnMenuShown); | ||
} catch (ex) { | ||
// chrome doesn't support the onShown event, but we don't use it for major functionality, so just ignore it | ||
void(ex); | ||
} | ||
InitMenus(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { OpenOrShowURL } from './tabs'; | ||
|
||
import { TAG_FILTERS_MIGRATED } from '../constants/notifications'; | ||
|
||
/** | ||
* Event handler for notification clicks | ||
* @param {string} notificationId the ID of the clicked notification | ||
*/ | ||
export const OnNotificationClicked = (notificationId) => { | ||
switch (notificationId) { | ||
case TAG_FILTERS_MIGRATED: | ||
OpenOrShowURL(browser.runtime.getURL('pages/manage.html#/keywords')); | ||
break; | ||
} | ||
|
||
return browser.notifications.clear(notificationId); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import semverLT from 'semver/functions/lt'; | ||
import semverValid from 'semver/functions/valid'; | ||
|
||
import { GetCategories } from './categories'; | ||
import { ImportFilters } from './filters'; | ||
|
||
import { TAG_FILTERS_MIGRATED } from '../constants/notifications'; | ||
|
||
export const OnInstalled = async (details) => { | ||
const { previousVersion } = details; | ||
|
||
if (semverValid(previousVersion) && semverLT(previousVersion, '6.0.0')) { | ||
const data = await browser.storage.local.get('tags'); | ||
if (data?.tags) { | ||
console.warn('Converting tag filters to keyword filters'); | ||
await ImportFilters(data); | ||
|
||
// TODO: is it appropriate to delete tag filters from previous versions? | ||
// await browser.storage.local.remove('tags'); | ||
|
||
browser.notifications.create(TAG_FILTERS_MIGRATED, { | ||
'type': 'basic', | ||
'iconUrl': browser.extension.getURL('images/icon-64.png'), | ||
'title': browser.i18n.getMessage('ExtensionName'), | ||
'message': browser.i18n.getMessage('TagFiltersMigratedNotificationMessage'), | ||
}); | ||
} | ||
} | ||
|
||
// fetch and store the latest category paths | ||
await GetCategories(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { REGEX } from '../constants/url'; | ||
|
||
/** | ||
* Focuses the first tab matching the specified URL or opens it in a new tab | ||
* @param {string} url the URL to focus/show | ||
*/ | ||
export const OpenOrShowURL = async (url) => { | ||
const tabs = await browser.tabs.query({ | ||
'currentWindow': true, | ||
'url': url | ||
}); | ||
|
||
if (tabs.length) { | ||
return browser.tabs.update(tabs[0].id, { | ||
'active': true | ||
}); | ||
} | ||
|
||
return browser.tabs.create({ | ||
'url': url | ||
}); | ||
}; | ||
|
||
/** | ||
* Event handler for tab updates | ||
* @param {number} tabId the ID of the tab that was updated | ||
* @param {object} changeInfo properties about the tab's changes | ||
* @param {tab} tab the new state of the tab | ||
*/ | ||
export const OnTabUpdate = (tabId, changeInfo, tab) => { | ||
if (REGEX.test(tab.url)) { | ||
browser.pageAction.show(tabId); | ||
} else { | ||
browser.pageAction.hide(tabId); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const TAG_FILTERS_MIGRATED = 'tag-filters-converted'; |