2010-08-21 3 views
6

콘텐츠 스크립트에서 간단한 바탕 화면 알림 코드를 표시하려고하는데 작동하지 않는 것처럼 보입니다. maifest.json 파일에 사용 권한을 추가했습니다. 콘텐츠 스크립트에서 콘텐츠를 보여줄 수 있습니까?콘텐츠 스크립트의 바탕 화면 알림

답변

2

예, 알림은 Chrome 특정 API를 사용하며 콘텐츠 스크립트는 일반 자바 스크립트에만 유효합니다 ... background page은 모든 크롬 관련 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> 
+0

큰 :

// in your contentscript.js chrome.extension.sendRequest({msg: "Sup?"}, function(response) { // optional callback - gets response console.log(response.returnMsg); }); 

그리고 수신 측에

당신이 onRequest 수신기가 있어야합니다

{ "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() 사용 .. 데이빗 감사합니다 :) – sharath

+0

permiss를 설정하면 이온을 사용하면 권한을 확인할 필요가 없습니다. http://code.google.com/chrome/extensions/notifications.html – npdoty

+1

콘텐츠 스크립트에서 시작된 바탕 화면 알림을 표시하는 방법을 실제로 설명하지 않습니다. –

8

콘텐츠 스크립트를 통해 직접 알림을 표시 할 수 없습니다. 그러나 수 있습니다 배경 페이지를 통해 그들을 보여줍니다.

당신 manifest.js은 다음과 비슷한 모습이 될 것

// 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 
    }); 
+0

나는 마침내 이런 식으로 일했습니다. 그들은 단지 콘텐츠 스크립트로부터 바탕 화면 알림을 허용 할 수도 있습니다. 사람들은 폐쇄 API에 대해 애플을 괴롭 히지만, 웹이 더 개방적 일 것이라는 점은 구글과 다르다. –

+0

이것은 질문의 제목을 토대로 내가 얻고 자했던 대답이다. 감사! –