Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TabManager {
- // private タブの切り替え
- static switchActiveTab(proc) {
- // 現在のウィンドウのタブを取得
- browser.tabs.query({
- 'currentWindow': true
- }).then(tabs => {
- if (tabs.length === 0) {
- throw "操作可能なタブがない";
- }
- const index = tabs.findIndex(tab => tab.active);
- if (index === -1) {
- throw "アクティブなタブがない";
- }
- // 切り替え後のインデックスを取得
- const target = proc(index, tabs.length);
- // 対象のタブをアクティブにする
- browser.tabs.update(tabs[target].id, {
- 'active': true
- });
- });
- }
- // private アクティブなタブへの操作
- static processActiveTab(proc) {
- // 現在のウィンドウのアクティブなタブを取得
- browser.tabs.query({
- 'currentWindow': true,
- 'active': true
- }).then(tabs => {
- if (tabs.length !== 1) {
- throw "アクティブなタブがない";
- }
- // タブへの処理
- proc(tabs[0].id);
- });
- }
- // private 全タブへの操作
- static processAllTabs(proc) {
- // 現在のウィンドウの全タブを取得
- browser.tabs.query({
- 'currentWindow': true
- }).then(tabs => {
- if (tabs.length === 0) {
- throw "アクティブなタブがない";
- }
- // 各タブへの処理
- for (const tab of tabs) {
- proc(tab.id);
- }
- });
- }
- // 前のタブへ(左端の場合は右端へ)
- static toPrevious() {
- TabManager.switchActiveTab((index, length) => (index + length - 1) % length);
- }
- // 右のタブへ(右端の場合は左端へ)
- static toNext() {
- TabManager.switchActiveTab((index, length) => (index + 1) % length);
- }
- // 現在のウィンドウのアクティブなタブを再読み込み
- static reload() {
- browser.tabs.reload();
- }
- // 現在のウィンドウのアクティブなタブを閉じる
- static close() {
- TabManager.processActiveTab(browser.tabs.remove);
- }
- // 現在のウィンドウのアクティブなタブを複製
- static duplicate() {
- TabManager.processActiveTab(browser.tabs.duplicate);
- }
- // 全タブの再読み込み
- static reloadAll() {
- TabManager.processAllTabs(browser.tabs.reload);
- }
- }
- class WindowManager {
- // private 現在のウィンドウへの操作
- static processCurrentWindow(proc) {
- // 現在のウィンドウを取得
- browser.windows.getCurrent(
- /* なし */
- ).then(win => {
- // ウィンドウへの処理
- proc(win.id);
- });
- }
- // private 現在のウィンドウの状態を更新
- static updateWindowState(state) {
- WindowManager.processCurrentWindow(id => {
- // ウィンドウを更新
- browser.windows.update(id, {
- 'state': state
- });
- });
- }
- // 現在のウィンドウを最小化
- static minimize() {
- WindowManager.updateWindowState('minimized');
- }
- // 現在のウィンドウを最大化
- static maximize() {
- WindowManager.updateWindowState('maximized');
- }
- // 現在のウィンドウをフルスクリーンにする
- static fullscreen() {
- WindowManager.updateWindowState('fullscreen');
- }
- // 現在のウィンドウとタブを閉じる
- static close() {
- WindowManager.processCurrentWindow(browser.windows.remove);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement