aboutsummaryrefslogtreecommitdiff
path: root/src/background
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-07-13 22:36:56 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2018-07-13 22:36:56 +0900
commit944dea59199fa03f77e0e7c0d3c02acf8ccb458f (patch)
tree5f44471cdb32376a8c93e6f6eba738180f08c96f /src/background
parent77b4e807e2a8b3e7ddb5f042719a34962a31b1c4 (diff)
parent28bfa3ac8124d3453cd539db26da4f4703e783df (diff)
Merge remote-tracking branch 'origin/master' into greenkeeper/css-loader-1.0.0
Diffstat (limited to 'src/background')
-rw-r--r--src/background/actions/command.js68
-rw-r--r--src/background/actions/console.js41
-rw-r--r--src/background/actions/tab.js11
-rw-r--r--src/background/components/operation.js94
-rw-r--r--src/background/index.js19
-rw-r--r--src/background/reducers/index.js23
-rw-r--r--src/background/shared/completions/index.js82
-rw-r--r--src/background/shared/versions/index.js38
-rw-r--r--src/background/shared/versions/release-notes.js8
-rw-r--r--src/background/shared/versions/storage.js10
10 files changed, 276 insertions, 118 deletions
diff --git a/src/background/actions/command.js b/src/background/actions/command.js
index fb8ff98..a7f619b 100644
--- a/src/background/actions/command.js
+++ b/src/background/actions/command.js
@@ -1,5 +1,5 @@
-import messages from 'shared/messages';
import actions from '../actions';
+import * as consoleActions from './console';
import * as tabs from '../shared/tabs';
import * as bookmarks from '../shared/bookmarks';
import * as parsers from 'shared/commands/parsers';
@@ -39,7 +39,7 @@ const winopenCommand = (url) => {
const bufferCommand = async(keywords) => {
if (keywords.length === 0) {
- return Promise.resolve([]);
+ return;
}
let keywordsStr = keywords.join(' ');
let got = await browser.tabs.query({
@@ -57,24 +57,18 @@ const bufferCommand = async(keywords) => {
const addbookmarkCommand = async(tab, args) => {
if (!args[0]) {
- return;
+ return { type: '' };
}
let item = await bookmarks.create(args.join(' '), tab.url);
if (!item) {
- return browser.tabs.sendMessage(tab.id, {
- type: messages.CONSOLE_SHOW_ERROR,
- text: 'Could not create a bookmark',
- });
+ return consoleActions.error(tab, 'Could not create a bookmark');
}
- return browser.tabs.sendMessage(tab.id, {
- type: messages.CONSOLE_SHOW_INFO,
- text: 'Saved current page: ' + item.url,
- });
+ return consoleActions.info(tab, 'Saved current page: ' + item.url);
};
const setCommand = (args) => {
if (!args[0]) {
- return Promise.resolve();
+ return { type: '' };
}
let [name, value] = parsers.parseSetOption(args[0], properties.types);
@@ -85,49 +79,69 @@ const setCommand = (args) => {
};
};
-// eslint-disable-next-line complexity
-const exec = (tab, line, settings) => {
+// eslint-disable-next-line complexity, max-lines-per-function
+const doExec = async(tab, line, settings) => {
let [name, args] = parsers.parseCommandLine(line);
switch (name) {
case 'o':
case 'open':
- return openCommand(parsers.normalizeUrl(args, settings.search));
+ await openCommand(parsers.normalizeUrl(args, settings.search));
+ break;
case 't':
case 'tabopen':
- return tabopenCommand(parsers.normalizeUrl(args, settings.search));
+ await tabopenCommand(parsers.normalizeUrl(args, settings.search));
+ break;
case 'w':
case 'winopen':
- return winopenCommand(parsers.normalizeUrl(args, settings.search));
+ await winopenCommand(parsers.normalizeUrl(args, settings.search));
+ break;
case 'b':
case 'buffer':
- return bufferCommand(args);
+ await bufferCommand(args);
+ break;
case 'bd':
case 'bdel':
case 'bdelete':
- return tabs.closeTabByKeywords(args.join(' '));
+ await tabs.closeTabByKeywords(args.join(' '));
+ break;
case 'bd!':
case 'bdel!':
case 'bdelete!':
- return tabs.closeTabByKeywordsForce(args.join(' '));
+ await tabs.closeTabByKeywordsForce(args.join(' '));
+ break;
case 'bdeletes':
- return tabs.closeTabsByKeywords(args.join(' '));
+ await tabs.closeTabsByKeywords(args.join(' '));
+ break;
case 'bdeletes!':
- return tabs.closeTabsByKeywordsForce(args.join(' '));
+ await tabs.closeTabsByKeywordsForce(args.join(' '));
+ break;
case 'addbookmark':
return addbookmarkCommand(tab, args);
case 'set':
return setCommand(args);
case 'q':
case 'quit':
- return tabcloseCommand();
+ await tabcloseCommand();
+ break;
case 'qa':
case 'quitall':
- return tabcloseAllCommand()
- case '':
- return Promise.resolve();
+ await tabcloseAllCommand();
+ break;
+ default:
+ return consoleActions.error(tab, name + ' command is not defined');
+ }
+ return { type: '' };
+};
+
+// eslint-disable-next-line complexity
+const exec = async(tab, line, settings) => {
+ try {
+ let action = await doExec(tab, line, settings);
+ return action;
+ } catch (e) {
+ return consoleActions.error(tab, e.toString());
}
- throw new Error(name + ' command is not defined');
};
export { exec };
diff --git a/src/background/actions/console.js b/src/background/actions/console.js
new file mode 100644
index 0000000..d385b2d
--- /dev/null
+++ b/src/background/actions/console.js
@@ -0,0 +1,41 @@
+import messages from 'shared/messages';
+
+const error = async(tab, text) => {
+ await browser.tabs.sendMessage(tab.id, {
+ type: messages.CONSOLE_SHOW_ERROR,
+ text,
+ });
+ return { type: '' };
+};
+
+const info = async(tab, text) => {
+ await browser.tabs.sendMessage(tab.id, {
+ type: messages.CONSOLE_SHOW_INFO,
+ text,
+ });
+ return { type: '' };
+};
+
+const showCommand = async(tab, command) => {
+ await browser.tabs.sendMessage(tab.id, {
+ type: messages.CONSOLE_SHOW_COMMAND,
+ command,
+ });
+ return { type: '' };
+};
+
+const showFind = async(tab) => {
+ await browser.tabs.sendMessage(tab.id, {
+ type: messages.CONSOLE_SHOW_FIND
+ });
+ return { type: '' };
+};
+
+const hide = async(tab) => {
+ await browser.tabs.sendMessage(tab.id, {
+ type: messages.CONSOLE_HIDE,
+ });
+ return { type: '' };
+};
+
+export { error, info, showCommand, showFind, hide };
diff --git a/src/background/actions/tab.js b/src/background/actions/tab.js
index 5cf1e8c..0f32a90 100644
--- a/src/background/actions/tab.js
+++ b/src/background/actions/tab.js
@@ -4,21 +4,24 @@ const openNewTab = async(
url, openerTabId, background = false, adjacent = false
) => {
if (!adjacent) {
- return browser.tabs.create({ url, active: !background });
+ await browser.tabs.create({ url, active: !background });
+ return { type: '' };
}
let tabs = await browser.tabs.query({
active: true, currentWindow: true
});
- return browser.tabs.create({
+ await browser.tabs.create({
url,
openerTabId,
active: !background,
index: tabs[0].index + 1
});
+ return { type: '' };
};
-const openToTab = (url, tab) => {
- return browser.tabs.update(tab.id, { url: url });
+const openToTab = async(url, tab) => {
+ await browser.tabs.update(tab.id, { url: url });
+ return { type: '' };
};
const selected = (tabId) => {
diff --git a/src/background/components/operation.js b/src/background/components/operation.js
index 465baf0..ce93270 100644
--- a/src/background/components/operation.js
+++ b/src/background/components/operation.js
@@ -2,6 +2,7 @@ import messages from 'shared/messages';
import operations from 'shared/operations';
import * as tabs from '../shared//tabs';
import * as zooms from '../shared/zooms';
+import * as consoleActions from '../actions/console';
export default class BackgroundComponent {
constructor(store) {
@@ -23,101 +24,104 @@ export default class BackgroundComponent {
switch (message.type) {
case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
- this.exec(message.operation, sender.tab),
- sender);
+ this.exec(message.operation, sender.tab));
}
}
// eslint-disable-next-line complexity, max-lines-per-function
- exec(operation, tab) {
+ async exec(operation, tab) {
let tabState = this.store.getState().tab;
switch (operation.type) {
case operations.TAB_CLOSE:
- return tabs.closeTab(tab.id);
+ await tabs.closeTab(tab.id);
+ break;
case operations.TAB_CLOSE_FORCE:
- return tabs.closeTabForce(tab.id);
+ await tabs.closeTabForce(tab.id);
+ break;
case operations.TAB_REOPEN:
- return tabs.reopenTab();
+ await tabs.reopenTab();
+ break;
case operations.TAB_PREV:
- return tabs.selectPrevTab(tab.index, operation.count);
+ await tabs.selectPrevTab(tab.index, operation.count);
+ break;
case operations.TAB_NEXT:
- return tabs.selectNextTab(tab.index, operation.count);
+ await tabs.selectNextTab(tab.index, operation.count);
+ break;
case operations.TAB_FIRST:
- return tabs.selectFirstTab();
+ await tabs.selectFirstTab();
+ break;
case operations.TAB_LAST:
- return tabs.selectLastTab();
+ await tabs.selectLastTab();
+ break;
case operations.TAB_PREV_SEL:
if (tabState.previousSelected > 0) {
- return tabs.selectTab(tabState.previousSelected);
+ await tabs.selectTab(tabState.previousSelected);
}
break;
case operations.TAB_RELOAD:
- return tabs.reload(tab, operation.cache);
+ await tabs.reload(tab, operation.cache);
+ break;
case operations.TAB_PIN:
- return tabs.updateTabPinned(tab, true);
+ await tabs.updateTabPinned(tab, true);
+ break;
case operations.TAB_UNPIN:
- return tabs.updateTabPinned(tab, false);
+ await tabs.updateTabPinned(tab, false);
+ break;
case operations.TAB_TOGGLE_PINNED:
- return tabs.toggleTabPinned(tab);
+ await tabs.toggleTabPinned(tab);
+ break;
case operations.TAB_DUPLICATE:
- return tabs.duplicate(tab.id);
+ await tabs.duplicate(tab.id);
+ break;
case operations.ZOOM_IN:
- return zooms.zoomIn();
+ await zooms.zoomIn();
+ break;
case operations.ZOOM_OUT:
- return zooms.zoomOut();
+ await zooms.zoomOut();
+ break;
case operations.ZOOM_NEUTRAL:
- return zooms.neutral();
+ await zooms.neutral();
+ break;
case operations.COMMAND_SHOW:
- return this.sendConsoleShowCommand(tab, '');
+ return consoleActions.showCommand(tab, '');
case operations.COMMAND_SHOW_OPEN:
if (operation.alter) {
// alter url
- return this.sendConsoleShowCommand(tab, 'open ' + tab.url);
+ return consoleActions.showCommand(tab, 'open ' + tab.url);
}
- return this.sendConsoleShowCommand(tab, 'open ');
+ return consoleActions.showCommand(tab, 'open ');
case operations.COMMAND_SHOW_TABOPEN:
if (operation.alter) {
// alter url
- return this.sendConsoleShowCommand(tab, 'tabopen ' + tab.url);
+ return consoleActions.showCommand(tab, 'tabopen ' + tab.url);
}
- return this.sendConsoleShowCommand(tab, 'tabopen ');
+ return consoleActions.showCommand(tab, 'tabopen ');
case operations.COMMAND_SHOW_WINOPEN:
if (operation.alter) {
// alter url
- return this.sendConsoleShowCommand(tab, 'winopen ' + tab.url);
+ return consoleActions.showCommand(tab, 'winopen ' + tab.url);
}
- return this.sendConsoleShowCommand(tab, 'winopen ');
+ return consoleActions.showCommand(tab, 'winopen ');
case operations.COMMAND_SHOW_BUFFER:
- return this.sendConsoleShowCommand(tab, 'buffer ');
+ return consoleActions.showCommand(tab, 'buffer ');
case operations.COMMAND_SHOW_ADDBOOKMARK:
if (operation.alter) {
- return this.sendConsoleShowCommand(tab, 'addbookmark ' + tab.title);
+ return consoleActions.showCommand(tab, 'addbookmark ' + tab.title);
}
- return this.sendConsoleShowCommand(tab, 'addbookmark ');
+ return consoleActions.showCommand(tab, 'addbookmark ');
case operations.FIND_START:
- return browser.tabs.sendMessage(tab.id, {
- type: messages.CONSOLE_SHOW_FIND
- });
+ return consoleActions.showFind(tab);
case operations.CANCEL:
- return browser.tabs.sendMessage(tab.id, {
- type: messages.CONSOLE_HIDE,
- });
+ return consoleActions.hide(tab);
case operations.PAGE_SOURCE:
- return browser.tabs.create({
+ await browser.tabs.create({
url: 'view-source:' + tab.url,
index: tab.index + 1,
openerTabId: tab.id,
});
- default:
- return Promise.resolve();
+ break;
}
- }
-
- sendConsoleShowCommand(tab, command) {
- return browser.tabs.sendMessage(tab.id, {
- type: messages.CONSOLE_SHOW_COMMAND,
- command,
- });
+ return { type: '' };
}
}
diff --git a/src/background/index.js b/src/background/index.js
index 02de53f..1e4c078 100644
--- a/src/background/index.js
+++ b/src/background/index.js
@@ -1,22 +1,17 @@
import * as settingActions from 'background/actions/setting';
-import messages from 'shared/messages';
import BackgroundComponent from 'background/components/background';
import OperationComponent from 'background/components/operation';
import TabComponent from 'background/components/tab';
import IndicatorComponent from 'background/components/indicator';
import reducers from 'background/reducers';
-import { createStore } from 'shared/store';
-import * as versions from 'shared/versions';
+import { createStore, applyMiddleware } from 'redux';
+import promise from 'redux-promise';
+import * as versions from './shared/versions';
-const store = createStore(reducers, (e, sender) => {
- console.error('Vim-Vixen:', e);
- if (sender) {
- return browser.tabs.sendMessage(sender.tab.id, {
- type: messages.CONSOLE_SHOW_ERROR,
- text: e.message,
- });
- }
-});
+const store = createStore(
+ reducers,
+ applyMiddleware(promise),
+);
const checkAndNotifyUpdated = async() => {
let updated = await versions.checkUpdated();
diff --git a/src/background/reducers/index.js b/src/background/reducers/index.js
index 78f855c..465f927 100644
--- a/src/background/reducers/index.js
+++ b/src/background/reducers/index.js
@@ -1,17 +1,8 @@
-import settingReducer from './setting';
-import findReducer from './find';
-import tabReducer from './tab';
+import { combineReducers } from 'redux';
+import setting from './setting';
+import find from './find';
+import tab from './tab';
-// Make setting reducer instead of re-use
-const defaultState = {
- setting: settingReducer(undefined, {}),
- find: findReducer(undefined, {}),
- tab: tabReducer(undefined, {}),
-};
-
-export default function reducer(state = defaultState, action = {}) {
- return { ...state,
- setting: settingReducer(state.setting, action),
- find: findReducer(state.find, action),
- tab: tabReducer(state.tab, action), };
-}
+export default combineReducers({
+ setting, find, tab,
+});
diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js
index d5875fe..9ca13f7 100644
--- a/src/background/shared/completions/index.js
+++ b/src/background/shared/completions/index.js
@@ -1,6 +1,19 @@
+import commandDocs from 'shared/commands/docs';
import * as tabs from './tabs';
import * as histories from './histories';
import * as bookmarks from './bookmarks';
+import * as properties from 'shared/settings/properties';
+
+const completeCommands = (typing) => {
+ let keys = Object.keys(commandDocs);
+ return keys
+ .filter(name => name.startsWith(typing))
+ .map(name => ({
+ caption: name,
+ content: name,
+ url: commandDocs[name],
+ }));
+};
const getSearchCompletions = (command, keywords, searchConfig) => {
let engineNames = Object.keys(searchConfig.engines);
@@ -74,20 +87,63 @@ const getBufferCompletions = async(command, keywords, excludePinned) => {
];
};
-const getCompletions = (line, settings) => {
- let typedWords = line.trim().split(/ +/);
- let typing = '';
- if (!line.endsWith(' ')) {
- typing = typedWords.pop();
+const getSetCompletions = (command, keywords) => {
+ let keys = Object.keys(properties.docs).filter(
+ name => name.startsWith(keywords)
+ );
+ let items = keys.map((key) => {
+ if (properties.types[key] === 'boolean') {
+ return [
+ {
+ caption: key,
+ content: command + ' ' + key,
+ url: 'Enable ' + properties.docs[key],
+ }, {
+ caption: 'no' + key,
+ content: command + ' no' + key,
+ url: 'Disable ' + properties.docs[key],
+ }
+ ];
+ }
+ return [
+ {
+ caption: key,
+ content: command + ' ' + key,
+ url: 'Set ' + properties.docs[key],
+ }
+ ];
+ });
+ items = items.reduce((acc, val) => acc.concat(val), []);
+ if (items.length === 0) {
+ return Promise.resolve([]);
}
+ return Promise.resolve([
+ {
+ name: 'Properties',
+ items,
+ }
+ ]);
+};
- if (typedWords.length === 0) {
- return Promise.resolve([]);
+const complete = (line, settings) => {
+ let trimmed = line.trimStart();
+ let words = trimmed.split(/ +/);
+ let name = words[0];
+ if (words.length === 1) {
+ let items = completeCommands(name);
+ if (items.length === 0) {
+ return Promise.resolve([]);
+ }
+ return Promise.resolve([
+ {
+ name: 'Console Command',
+ items: completeCommands(name),
+ }
+ ]);
}
- let name = typedWords.shift();
- let keywords = typedWords.concat(typing).join(' ');
+ let keywords = trimmed.slice(name.length).trimStart();
- switch (name) {
+ switch (words[0]) {
case 'o':
case 'open':
case 't':
@@ -108,12 +164,10 @@ const getCompletions = (line, settings) => {
case 'bdelete':
case 'bdeletes':
return getBufferCompletions(name, keywords, true);
+ case 'set':
+ return getSetCompletions(name, keywords);
}
return Promise.resolve([]);
};
-const complete = (line, settings) => {
- return getCompletions(line, settings);
-};
-
export { complete };
diff --git a/src/background/shared/versions/index.js b/src/background/shared/versions/index.js
new file mode 100644
index 0000000..aa09c92
--- /dev/null
+++ b/src/background/shared/versions/index.js
@@ -0,0 +1,38 @@
+import * as storage from './storage';
+import * as releaseNotes from './release-notes';
+import manifest from '../../../../manifest.json';
+
+const NOTIFICATION_ID = 'vimvixen-update';
+
+const notificationClickListener = (id) => {
+ if (id !== NOTIFICATION_ID) {
+ return;
+ }
+
+ browser.tabs.create({ url: releaseNotes.url(manifest.version) });
+ browser.notifications.onClicked.removeListener(notificationClickListener);
+};
+
+const checkUpdated = async() => {
+ let prev = await storage.load();
+ if (!prev) {
+ return true;
+ }
+ return manifest.version !== prev;
+};
+
+const notify = () => {
+ browser.notifications.onClicked.addListener(notificationClickListener);
+ return browser.notifications.create(NOTIFICATION_ID, {
+ 'type': 'basic',
+ 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'),
+ 'title': 'Vim Vixen ' + manifest.version + ' has been installed',
+ 'message': 'Click here to see release notes',
+ });
+};
+
+const commit = () => {
+ storage.save(manifest.version);
+};
+
+export { checkUpdated, notify, commit };
diff --git a/src/background/shared/versions/release-notes.js b/src/background/shared/versions/release-notes.js
new file mode 100644
index 0000000..6ef2335
--- /dev/null
+++ b/src/background/shared/versions/release-notes.js
@@ -0,0 +1,8 @@
+const url = (version) => {
+ if (version) {
+ return 'https://github.com/ueokande/vim-vixen/releases/tag/' + version;
+ }
+ return 'https://github.com/ueokande/vim-vixen/releases/';
+};
+
+export { url };
diff --git a/src/background/shared/versions/storage.js b/src/background/shared/versions/storage.js
new file mode 100644
index 0000000..7883258
--- /dev/null
+++ b/src/background/shared/versions/storage.js
@@ -0,0 +1,10 @@
+const load = async() => {
+ let { version } = await browser.storage.local.get('version');
+ return version;
+};
+
+const save = (version) => {
+ return browser.storage.local.set({ version });
+};
+
+export { load, save };