use promise and show errors

jh-changes
Shin'ya Ueoka 7 years ago
parent 68753b3660
commit e0bff54a82
  1. 59
      src/background/index.js
  2. 28
      src/background/tabs.js
  3. 6
      src/background/zooms.js
  4. 5
      src/content/index.js

@ -5,52 +5,46 @@ import KeyQueue from './key-queue';
const queue = new KeyQueue();
const keyPressHandle = (request, sender, sendResponse) => {
const keyPressHandle = (request, sender) => {
let action = queue.push({
code: request.code,
ctrl: request.ctrl
});
if (!action) {
return;
return Promise.resolve();
}
if (actions.isBackgroundAction(action[0])) {
doBackgroundAction(sender, action);
return doBackgroundAction(sender, action);
} else if (actions.isContentAction(action[0])) {
sendResponse({
return Promise.resolve({
type: 'response.action',
action: action
});
}
return Promise.resolve();
};
const doBackgroundAction = (sender, action) => {
switch(action[0]) {
case actions.TABS_CLOSE:
tabs.closeTab(sender.tab.id);
break;
return tabs.closeTab(sender.tab.id);
case actions.TABS_REOPEN:
tabs.reopenTab();
break;
return tabs.reopenTab();
case actions.TABS_PREV:
tabs.selectPrevTab(sender.tab.index, actions[1] || 1);
break;
return tabs.selectPrevTab(sender.tab.index, actions[1] || 1);
case actions.TABS_NEXT:
tabs.selectNextTab(sender.tab.index, actions[1] || 1);
break;
return tabs.selectNextTab(sender.tab.index, actions[1] || 1);
case actions.TABS_RELOAD:
tabs.reload(sender.tab, actions[1] || false);
break;
return tabs.reload(sender.tab, actions[1] || false);
case actions.ZOOM_IN:
zooms.zoomIn();
break;
return zooms.zoomIn();
case actions.ZOOM_OUT:
zooms.zoomOut();
break;
return zooms.zoomOut();
case actions.ZOOM_NEUTRAL:
zooms.neutral();
break;
return zooms.neutral();
}
return Promise.resolve();
}
const normalizeUrl = (string) => {
@ -63,10 +57,10 @@ const normalizeUrl = (string) => {
const cmdBuffer = (arg) => {
if (isNaN(arg)) {
tabs.selectByKeyword(arg);
return tabs.selectByKeyword(arg);
} else {
let index = parseInt(arg, 10) - 1;
tabs.selectAt(index);
return tabs.selectAt(index);
}
}
@ -74,28 +68,25 @@ const cmdEnterHandle = (request, sender) => {
let words = request.text.split(' ').filter((s) => s.length > 0);
switch (words[0]) {
case 'open':
browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
return;
return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
case 'tabopen':
browser.tabs.create({ url: normalizeUrl(words[1]) });
return;
return browser.tabs.create({ url: normalizeUrl(words[1]) });
case 'b':
case 'buffer':
cmdBuffer(words[1]);
return;
return cmdBuffer(words[1]);
}
throw new Error(words[0] + ' command is not defined');
};
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
browser.runtime.onMessage.addListener((request, sender) => {
switch (request.type) {
case 'event.keypress':
keyPressHandle(request, sender, sendResponse);
break;
return keyPressHandle(request, sender);
case 'event.cmd.enter':
cmdEnterHandle(request, sender, sendResponse);
break;
return cmdEnterHandle(request, sender);
case 'event.cmd.suggest':
// TODO make suggestion and return via sendResponse
// TODO make suggestion and return
break;
}
return Promise.resolve();
});

@ -1,9 +1,9 @@
const closeTab = (id) => {
browser.tabs.remove(id);
return browser.tabs.remove(id);
};
const reopenTab = () => {
browser.sessions.getRecentlyClosed({
return browser.sessions.getRecentlyClosed({
maxResults: 1
}).then((sessions) => {
if (sessions.length === 0) {
@ -11,15 +11,15 @@ const reopenTab = () => {
}
let session = sessions[0];
if (session.tab) {
browser.sessions.restore(session.tab.sessionId);
return browser.sessions.restore(session.tab.sessionId);
} else {
browser.sessions.restore(session.window.sessionId);
return browser.sessions.restore(session.window.sessionId);
}
});
};
const selectAt = (index) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) {
return;
}
@ -27,12 +27,12 @@ const selectAt = (index) => {
throw new RangeError(`tab ${index} does not exist`)
}
let id = tabs[index].id;
chrome.tabs.update(id, { active: true })
return browser.tabs.update(id, { active: true })
});
};
const selectByKeyword = (keyword) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword)
})
@ -43,34 +43,34 @@ const selectByKeyword = (keyword) => {
throw new RangeError('More than one match for ' + keyword);
}
chrome.tabs.update(matched[0].id, { active: true });
})
return browser.tabs.update(matched[0].id, { active: true });
});
}
const selectPrevTab = (current, count) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) {
return;
}
let select = (current - count) % tabs.length
let id = tabs[select].id;
chrome.tabs.update(id, { active: true })
return browser.tabs.update(id, { active: true })
});
};
const selectNextTab = (current, count) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
return browser.tabs.query({ currentWindow: true }, (tabs) => {
if (tabs.length < 2) {
return;
}
let select = (current + count + tabs.length) % tabs.length
let id = tabs[select].id;
chrome.tabs.update(id, { active: true })
return browser.tabs.update(id, { active: true })
});
};
const reload = (current, cache) => {
browser.tabs.reload(
return browser.tabs.reload(
current.id,
{ bypassCache: cache }
);

@ -10,7 +10,7 @@ const ZOOM_SETTINGS = [
];
const zoomIn = (tabId = undefined) => {
browser.tabs.getZoom(tabId).then((factor) => {
return browser.tabs.getZoom(tabId).then((factor) => {
for (let f of ZOOM_SETTINGS) {
if (f > factor) {
browser.tabs.setZoom(tabId, f);
@ -21,7 +21,7 @@ const zoomIn = (tabId = undefined) => {
};
const zoomOut = (tabId = undefined) => {
browser.tabs.getZoom(tabId).then((factor) => {
return browser.tabs.getZoom(tabId).then((factor) => {
for (let f of [].concat(ZOOM_SETTINGS).reverse()) {
if (f < factor) {
browser.tabs.setZoom(tabId, f);
@ -32,7 +32,7 @@ const zoomOut = (tabId = undefined) => {
};
const neutral = (tabId = undefined) => {
browser.tabs.setZoom(tabId, 1);
return browser.tabs.setZoom(tabId, 1);
};
export { zoomIn, zoomOut, neutral };

@ -83,6 +83,7 @@ window.addEventListener("keypress", (e) => {
browser.runtime.sendMessage(request)
.then(handleResponse)
.catch((err) => {
vvConsole.showError(err.message);
console.log(`Vim Vixen: ${err}`);
});
});
@ -96,12 +97,16 @@ messages.receive(window, (message) => {
browser.runtime.sendMessage({
type: 'event.cmd.enter',
text: message.value
}).catch((e) => {
vvConsole.showError(e.message);
});
break;
case 'vimvixen.command.change':
browser.runtime.sendMessage({
type: 'event.cmd.suggest',
text: message.value
}).catch((e) => {
vvConsole.showError(e.message);
});
break;
default: