0

私はChritterと呼ばれる公式のChrome拡張機能のチュートリアルに従っています。そこで、Twitterからツイートを取り出して、拡張機能に配置します。私はXMLファイルからアイテムを取得しようとしている以外は同様のことをしようとしています。background_pageからポップアップまでの情報を取得するには?

マイXML

<xml> 

<item> 
<title>Title 1</title> 
<description>Description 1</description> 
<duration>55:00</duration> 
<published>28/01/2011</published> 
</item> 

<item> 
<title>Title 2</title> 
<description>Description 2</description> 
<duration>55:00</duration> 
<published>28/01/2011</published> 
</item> 

</xml> 

background.html

<!-- 
To change this template, choose Tools | Templates 
and open the template in the editor. 
--> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

     <script type="text/javascript"> 
      var fetchFreq = 30000; // how often we fetch new items (30s) 
      var req; // request object 
      var unreadCount = 0; // how many unread items we have 
      var items; // all currently fetched items 

      getItems(); 
      //setInterval(getItems, fetchFreq); 

      function getItems(){ 
       req = new XMLHttpRequest(); 
       req.open("GET", "http://urltoxml.com/xmlfile.xml", false); 
       req.onload = processItems; 
       req.send(); 
      } 

      function processItems(){ 

       xmlDoc = req.responseXML; 

       items = xmlDoc.getElementsByTagName("item"); 

       unreadCount += items.length; 

       if (unreadCount > 0) { 
        chrome.browserAction.setBadgeBackgroundColor({ 
         color: [255, 0, 0, 255] 
        }); 
        chrome.browserAction.setBadgeText({text: '' + unreadCount}); 
       } 

       items = xmlDoc.concat(items); 
      } 
     </script> 
    </head> 
</html> 

私はbackground.htmlからフェッチされたアイテムを取得する方法を知っているとpopup.html上に表示されないのですか?

popup.html

<html> 
    <head> 
    <link rel="stylesheet" href="popup.css" /> 
    <script src="util.js"></script> 
    <script> 
     var bg; // background page 

     // timeline attributes 
     var timeline; 
     var template; 
     var title; 
     var link; 
     var description; 

     onload = setTimeout(init, 0); // workaround for http://crbug.com/24467 

     // initialize timeline template 
     function init() { 
     chrome.browserAction.setBadgeText({text: ''}); 
     bg = chrome.extension.getBackgroundPage(); 
     bg.unreadCount = 0; 

     timeline = document.getElementById('timeline'); 
     template = xpath('//ol[@id="template"]/li', document); 
     title = xpath('//div[@class="text"]/span', title); 
     content = xpath('//div[@class="text"]/span', template); 

     update(); 
     } 

    function update(){ 

    // how to do this ? 
    // See Chritter example below with JSON, 
    // except i want to it with xml ? 

    } 

    </script> 
    </head> 
    <body> 
    <div id="body"> 
     <ol id="timeline" /> 
    </div> 
    <ol id="template"> 
     <li> 
     <div class="text"> 
      <a></a> 
      <span></span> 
     </div> 
     <div class="clear"></div> 
     </li> 
    </ol> 
    </body> 
</html> 

Chritter拡張子はそれだけでJSONで動作するように思わない方法。ここでは、彼らはそれを行う方法です。

// update display 
    function update() { 
    var user; 
    var url; 
    var item; 

    for (var i in bg.tweets) { 
     user = bg.tweets[i].user; 
     url = 'http://twitter.com/' + user.screen_name; 

     // thumbnail 
     link.title = user.name; 
     link.href = openInNewTab(url); 
     image.src = user.profile_image_url; 
     image.alt = user.name; 

     // text 
     author.href = openInNewTab(url); 
     author.innerHTML = user.name; 
     content.innerHTML = linkify(bg.tweets[i].text); 

     // copy node and update 
     item = template.cloneNode(true); 
     timeline.appendChild(item); 
    } 
    } 

Chritterbackground.html

<html> 
    <head> 
    <script type="text/javascript"> 
     var fetchFreq = 30000; // how often we fetch new tweets (30s) 
     var req; // request object 
     var unreadCount = 0; // how many unread tweets we have 
     var tweets; // all currently fetched tweets 

     getTweets(); 
     setInterval(getTweets, fetchFreq); 

     // fetch timeline from server 
     function getTweets() { 
     req = new XMLHttpRequest(); 
     req.open('GET', 'http://twitter.com/statuses/public_timeline.json'); 
     req.onload = processTweets; 
     req.send(); 
     } 

     // process new batch of tweets 
     function processTweets() { 
     var res = JSON.parse(req.responseText); 
     unreadCount += res.length; 

     if (unreadCount > 0) { 
      chrome.browserAction.setBadgeBackgroundColor({ 
      color: [255, 0, 0, 255] 
      }); 
      chrome.browserAction.setBadgeText({text: '' + unreadCount}); 
     } 

     tweets = res.concat(tweets); 
     } 
    </script> 
    </head> 
</html> 

感謝すべてのヘルプ!ありがとう!

答えて

2

あなたが背景ページからitems VARにアクセスする場合:あなたはブラウザセッション間で保持するためにそれらを必要としない限り、

var items = chrome.extension.getBackgroundPage().items; 
0

正確な質問はわかりませんが、一般的なプラクティスはバックグラウンドページのデータをlocalstorageに保存し、ポップアップページからこのデータにアクセスすることです。

http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm

+1

ローカルストレージに物事を格納する必要はありません。バックグラウンドページとその変数は、コンテンツスクリプト以外のすべての拡張スクリプトから直接アクセスできます。 – brymck

+0

私は同意する....... –

関連する問題