透過程式輔助方式將網頁存取權授予 ChromeOS Desk

1. 事前準備

桌面連接器是 ChromeOS 功能,可讓網頁以程式輔助方式存取 ChromeOS 虛擬桌面。在本程式碼研究室中,您將瞭解如何搭配 Chrome 開發人員工具使用 Desk Connector。

必要條件

課程內容

  • 如何設定視窗屬性。
  • 如何啟動辦公桌。
  • 如何移除辦公桌。
  • 如何取得使用中的桌面。
  • 如何切換至其他桌面。

軟硬體需求

2. 做好準備

如要開始設定,請按照下列步驟操作:

  1. 在網頁上按一下滑鼠右鍵,然後點選「檢查」
  2. 在 Chrome 開發人員工具面板中,按一下「Console」(控制台)。您將在本程式碼研究室的「控制台」面板中執行所有程式碼。
  3. (選用) 在整個程式碼研究室中,您可以手動與虛擬桌面互動,方法是按下 F5[]|| 進入總覽模式。

7a5398f02e46d103.png

3. 設定視窗屬性

SetWindowProperties API 可讓網頁以程式輔助方式設定視窗的屬性。將 allDesks 屬性設為 true 值時,系統會將目前分頁視窗釘選至所有桌面。

如要設定視窗屬性,請按照下列步驟操作:

  1. 在「控制台」面板中,將視窗設為顯示在所有桌面上:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "SetWindowProperties",
    "operands": {
        "allDesks": true
    }
   },
  function(response) {
      console.log(response);
  }
);
  1. 將視窗設為再次顯示在單一桌面上:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond",{
    "messageType": "SetWindowProperties",
    "operands": {
        "allDesks": false
    }
   },
  function(response) {
     console.log(response);
  }
);
  1. 再次將視窗釘選至所有桌面,方便示範:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "SetWindowProperties",
    "operands": {
        "allDesks": true
    }
   },
  function(response) {
     console.log(response);
  }
);

如果 Chromebook 上有多個虛擬桌面,現在所有桌面都會顯示目前的視窗。

4. 取得目前使用中的桌面

網頁可透過 GetActiveDesk API,以程式輔助方式查詢目前啟用的桌面。

  • 如要取得目前使用的桌面,請擷取目前的桌面 ID,然後將 deskUuid 屬性儲存在 baseDesk 變數中:
let baseDesk;
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "GetActiveDesk",
    },
   function(response) {
    console.log(response);
    if(response && response.operands) {
    baseDesk = response.operands.deskUuid; 
   }       
  }
);

5. 啟動桌面

網頁可透過 LaunchDesk API 以程式輔助方式啟動空白桌面,並切換至該桌面。

如要啟動辦公桌,請按照下列步驟操作:

  1. 啟動名為「test」的桌面。
  2. 建立儲存書桌 deskUuid 屬性的 newDesk1 變數。
let newDesk1;                   
chrome.runtime.sendMessage(
    "oagemgapjncacjdaockjfaidedggjond", {
        "messageType": "LaunchDesk",
        "operands": {
            "deskName": "test"           
        }
    },
    function(response) {
     console.log(response);
     if(response&&response.operands) {
     newDesk1 = response.operands.deskUuid;   
    }  
   }
);

系統會將你帶往新啟用的桌面。如果控制台視窗位於獨立視窗中,控制台視窗會顯示在原始桌面上。如要切換回原始桌面,請按 F5[]|| 鍵。

你最多可以同時開啟八個桌面。如果 API 傳回 DesksCountCheckFailedError 錯誤,您必須先移除一些辦公桌,才能啟動更多辦公桌。

6. 移除桌面

網頁可透過 RemoveDesk API,以程式輔助方式依據桌面 ID 移除桌面。關閉桌面上所有視窗。

為求簡單,請移除新啟動的桌面。不過,實際上網頁可以依辦公桌 ID 移除任何指定辦公桌。

如要移除辦公桌,請按照下列步驟操作:

  1. 移除新啟動的桌面:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "RemoveDesk",
    "operands": {
        "deskId": newDesk1                        
    }
  },
  function(response) {
     console.log(response);
  }
);
  1. 在要求確認或放棄移除的對話方塊中,確認要移除辦公桌。系統會關閉桌面上的所有視窗。

ChromeOS 關閉所有視窗通知

  1. 如要呼叫 API 移除辦公桌,且不顯示確認視窗,請使用 skipConfirmation 參數:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "RemoveDesk",
    "operands": {
        "deskId": newDesk1,
        "skipConfirmation":true
   }
  },
  function(response) {
     console.log(response);
  }
);

7. 切換至其他桌面

網頁可透過 SwitchDesk API,以程式輔助方式切換至指定桌面。

為求簡單,請啟動桌面,然後切換至上一個桌面。不過,網頁實際上可以依據辦公室 ID 切換至任何指定辦公室。

如要切換至其他辦公桌,請按照下列步驟操作:

  1. 啟動桌面:
let newDesk1;                   
chrome.runtime.sendMessage(
    "oagemgapjncacjdaockjfaidedggjond", {
        "messageType": "LaunchDesk",
        "operands": {
            "deskName": "test"          
        }
    },
    function(response) {
     console.log(response);
     if(response && response.operands) {
     newDesk1 = response.operands.deskUuid;
    }     
   }
);
  1. 切換至上一個桌面:
chrome.runtime.sendMessage("oagemgapjncacjdaockjfaidedggjond", {
    "messageType": "SwitchDesk",
    "operands": {
        "deskId": baseDesk                 
   }
  },
   function(response) {
       console.log(response); 
  }
);

8. 恭喜

恭喜!您已瞭解如何透過 Desk Connector Demo 擴充功能和 Chrome 開發人員工具,以程式輔助方式授予網頁 ChromeOS 桌面存取權。

瞭解詳情