2013-11-14 2 views
4

AngularJS를 사용하여 Chrome 패키지 앱을 만들고 있는데 백그라운드 스크립트 ('runtime.js')에서 다른 앱으로 메시지를 보내려고합니다. 내 프로젝트의 자바 스크립트 파일.Chrome 패키지 앱 - background.js에서 다른 스크립트 페이지로 전달되는 메시지

manifest.json을

{ 
     "name": "App Name", 
     "description": "Chrome Packaged", 
     "version": "0.0.9", 
     "manifest_version": 2, 
     "icons": { 
     "16": "img/icon16.png", 
     "48": "img/icon48.png", 
     "128":"img/icon128.png" 
     }, 
     "app": { 
     "background": { 
      "scripts": ["runtime.js"] 
     } 
     }, 
     "permissions": [ 
     "alarms", 
     "storage", 
     "unlimitedStorage", 
     "notifications", 
     "app.runtime" 
     ] 
    } 

runtime.js

chrome.app.runtime.onLaunched.addListener(function() { 
    chrome.app.window.create('index.html', { 
    minWidth: 400, 
    minHeight: 700, 
    bounds: { 
     width: 1000, 
     height: 700 
    } 
    });  
}); 

chrome.runtime.sendMessage({message: "hello"}, function() { 
    console.log('sent') 
}); 

main.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { 
    console.log('message received!'); 
}); 

나는 배경 페이지 "포트입니다 검사 할 때 점점 계속 오류 : 수 수신 연결이 존재하지 않습니다. "

어떤 문제 일 수 있습니까? 감사!

+0

주된 문제는 백그라운드 프로세스가 비 백그라운드 스크립트와 통신하도록하려는 것입니다. – adam8810

답변

7

메시지를 보내기 전에 index.html (main.js를 가져 오는 것으로 가정)이로드 될 때까지 기다려야합니다. 그러나 실제로 메시지를 보내는 대신 chrome.app.window.create에서 가져온 창 객체를 통해 직접 함수 호출을 할 수 있습니다.

chrome.app.runtime.onLaunched.addListener(function() { 
    chrome.app.window.create('index.html', { 
     minWidth: 400, 
     minHeight: 700, 
     bounds: { 
      width: 1000, 
      height: 700 
     } 
    }, function (myWindow) { 
     myWindow.contentWindow.addEventListener('load', function(e) { 
      myWindow.contentWindow.functionFromMainJs('hello'); 
     }); 
    });  
}); 
관련 문제