I’m trying to write an xkcd extension for chrome in javascript.
To obtain the url to the latest image, I fetch the JSON from http://dynamic.xkcd.com/api-0/jsonp/comic/ and try to parse it. Obviously, that doesn’t quite work.
Neither is the json parsed correctly, nor is the image from the hardcoded url shown in the popup. How can I solve these problems?
So my questions are:
-
How can I retrieve the image url from the json?
-
How can I insert the image to the popup?
Here is my code:
(I followed a tutorial, that’s why there’s still some flicker-snippets left…)
The manifest.json:
{
"name": "xkcd extension",
"version": "1.0",
"description": "The first xkcd extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"http://www.xkcd.com/"
]
}
The popup.html:
<!doctype html>
<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:300px;
height:300px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script>
<script>
showPhotos();
function showPhotos() {
// var url="http://xkcd.com/info.0.json";
var url = "http://dynamic.xkcd.com/api-0/jsonp/comic/";
alert(url);
// var imgURL;
// $.getJSON(url, function(data) {
// alert(data.img);
// imgURL=data.img;
// });
//alert(imgURL);
var img = document.createElement("image");
alert(img);
img.src = 'http://imgs.xkcd.com/comics/wrong_superhero.png';
alert(img.src);
document.body.appendChild(img);
}
</script>
</head>
<body>
<div id="result">
</div>
</body>
</html>
var img = document.createElement("image");should definitely be
var img = document.createElement("img");