1. 程式人生 > 其它 >js推送網頁到擴充套件屏上--谷歌瀏覽器

js推送網頁到擴充套件屏上--谷歌瀏覽器

 

平時我們推送網頁、開啟視窗都是用的 window.open,但是谷歌卻不支援這種方法,也不是不支援,是可以開啟視窗,但是無法將視窗移動到擴充套件屏上。

 

後面經過百度,發現了一個支援谷歌推送網頁到擴充套件屏的方法:PresentationRequest

 

完整demo:

親測有效

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>谷歌推送網頁</title> </head> <body> <button onclick="test1()">推送</button> <!-- <button onclick="test2()">重連</button> --> <!-- <button onclick="test3()">關閉連線</button>
--> <button onclick="test4()">關閉彈窗</button> <script> // document: https://w3c.github.io/presentation-api/ var request = new PresentationRequest("https://baidu.com"); navigator.presentation.defaultRequest = request; let con; var pId;
function test1(){ request.start().then(connect=>{ console.log(connect.url) console.log(connect.id) pId = connect.id }).catch(err=>{console.log(err)}); } // 監測連線是否可用 request.addEventListener("connectionavailable", function(e){ con = e.connection; con.addEventListener("close", function(){console.log("closed");}) con.addEventListener("terminate", function(){console.log("terminated");}) con.addEventListener("message", function(e){console.log(e.data);}) }) function test2(){ request.reconnect(pId) } function test3(){ con.close() } function test4(){ con.terminate() } </script> </body> </html>