Chrome扩展程序不支持代码?

时间:2012-05-31 15:17:11

标签: javascript html google-chrome-extension

在这里提出2个问题之后,我终于完善了我的Chrome扩展程序。我根据教程网站上的一个示例来制作它供个人使用,我的版本要做的是从用户那里获取查询输入,转到Flickr的API并通过搜索该查询返回24个图像。我打开页面,好吧,一个页面,它完美地运行。但是当我尝试将其作为扩展名打开时,无论用户输入什么内容,查询字词都不会更改。因此,我得出结论,在Chrome扩展中不支持某些代码,或者我正在做一些可怕的错误。如果前者是正确的,你能指出我能做什么,不能在扩展中使用(或者把我链接到有答案的地方)?注意:是的,我知道服务器端语言不能完全正常工作。但是,如果我正在做一些愚蠢的事情,请告诉我,如果可能的话,请帮我解决这个问题。提前感谢您提供的任何帮助。代码如下:

JS(popup.js):

var q = "cake"; //Default search term
var req;
function querySubmit() {
oForm = document.forms["queryForm"];
oText = oForm.elements["query"];
q = oText.value
document.getElementById("images").innerHTML = "";
req.open(
"GET",
"http://api.flickr.com/services/rest/?" +
    "method=flickr.photos.search&" +
    "api_key=90485e931f687a9b9c2a66bf58a3861a&" +
    "text=" + q + "&" +
    "safe_search=1&" +  
    "content_type=1&" +  
    "sort=relevance&" +  
    "per_page=24",
true);
req.onload = showPhotos;
req.send(null);}

var req = new XMLHttpRequest();
req.open(
"GET",
"http://api.flickr.com/services/rest/?" +
    "method=flickr.photos.search&" +
    "api_key=90485e931f687a9b9c2a66bf58a3861a&" +
    "text=" + q + "&" +
    "safe_search=1&" +  
    "content_type=1&" +  
    "sort=relevance&" +  
    "per_page=24",
true);
req.onload = showPhotos;
req.send(null);


function showPhotos() {
  var photos = req.responseXML.getElementsByTagName("photo");

  for (var i = 0, photo; photo = photos[i]; i++) {
    var a = document.createElement("a");
    a.setAttribute("href",constructImageURL(photo));
    a.setAttribute("target","_blank");

    var img = document.createElement("img");
    img.setAttribute("src",constructImageURL(photo));

    a.appendChild(img);
    document.getElementById("images").appendChild(a);
  }
}

function constructImageURL(photo) {
  return "http://farm" + photo.getAttribute("farm") +
  ".static.flickr.com/" + photo.getAttribute("server") +
  "/" + photo.getAttribute("id") +
  "_" + photo.getAttribute("secret") +
  "_s.jpg";
}

HTML(popup.html):

<!DOCTYPE html>
<html>
<head>
<title>Teh 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>
<!-- JavaScript and HTML must be in separate files for security. -->
<script src="popup.js"></script>
</head>
<body>
<div id="images">
</div>
<form name="queryForm" onsubmit="querySubmit();return false" action="#">
Search: <input type='text' name='query'>
<input type="submit" />
</form>
</body>
</html>

的manifest.json:

{
  "name": "Flickr image searcher",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Searches images on Flickr wirtout opening another page.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "results.html"
  },
  "permissions": [
    "http://api.flickr.com/"
  ]
}

1 个答案:

答案 0 :(得分:1)

HTML中的onsubmit处理程序是内联JavaScript,manifest_version: 2中不允许这样做。

相反,在JS文件中使用addEventListenersubmit事件处理函数绑定到表单:

theForm.addEventListener("submit", function() {
    //...
    return false; // stop submission
});