Move commands to background/shared
This commit is contained in:
parent
62d3c09f61
commit
4d4aaa2c4b
5 changed files with 3 additions and 3 deletions
|
@ -2,7 +2,7 @@ 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';
|
||||
import * as parsers from 'background/shared/commands/parsers';
|
||||
import * as properties from 'shared/settings/properties';
|
||||
|
||||
const openCommand = async(url) => {
|
||||
|
|
11
src/background/shared/commands/docs.js
Normal file
11
src/background/shared/commands/docs.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
export default {
|
||||
set: 'Set a value of the property',
|
||||
open: 'Open a URL or search by keywords in current tab',
|
||||
tabopen: 'Open a URL or search by keywords in new tab',
|
||||
winopen: 'Open a URL or search by keywords in new window',
|
||||
buffer: 'Sekect tabs by matched keywords',
|
||||
bdelete: 'Close a certain tab matched by keywords',
|
||||
bdeletes: 'Close all tabs matched by keywords',
|
||||
quit: 'Close the current tab',
|
||||
quitall: 'Close all tabs',
|
||||
};
|
59
src/background/shared/commands/parsers.js
Normal file
59
src/background/shared/commands/parsers.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
const normalizeUrl = (args, searchConfig) => {
|
||||
let concat = args.join(' ');
|
||||
try {
|
||||
return new URL(concat).href;
|
||||
} catch (e) {
|
||||
if (concat.includes('.') && !concat.includes(' ')) {
|
||||
return 'http://' + concat;
|
||||
}
|
||||
let query = concat;
|
||||
let template = searchConfig.engines[
|
||||
searchConfig.default
|
||||
];
|
||||
for (let key in searchConfig.engines) {
|
||||
if (args[0] === key) {
|
||||
query = args.slice(1).join(' ');
|
||||
template = searchConfig.engines[key];
|
||||
}
|
||||
}
|
||||
return template.replace('{}', encodeURIComponent(query));
|
||||
}
|
||||
};
|
||||
|
||||
const mustNumber = (v) => {
|
||||
let num = Number(v);
|
||||
if (isNaN(num)) {
|
||||
throw new Error('Not number: ' + v);
|
||||
}
|
||||
return num;
|
||||
};
|
||||
|
||||
const parseSetOption = (word, types) => {
|
||||
let [key, value] = word.split('=');
|
||||
if (value === undefined) {
|
||||
value = !key.startsWith('no');
|
||||
key = value ? key : key.slice(2);
|
||||
}
|
||||
let type = types[key];
|
||||
if (!type) {
|
||||
throw new Error('Unknown property: ' + key);
|
||||
}
|
||||
if (type === 'boolean' && typeof value !== 'boolean' ||
|
||||
type !== 'boolean' && typeof value === 'boolean') {
|
||||
throw new Error('Invalid argument: ' + word);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'string': return [key, value];
|
||||
case 'number': return [key, mustNumber(value)];
|
||||
case 'boolean': return [key, value];
|
||||
}
|
||||
};
|
||||
|
||||
const parseCommandLine = (line) => {
|
||||
let words = line.trim().split(/ +/);
|
||||
let name = words.shift();
|
||||
return [name, words];
|
||||
};
|
||||
|
||||
export { normalizeUrl, parseCommandLine, parseSetOption };
|
|
@ -1,4 +1,4 @@
|
|||
import commandDocs from 'shared/commands/docs';
|
||||
import commandDocs from 'background/shared/commands/docs';
|
||||
import * as tabs from './tabs';
|
||||
import * as histories from './histories';
|
||||
import * as bookmarks from './bookmarks';
|
||||
|
|
Reference in a new issue