iJade
iJade

Reputation: 23811

Issue with chrome extension code to fetch tweets from twitter

I'm trying to create a chrome extension which shows tweets with hashtag perkytweets in the extension pop up but nothing is showing up.

Here is the manifest.json code

    {
  "name": "My First Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

Here is the popup.html code

<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        min-width:357px;
        overflow-x:hidden;
      }

      img {
        margin:5px;
        border:2px solid black;
        vertical-align:middle;
        width:75px;
        height:75px;
      }
    </style>


    <script src="script.js"></script>
  </head>
  <body>
  </body>
</html>

Here is the script.js code

var req = new XMLHttpRequest();
req.open(
    "GET",
    "http://search.twitter.com/search.atom?q=perkytweets",
    true);
req.onload = showTweets;
req.send(null);


function showTweets()
{
    document.body.appendChild(req.responseXML);
}

Upvotes: 0

Views: 370

Answers (1)

epoch
epoch

Reputation: 16615

You need to fire of the request only when the document has been loaded. try wrapping your code in an onload handler

function showTweets() {
    document.body.appendChild(req.responseXML);
}

var req;
window.onload = function() {
    req = new XMLHttpRequest();
    req.open("GET", "http://search.twitter.com/search.atom?q=perkytweets", true);
    req.onload = showTweets;
    req.send(null);
}

UPDATE

You need to allow access to the twitter URL, add this to your manifest:

"permissions": [ "http://search.twitter.com/*" ]

So your manifest.json will now look like this:

{
    "name": "My First Extension",
    "version": "1.0",
    "manifest_version": 2,
    "description": "The first extension that I made.",
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "permissions": [
        "http://search.twitter.com/*"
    ]
}

UPDATE

req.responseXML is not an Element yet, and as such cannot be directly appended to the body, console.log your result and go from there.

Upvotes: 1

Related Questions