diff options
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/actions/command.js | 2 | ||||
-rw-r--r-- | src/background/shared/commands/docs.js | 11 | ||||
-rw-r--r-- | src/background/shared/commands/parsers.js | 59 | ||||
-rw-r--r-- | src/background/shared/completions/index.js | 2 |
4 files changed, 72 insertions, 2 deletions
diff --git a/src/background/actions/command.js b/src/background/actions/command.js index a7f619b..feb16d8 100644 --- a/src/background/actions/command.js +++ b/src/background/actions/command.js @@ -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) => { diff --git a/src/background/shared/commands/docs.js b/src/background/shared/commands/docs.js new file mode 100644 index 0000000..c73eb71 --- /dev/null +++ b/src/background/shared/commands/docs.js @@ -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', +}; diff --git a/src/background/shared/commands/parsers.js b/src/background/shared/commands/parsers.js new file mode 100644 index 0000000..fb37d2a --- /dev/null +++ b/src/background/shared/commands/parsers.js @@ -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 }; diff --git a/src/background/shared/completions/index.js b/src/background/shared/completions/index.js index 9ca13f7..157604e 100644 --- a/src/background/shared/completions/index.js +++ b/src/background/shared/completions/index.js @@ -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'; |