Skip to content

Commit

Permalink
Refactor messages to always have a .action property
Browse files Browse the repository at this point in the history
This action describes what the message is telling the background to do,
or what is happening.

The refactor simplifies the logic of handleMessage() as messages now are
always objects. In particular, put the toggle and options together as
they both need to handle the adding and removing of listeners.
  • Loading branch information
Cimbali committed Jun 6, 2018
1 parent 6a45df9 commit 1b84e22
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
51 changes: 27 additions & 24 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function cleanRedirectHeaders(details)
if (cleanDest == dest)
return {};

handleMessage({ url: cleanDest, orig: dest, type: 'header' });
handleMessage({ action: 'notify', url: cleanDest, orig: dest, type: 'header' });
return {redirectUrl: cleanDest};
}

Expand All @@ -73,15 +73,15 @@ function onRequest(details)
// Prevent frame/script/etc. redirections back to top-level document (see 182e58e)
if (new URL(cleanDest).domain == new URL(curLink).domain && details.type != 'main_frame')
{
handleMessage({ url: cleanDest, orig: dest, dropped: true, type: 'request' });
handleMessage({ action: 'notify', url: cleanDest, orig: dest, dropped: true, type: 'request' });
return {cancel: true};
}

// Allowed requests when destination is self, to protect against infinite loops (see 42106fd).
else if (cleanDest == curLink)
return {}

handleMessage({ url: cleanDest, orig: dest });
handleMessage({ action: 'notify', url: cleanDest, orig: dest });
return {redirectUrl: cleanDest};
}

Expand All @@ -90,20 +90,12 @@ function handleMessage(message, sender)
{
log('received message :', JSON.stringify(message))

if (message == 'get_cleaned_list')
if (message.action == 'cleaned list')
{
return Promise.resolve(historyCleanedLinks);
}

else if (message == 'toggle')
{
prefValues.enabled = !prefValues.enabled;

setIcon(prefValues.enabled ? icon_default : icon_disabled);
return browser.storage.local.set({configuration: serializeOptions()}).then(() => handleMessage({options: Date.now()}))
}

else if ('url' in message)
else if (message.action == 'notify')
{
var p;
if (prefValues.notifications)
Expand All @@ -118,7 +110,7 @@ function handleMessage(message, sender)
browser.alarms.create('clearNotification:' + message.url, {when: Date.now() + prefValues.notiftime});
}
else
p = Promise.resolve(null)
p = Promise.resolve(null);

if (prefValues.cltrack)
{
Expand All @@ -133,17 +125,17 @@ function handleMessage(message, sender)
return p;
}

else if ('openUrl' in message)
else if (message.action == 'open url')
{
if (message.target == new_window)
return browser.windows.create({ url: message.openUrl });
return browser.windows.create({ url: message.link });
else if (message.target == new_tab)
return browser.tabs.create({ url: message.openUrl, active: prefValues.switchToTab, openerTabId: sender.tab.id });
return browser.tabs.create({ url: message.link, active: prefValues.switchToTab, openerTabId: sender.tab.id });
else
return browser.tabs.update({ url: message.openUrl });
return browser.tabs.update({ url: message.link });
}

else if ('whitelist' in message)
else if (message.action == 'whitelist')
{
var entry = historyCleanedLinks.splice(message.whitelist, 1)[0];
var host = (new URL(entry.orig)).hostname;
Expand All @@ -156,10 +148,21 @@ function handleMessage(message, sender)
return Promise.resolve(null)
}

else if ('options' in message)
else if (message.action == 'options' || message.action == 'toggle')
{
var oldPrefValues = Object.assign({}, prefValues);
return loadOptions().then(() =>

if (message.action == 'toggle')
{
prefValues.enabled = !prefValues.enabled;

setIcon(prefValues.enabled ? icon_default : icon_disabled);
var p = browser.storage.local.set({configuration: serializeOptions()});
}
else
var p = loadOptions();

return p.then(() =>
{
if (!prefValues.cltrack)
historyCleanedLinks.splice(0, historyCleanedLinks.length);
Expand Down Expand Up @@ -196,17 +199,17 @@ function handleMessage(message, sender)
browser.webRequest.onBeforeRequest.removeListener(onRequest);

browser.tabs.query({}).then(tabs => tabs.forEach(tab =>
browser.tabs.sendMessage(tab.id, 'reloadOptions').catch(() => {})
browser.tabs.sendMessage(tab.id, {action: 'reload options'}).catch(() => {})
));
})
}

else if ('rightClickLink' in message)
else if (message.action == 'right click')
{
return new Promise((resolve, rejecte) =>
{
lastRightClick = {
textLink: message.rightClickLink,
textLink: message.link,
reply: resolve
}
})
Expand Down
10 changes: 6 additions & 4 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function eventDoClick(url, node, evt)
}

browser.runtime.sendMessage({
openUrl: url,
action: 'open url',
link: url,
target: (evt.ctrlKey || evt.button == 1) ? new_tab : (evt.shiftKey ? new_window : same_tab)
}).catch(() =>
{
Expand Down Expand Up @@ -113,7 +114,7 @@ function onClick(evt)
{
// NB: always send out the textLink here, even if prefValues.textcl is false, to get the reply
// and potentially copy to clipboard
browser.runtime.sendMessage({rightClickLink: textLink}).then(reply => copyToClipboard(reply) );
browser.runtime.sendMessage({action: 'right click', link: textLink}).then(reply => copyToClipboard(reply) );
}
else
{
Expand Down Expand Up @@ -149,12 +150,13 @@ function onClick(evt)
highlightLink(node);

// instead of blinking the URL bar, tell the background to show a notification.
browser.runtime.sendMessage({url: cleanedLink, orig: link, type: 'clicked'});
browser.runtime.sendMessage({action: 'notify', url: cleanedLink, orig: link, type: 'clicked'});
}
}
else if(!textLink && node.hasAttribute(attr_cleaned_link))
{
browser.runtime.sendMessage({
action: 'notify',
url: evt.target.href,
orig: evt.target.getAttribute(attr_cleaned_link),
type: 'pre-cleaned'
Expand All @@ -172,7 +174,7 @@ loadOptions().then(() =>

browser.runtime.onMessage.addListener(message =>
{
if (message == 'reloadOptions')
if (message.action == 'reload options')
{
var had_onclick = prefValues.enabled;
return loadOptions().then(() =>
Expand Down
2 changes: 1 addition & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function save_options()

browser.storage.local.set({configuration: prefs}).then(() =>
{
browser.runtime.sendMessage({options: Date.now()})
browser.runtime.sendMessage({action: 'options'});
loadOptions();
});
}
Expand Down
6 changes: 3 additions & 3 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function populate_popup()

add_option(_('bootstrap_listheader_original'), _('bootstrap_listheader_cleaned'), []);

browser.runtime.sendMessage('get_cleaned_list').then(response =>
browser.runtime.sendMessage({action: 'cleaned list'}).then(response =>
{
response.forEach(clean => add_option(clean.orig, clean.url,
'dropped' in clean ? ['dropped', clean.type] : [clean.type]));
Expand All @@ -93,14 +93,14 @@ function populate_popup()
{
prefValues.enabled = !prefValues.enabled;
set_toggle_text();
browser.runtime.sendMessage('toggle');
browser.runtime.sendMessage({action: 'toggle'});
}

document.querySelector('#whitelist').onclick = () =>
{
var select = document.querySelector('select');
var id = parseInt(select.value);
browser.runtime.sendMessage({whitelist: id}).then(() =>
browser.runtime.sendMessage({action: 'whitelist', item: id}).then(() =>
// remove selected element, and renumber higher-ordered ones
select.querySelectorAll('option').forEach(opt =>
{
Expand Down

0 comments on commit 1b84e22

Please sign in to comment.