0

我嘗試使用desktop擴展通知使用擴展名。 我會這樣工作 - 當用戶訪問建議的頁面時,它會顯示。帶擴展卡的Chrome擴展通知

background.js

function show() { 
    var notification = window.webkitNotifications.createNotification(
    '48.png', 
    'YOUR VISIT PAGE http://stackoverflow.com/!' 
); 
    notification.show(); 
} 

// Conditionally initialize the options. 
if (!localStorage.isInitialized) { 
    localStorage.isActivated = true; // The display activation. 
    localStorage.frequency = 1;  // The display frequency, in minutes. 
    localStorage.isInitialized = true; // The option initialization. 
} 


function checkForValidUrl(tabId, changeInfo, tab) { 
    if (tab.url.indexOf('stackoverflow') > -1) { 
    if (window.webkitNotifications) { 
     if (JSON.parse(localStorage.isActivated)) { 
      show(); 
     } 
    } 
    } 
} 


chrome.tabs.onUpdated.addListener(checkForValidUrl); 

manifest.json的

{ 
    "name": "YouVisit", 
    "version": "0.1", 
    "description": 
    "Show desktop notification when user visit page", 
    "icons": {"48": "48.png"}, 
    "permissions": [ 
    "notifications", 
    "tabs" 
    ], 
    "background": { "scripts": ["background.js"] }, 
    "manifest_version": 2, 

    "web_accessible_resources": [ 
    "48.png" 
    ] 
} 

任何想法,爲什麼這個代碼不工作?有人能給我一些文學作品嗎?

+0

什麼它不工作?你需要包含錯誤細節。 – abraham

+0

您應該使用[chrome.storage.sync](https://developer.chrome.com/extensions/storage)而不是localStorage,並考慮使用[Chrome擴展程序通知](https://developer.chrome.com/擴展/通知)(其中[不需要任何特殊許可](https://developer.chrome.com/extensions/permission_warnings#nowarning))而不是webkitNotifications(這需要用戶授予權限)。 –

回答

1

你沒有提供的功能createNotification()適當的參數:

根據the docs

// Create a simple text notification: 
var notification = webkitNotifications.createNotification(
    '48.png',   // icon url - can be relative 
    'Hello!',   // notification title 
    'Lorem ipsum...' // notification body text 
);