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

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

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

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