2014-03-12 4 views
0

사용자에게 RSS 피드의 링크를 제공하도록 고안된 Chrome 확장 프로그램을 구현하기 시작했습니다.popup.js와 background.js 간의 통신

브라우저 액션 확장은 백그라운드 스크립트 (bg.js)와 jQuery를 가진 popup.html과 popup.js을 포함

지금까지 내가, 모든 곳에 지점 callback()까지 잘 실행 말할 수있는 bg.js에서 호출하고 popup.js에서 수신해야합니다. popup.js의 콜백이 실행되지 않는 것 같습니다.

배경 페이지 콘솔과 팝업 콘솔을 모두 확인했지만 오류가 없습니다.

bg.js에서 repsonse를 받으려면 무엇을 변경해야합니까?

Btw : chrome.runtime.sendMessagechrome.extension.sendMessage을 사용하는 경우의 차이점은 무엇입니까? 이것은

{ 
    "manifest_version": 2, 

    "name": "heise Newsticker", 
    "description": "Der RSS-Feed des heise Newstickers.", 
    "version": "1.0", 

    "icons": { 
     "128" : "logo128.png" 
    }, 
    "background": { 
    "persistent": true, 
    "scripts": ["bg.js"] 
    }, 
    "permissions": [ 
    "http://www.heise.de/newsticker*", 
    "http://heise.de.feedsportal.com/c/35207/f/653902/index.rss" 
    ], 
    "browser_action": { 
    "default_icon": "logo.png", 
    "default_popup": "popup.html" 
    } 
} 

처럼

은 manifest.json을 보이는

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="popup.js"></script> 
</head> 
<body> 
<div id="ziel"></div> 
</body> 
</html> 

popup.html

function fetch_feed(url, callback) { 
     var xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = function(data) { 
     if (xhr.readyState == 4) { 
     if (xhr.status == 200 || xhr.status == 304) { 
     console.log("Request successful"); 
      var data = xhr.responseText; 
      callback(data); 
     } else { 
     console.log("Request not successful"); 
      callback(null); 
     } 
     } 
    } 
    // Note that any URL fetched here must be matched by a permission in 
    // the manifest.json file! 
    xhr.open('GET', url, true); 
    xhr.send(); 
} 

function onMessage(request, sender, callback) { 
     console.log("onmessage received with URL: " + request.url); 
     if (request.action == 'fetch_feed') { 
     fetch_feed(request.url, callback); 
     } 
} 

console.log('listener installed'); 
// Wire up the listener. 
chrome.extension.onMessage.addListener(onMessage); 

bg.js 그리고 마지막으로 : popup.js

$(document).ready(function() { 
     fetch_feed(); 
}); 

function fetch_feed() { 
     console.log("starting"); 
     chrome.extension.sendMessage({'action' : 'fetch_feed', 'url' : 'http://www.heise.de/newsticker/heise-atom.xml'}, 
       function(response) { 
         console.log("Response successfully received: " + response); 
         display_stories(response); 
       } 
     ); 
} 

function display_stories(xml) { 
    $('#ziel').html("<h3>now displaying</h3>"); 
} 
+0

문제는'fetch_feed' 메소드의'callback' 매개 변수에있는 것 같습니다. 'onMessage()'에서'callback'을 사용하면 잘 작동하지만'fetch_feed'에서는 절대로 호출되지 않습니다. 이것은 이상한 해결책일까요? – devnull69

답변

0

해결 방법 (단순화) : bg.js에서 모든 내용을 제거하고 XMLHttpRequest를 popup.js로 이동했습니다.

더 이상 메시지를 전달하지 않아 ... 문제가 해결되었지만 문제가 여전히 이상합니다.