簡體   English   中英

單擊 chrome.notification 如何打開 HTML 頁面

[英]How can I open HTML page on clicking chrome.notification

我正在開發 Chrome 擴展。 當用戶點擊通知時,我想打開 HTML 頁面。 通知必須在一定時間間隔后出現。 設置間隔和 background.js 中的 chrome.notification 一切正常,但我無法打開帶有通知的 html 頁面。Window.open 不起作用,因為未定義 window:

window.open('PAGE.html');

這是我的代碼 Background.js

async function getCurrentTab() {
    let queryOptions = { active: true, lastFocusedWindow: true };
    // `tab` will either be a `tabs.Tab` instance or `undefined`.
    let [tab] = await chrome.tabs.query(queryOptions);
    return tab;
  }
  setInterval(func,60000);
  function func(){
  var opt = {
    type: "basic",
    title: "Gympass",
    message: "You have been working for too long on Chrome. Would you like to take a break?",
    iconUrl: "logo.png"
  };
  chrome.notifications.create(opt, creationCallback);
  
  function creationCallback(){
    window.open('PAGE.html');
    //-----------SOME CODE -----------------------
    }
  }

清單.json

{
    "name" : "My Extension",
    "description": "Build an Extension!",
    "version": "1.0",
    "manifest_version": 3,
    "background": { "service_worker": "background.js" },
    "permissions": ["storage",
                    "scripting",
                    "tabs",
                    "alarms",
                    "webNavigation",
                    "notifications",  "activeTab", "contextMenus"],
    "host_permissions": [
                            "http://*/",
                            "https://*/"
                      ],
    "chrome_url_overrides" : {
                        "newtab": "storyboard_break_time.html"
                      },
    "action": {
                    "default_popup": "popup.html"
                },
    "icons": {
                  "16": "/images/logo-16.png",
                  "32": "/images/logo-32.png",
                  "48": "/images/logo-48.png",
                  "128": "/images/logo-128.png"
              }
}

我希望我的擴展程序在特定時間后生成通知,然后單擊該通知 HTML 頁面應在谷歌瀏覽器中打開。

重新創建您的chrome.notification以:

var notification = new Notification('Here Comes the Title', {
  icon: 'icon.png',
  body: "Hello World!"
});

notification.onclick = function () {
  window.open("LINK_TO_OPEN");
}

我使用 chrome.tabs API chrome.tabs.create({ url: "PAGE.html" });

而不是:window.open("PAGE.html");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM