Advertisement
miozn

WebExtensionsマウスジェスチャ(仮) タブ&ウィンドウ操作

Nov 11th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TabManager {
  2.     // private タブの切り替え
  3.     static switchActiveTab(proc) {
  4.         // 現在のウィンドウのタブを取得
  5.         browser.tabs.query({
  6.             'currentWindow': true
  7.         }).then(tabs => {
  8.             if (tabs.length === 0) {
  9.                 throw "操作可能なタブがない";
  10.             }
  11.  
  12.             const index = tabs.findIndex(tab => tab.active);
  13.             if (index === -1) {
  14.                 throw "アクティブなタブがない";
  15.             }
  16.  
  17.             // 切り替え後のインデックスを取得
  18.             const target = proc(index, tabs.length);
  19.  
  20.             // 対象のタブをアクティブにする
  21.             browser.tabs.update(tabs[target].id, {
  22.                 'active': true
  23.             });
  24.         });
  25.     }
  26.  
  27.     // private アクティブなタブへの操作
  28.     static processActiveTab(proc) {
  29.         // 現在のウィンドウのアクティブなタブを取得
  30.         browser.tabs.query({
  31.             'currentWindow': true,
  32.             'active': true
  33.         }).then(tabs => {
  34.             if (tabs.length !== 1) {
  35.                 throw "アクティブなタブがない";
  36.             }
  37.             // タブへの処理
  38.             proc(tabs[0].id);
  39.         });
  40.     }
  41.  
  42.     // private 全タブへの操作
  43.     static processAllTabs(proc) {
  44.         // 現在のウィンドウの全タブを取得
  45.         browser.tabs.query({
  46.             'currentWindow': true
  47.         }).then(tabs => {
  48.             if (tabs.length === 0) {
  49.                 throw "アクティブなタブがない";
  50.             }
  51.  
  52.             // 各タブへの処理
  53.             for (const tab of tabs) {
  54.                 proc(tab.id);
  55.             }
  56.         });
  57.     }
  58.  
  59.     // 前のタブへ(左端の場合は右端へ)
  60.     static toPrevious() {
  61.         TabManager.switchActiveTab((index, length) => (index + length - 1) % length);
  62.     }
  63.  
  64.     // 右のタブへ(右端の場合は左端へ)
  65.     static toNext() {
  66.         TabManager.switchActiveTab((index, length) => (index + 1) % length);
  67.     }
  68.  
  69.     // 現在のウィンドウのアクティブなタブを再読み込み
  70.     static reload() {
  71.         browser.tabs.reload();
  72.     }
  73.  
  74.     // 現在のウィンドウのアクティブなタブを閉じる
  75.     static close() {
  76.         TabManager.processActiveTab(browser.tabs.remove);
  77.     }
  78.  
  79.     // 現在のウィンドウのアクティブなタブを複製
  80.     static duplicate() {
  81.         TabManager.processActiveTab(browser.tabs.duplicate);
  82.     }
  83.  
  84.     // 全タブの再読み込み
  85.     static reloadAll() {
  86.         TabManager.processAllTabs(browser.tabs.reload);
  87.     }
  88. }
  89.  
  90. class WindowManager {
  91.     // private 現在のウィンドウへの操作
  92.     static processCurrentWindow(proc) {
  93.         // 現在のウィンドウを取得
  94.         browser.windows.getCurrent(
  95.             /* なし */
  96.         ).then(win => {
  97.             // ウィンドウへの処理
  98.             proc(win.id);
  99.         });
  100.     }
  101.  
  102.     // private 現在のウィンドウの状態を更新
  103.     static updateWindowState(state) {
  104.         WindowManager.processCurrentWindow(id => {
  105.             // ウィンドウを更新
  106.             browser.windows.update(id, {
  107.                 'state': state
  108.             });
  109.         });
  110.     }
  111.  
  112.     // 現在のウィンドウを最小化
  113.     static minimize() {
  114.         WindowManager.updateWindowState('minimized');
  115.     }
  116.  
  117.     // 現在のウィンドウを最大化
  118.     static maximize() {
  119.         WindowManager.updateWindowState('maximized');
  120.     }
  121.  
  122.     // 現在のウィンドウをフルスクリーンにする
  123.     static fullscreen() {
  124.         WindowManager.updateWindowState('fullscreen');
  125.     }
  126.  
  127.     // 現在のウィンドウとタブを閉じる
  128.     static close() {
  129.         WindowManager.processCurrentWindow(browser.windows.remove);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement