我試圖從內容腳本中顯示一個簡單的桌面通知代碼,但它似乎沒有工作..我在maifest.json文件中添加了權限。從內容腳本中顯示它們是否有限制?來自內容腳本的桌面通知
回答
是的,通知使用Chrome特定的API,而內容腳本僅適用於一般JavaScript等... background page是所有特定於Chrome的API都可以運行的地方...首先,您需要註冊背景在manifest.json文件頁面 - 這樣的:
"background_page": "background.html",
另外在清單文件,允許所需的權限:
"permissions": [ "notifications" ],
然後你在後臺頁面腳本應該是這樣的:
<script>
setTimeout("setNotification();",1);
function setNotification(){
var n
if (window.webkitNotifications.checkPermission() != 0){
setNotification();
return false;
}
n = window.webkitNotifications.createHTMLNotification('http://www.your-notification-address.com');
n.show();}
</script>
您無法直接通過內容腳本顯示通知。 但是,您可以通過背景頁面顯示它們。
你manifest.js應該是這個樣子:
{
"name": "Notify This",
"version": "0.1",
"permissions": [
"notifications"
],
"background_page": "background.html",
"content_scripts": [
{
"matches": ["http://www.example.com/*"],
"js": ["contentscript.js"]
}
]
}
然後使用chrome.extension.sendRequest():
// in your contentscript.js
chrome.extension.sendRequest({msg: "Sup?"}, function(response) { // optional callback - gets response
console.log(response.returnMsg);
});
而在接收端,你應該有一個onRequest聽衆:
// in your background.html
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
// Create a simple text notification:
var notify = webkitNotifications.createNotification(
'48.png', // icon url - can be relative
'Hello!', // notification title
request.msg // notification body text
);
notify.show();
setTimeout(function(){ notify.cancel(); },5000);
sendResponse({returnMsg: "All good!"}); // optional response
});
以下看到我的回答謝謝我終於完成了這樣的工作。他們可能只是允許來自內容腳本的桌面通知。人們在蘋果公司對這樣封閉的API感到惱火,但它不像谷歌那樣網絡意味着更開放...... – 2011-05-31 15:14:28
這是我希望根據問題標題得到的答案。謝謝! – 2011-08-24 19:50:07
- 1. 來自後臺腳本的桌面通知
- 2. 來自GitHub的桌面通知
- 3. 內容腳本通知
- 4. 來自edittext字段的通知內容
- 5. 桌面通知
- 6. HTML5桌面通知
- 7. Vaadin桌面通知
- 8. Chrome桌面通知點擊以關注內容
- 9. gmail中的桌面通知
- 10. Chrome的桌面通知
- 11. asp.net中的桌面通知
- 12. 如何在谷歌應用腳本中使用桌面通知?
- 13. PowerShell腳本來更改桌面
- 14. Chrome桌面HTML通知
- 15. Java桌面通知Ubuntu
- 16. Html5桌面通知php/mysql
- 17. Facebook桌面通知器
- 18. 從彈出頁面到內容腳本的通信頁面
- 19. 限制來自ElastAlert的電子郵件通知中的內容
- 20. 縮進桌面內容
- 21. Photoshop CS5腳本填充內容知道
- 22. 遠程桌面PowerShell腳本
- 23. jQuery腳本是在桌面
- 24. 與Chrome擴展中的內容腳本中的頁面腳本進行通信
- 25. 激活來自bash腳本的KDE 4通知
- 26. 顯示來自Swift腳本的系統警報或通知
- 27. 來自background.js的內容腳本中的Chrome調用函數
- 28. 來自Docker容器的對話腳本
- 29. 從內容腳本觸發Chrome擴展程序通知
- 30. 來自asp.net頁面內容的PDF
大..謝謝大衛:) – sharath 2010-08-23 19:09:23
如果你設置permiss在您的擴展清單中通知離子,您無需檢查權限。 http://code.google.com/chrome/extensions/notifications.html – npdoty 2010-08-28 05:45:42
沒有真正解釋如何顯示源自內容腳本的桌面通知。 – 2011-04-16 09:32:32