This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/src/background/shared/tabs.js
2018-05-12 10:54:38 +09:00

174 lines
4.7 KiB
JavaScript

import * as tabCompletions from './completions/tabs';
const closeTab = (id) => {
return browser.tabs.get(id).then((tab) => {
if (!tab.pinned) {
return browser.tabs.remove(id);
}
});
};
const closeTabForce = (id) => {
return browser.tabs.remove(id);
};
const closeTabByKeywords = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword);
}).filter(t => !t.pinned);
if (matched.length === 0) {
throw new Error('No matching buffer for ' + keyword);
} else if (matched.length > 1) {
throw new Error('More than one match for ' + keyword);
}
browser.tabs.remove(matched[0].id);
});
};
const closeTabByKeywordsForce = (keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword);
});
if (matched.length === 0) {
throw new Error('No matching buffer for ' + keyword);
} else if (matched.length > 1) {
throw new Error('More than one match for ' + keyword);
}
browser.tabs.remove(matched[0].id);
});
};
const closeTabsByKeywords = (keyword) => {
tabCompletions.getCompletions(keyword).then((tabs) => {
let tabs2 = tabs.filter(tab => !tab.pinned);
browser.tabs.remove(tabs2.map(tab => tab.id));
});
};
const closeTabsByKeywordsForce = (keyword) => {
tabCompletions.getCompletions(keyword).then((tabs) => {
browser.tabs.remove(tabs.map(tab => tab.id));
});
};
const reopenTab = () => {
return browser.sessions.getRecentlyClosed({
maxResults: 1
}).then((sessions) => {
if (sessions.length === 0) {
return;
}
let session = sessions[0];
if (session.tab) {
return browser.sessions.restore(session.tab.sessionId);
}
return browser.sessions.restore(session.window.sessionId);
});
};
const selectAt = (index) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
if (tabs.length < 2) {
return;
}
if (index < 0 || tabs.length <= index) {
throw new RangeError(`tab ${index + 1} does not exist`);
}
let id = tabs[index].id;
return browser.tabs.update(id, { active: true });
});
};
const selectByKeyword = (current, keyword) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let matched = tabs.filter((t) => {
return t.url.includes(keyword) || t.title.includes(keyword);
});
if (matched.length === 0) {
throw new RangeError('No matching buffer 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 });
});
};
const selectPrevTab = (current, count) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
if (tabs.length < 2) {
return;
}
let select = (current - count + tabs.length) % tabs.length;
let id = tabs[select].id;
return browser.tabs.update(id, { active: true });
});
};
const selectNextTab = (current, count) => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
if (tabs.length < 2) {
return;
}
let select = (current + count) % tabs.length;
let id = tabs[select].id;
return browser.tabs.update(id, { active: true });
});
};
const selectFirstTab = () => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let id = tabs[0].id;
return browser.tabs.update(id, { active: true });
});
};
const selectLastTab = () => {
return browser.tabs.query({ currentWindow: true }).then((tabs) => {
let id = tabs[tabs.length - 1].id;
return browser.tabs.update(id, { active: true });
});
};
const selectTab = (id) => {
return browser.tabs.update(id, { active: true });
};
const reload = (current, cache) => {
return browser.tabs.reload(
current.id,
{ bypassCache: cache }
);
};
const updateTabPinned = (current, pinned) => {
return browser.tabs.query({ currentWindow: true, active: true })
.then(() => {
return browser.tabs.update(current.id, { pinned: pinned });
});
};
const toggleTabPinned = (current) => {
updateTabPinned(current, !current.pinned);
};
const duplicate = (id) => {
return browser.tabs.duplicate(id);
};
export {
closeTab, closeTabForce, closeTabByKeywords, closeTabByKeywordsForce,
closeTabsByKeywords, closeTabsByKeywordsForce,
reopenTab, selectAt, selectByKeyword,
selectPrevTab, selectNextTab, selectFirstTab,
selectLastTab, selectTab, reload, updateTabPinned,
toggleTabPinned, duplicate
};