Skip to content

Commit

Permalink
Add JS handler for opening adjacent tabs
Browse files Browse the repository at this point in the history
Resolves #94

If `opts.openFeed` is "next", try to get the current tab index and pass to tab create method with next index value (`tab.index + 1`)
  • Loading branch information
shgysk8zer0 committed Aug 4, 2018
1 parent 7e7d701 commit 4071522
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
37 changes: 31 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ICONS = {
disabled: 'icons/subscribe-disabled.svg',
};

function openFeed({feed, target = 'current', service = 'rss'} = {}) {
function openFeed({feed, target = 'current', service = 'rss', index = undefined} = {}) {
let url = null;
switch (service) {
case 'feedly':
Expand All @@ -44,7 +44,16 @@ function openFeed({feed, target = 'current', service = 'rss'} = {}) {
break;

case 'tab':
browser.tabs.create({url: url.toString()});
browser.tabs.create({
url: url.toString(),
});
break;

case 'next':
browser.tabs.create({
url: url.toString(),
index,
});
break;

case 'current':
Expand All @@ -58,13 +67,17 @@ function openFeed({feed, target = 'current', service = 'rss'} = {}) {

async function clickHandler(tab) {
const opts = await storage.get(['openFeed', 'service']);
const index = opts.openFeed === 'next' ? tab.index + 1 : null;

try {
openFeed({
feed: TABS[tab.id][0].href,
target: opts.openFeed,
service: opts.service,
index,
});
} catch (err) {
/* eslint no-console: 0 */
console.error(err);
}
}
Expand All @@ -76,7 +89,10 @@ function removeHandler(tabId) {
async function updatePageAction(tab, links) {
if (links.length > 0) {
const opts = await storage.get('icon');
TABS[tab.id] = links;
TABS[tab.id] = links.map(link => {
link.tabId = tab.id;
return link;
});

if (! ICONS.hasOwnProperty(opts.icon)) {
opts.icon = defaultOpts.icon;
Expand Down Expand Up @@ -108,13 +124,20 @@ async function updatePageAction(tab, links) {
}
}

function messageHandler(msg, sender) {
async function messageHandler(msg, sender) {
switch (msg.type) {
case 'feeds':
updatePageAction(sender.tab, msg.links);
break;

case 'openFeed':
if (msg.params.target === 'next') {
const tabs = await browser.tabs.query({active: true, currentWindow: true});
if (tabs.length === 1) {
const tab = tabs[0];
msg.params.index = tab.index + 1;
}
}
openFeed(msg.params);
break;

Expand All @@ -136,7 +159,7 @@ function scanPage(tab) {
title: browser.i18n.getMessage('extensionNA'),
});

browser.tabs.sendMessage(tabId, {type: 'scan'});
browser.tabs.sendMessage(tabId, {type: 'scan'}).catch(() => {});
}

async function refreshAllTabsPageAction() {
Expand Down Expand Up @@ -176,7 +199,9 @@ async function optChange(opts) {

async function updateHandler(update) {
if (update.temporary) {
storage.get().then(opts => console.log({update, opts}));
/* eslint no-console: 0 */
const opts = await storage.get();
console.log({update, opts});
}

if (update.reason === 'install') {
Expand Down
4 changes: 3 additions & 1 deletion js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const types = {
function $(query, base = document) {
return [...base.querySelectorAll(query)];
}

async function init() {
const opts = await storage.get([
'template',
Expand Down Expand Up @@ -42,7 +43,6 @@ async function init() {
if (opts.hasOwnProperty('bgImage')) {
document.documentElement.style.setProperty('--feed-bg-image', `url(${opts.bgImage})`);
}
console.log(document.documentElement.style);

try {
links.forEach(link => {
Expand All @@ -59,6 +59,7 @@ async function init() {
container.appendChild(feed);
});
} catch (error) {
/* eslint no-console: 0 */
console.error(error);
}
}
Expand All @@ -67,6 +68,7 @@ async function openFeed(click) {
click.preventDefault();

const opts = await storage.get(['openFeed', 'service']);

browser.runtime.sendMessage({
type: 'openFeed',
params: {
Expand Down

0 comments on commit 4071522

Please sign in to comment.