select tab as rotations

This commit is contained in:
Shin'ya Ueoka 2017-09-04 07:54:27 +09:00
parent 8cabd68b92
commit 4341f5ab62
2 changed files with 9 additions and 7 deletions

View file

@ -55,9 +55,9 @@ const normalizeUrl = (string) => {
} }
} }
const cmdBuffer = (arg) => { const cmdBuffer = (sender, arg) => {
if (isNaN(arg)) { if (isNaN(arg)) {
return tabs.selectByKeyword(arg); return tabs.selectByKeyword(sender.tab, arg);
} else { } else {
let index = parseInt(arg, 10) - 1; let index = parseInt(arg, 10) - 1;
return tabs.selectAt(index); return tabs.selectAt(index);
@ -73,7 +73,7 @@ const cmdEnterHandle = (request, sender) => {
return browser.tabs.create({ url: normalizeUrl(words[1]) }); return browser.tabs.create({ url: normalizeUrl(words[1]) });
case 'b': case 'b':
case 'buffer': case 'buffer':
return cmdBuffer(words[1]); return cmdBuffer(sender, words[1]);
} }
throw new Error(words[0] + ' command is not defined'); throw new Error(words[0] + ' command is not defined');
}; };

View file

@ -31,7 +31,7 @@ const selectAt = (index) => {
}); });
}; };
const selectByKeyword = (keyword) => { const selectByKeyword = (current, keyword) => {
return browser.tabs.query({ currentWindow: true }).then((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)
@ -39,10 +39,12 @@ const selectByKeyword = (keyword) => {
if (matched.length == 0) { if (matched.length == 0) {
throw new RangeError('No matching buffer for ' + keyword); throw new RangeError('No matching buffer for ' + keyword);
} else if (matched.length >= 2) {
throw new RangeError('More than one match for ' + keyword);
} }
for (let tab of matched) {
if (tab.index > current.index) {
return browser.tabs.update(tab.id, { active: true });
}
}
return browser.tabs.update(matched[0].id, { active: true }); return browser.tabs.update(matched[0].id, { active: true });
}); });
} }