Skip to content

Commit

Permalink
reorganize background script logic; show notification after convertin…
Browse files Browse the repository at this point in the history
…g tag filters to keyword filters
  • Loading branch information
rthaut committed May 6, 2020
1 parent f902c40 commit 1a04221
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 82 deletions.
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,8 @@
"content" : "$1"
}
}
},
"TagFiltersMigratedNotificationMessage": {
"message": "Your old tag filters have been converted to new keyword filters automatically.\n\nClick this notification to view your keyword filters."
}
}
91 changes: 11 additions & 80 deletions app/scripts/background.js
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();
19 changes: 19 additions & 0 deletions app/scripts/background/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ export const MENUS = [
}
];

/**
* Initializes menus and event handlers
*/
export const InitMenus = () => {
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);
}
};

/**
* Event handler for when a menu item is clicked
* @param {object} info menu info
Expand Down
2 changes: 0 additions & 2 deletions app/scripts/background/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ export const OnRuntimeMessage = (message) => {
export const SendMessageToAllTabs = async (action, data) => {
const tabs = await browser.tabs.query({ 'url': '*://*.deviantart.com/*' });
for (const tab of tabs) {
console.debug(tab);
browser.tabs.sendMessage(tab.id, { action, data });
}
};

export const SendMessageToScriptPage = async (page, action, data) => {
const tabs = await browser.tabs.query({ 'url': browser.runtime.getURL(page) });
for (const tab of tabs) {
console.debug(tab);
browser.tabs.sendMessage(tab.id, { action, data });
}
};
17 changes: 17 additions & 0 deletions app/scripts/background/notifications.js
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);
};
32 changes: 32 additions & 0 deletions app/scripts/background/runtime.js
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();
};
17 changes: 17 additions & 0 deletions app/scripts/background/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ export const MONITORED_STORAGE_KEYS = [
'users',
];

/**
* Event handler for all storage changes
* @param {object} changes the storage changes
* @param {string} areaName the name of the changed storage area
*/
export const OnStorageChanged = (changes, areaName) => {
switch (areaName) {
case 'local':
OnLocalStorageChanged(changes);
break;
}
};

/**
* Event handler for local storage changes
* @param {object} changes the storage changes
*/
export const OnLocalStorageChanged = (changes) => {
console.time('OnLocalStorageChanged()');
for (const key of Object.keys(changes)) {
Expand Down
36 changes: 36 additions & 0 deletions app/scripts/background/tabs.js
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);
}
};
1 change: 1 addition & 0 deletions app/scripts/constants/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TAG_FILTERS_MIGRATED = 'tag-filters-converted';

0 comments on commit 1a04221

Please sign in to comment.