https://developer.chrome.com/extensions/getstarted
这个教程还可以吗?我已经下载了所有提到的文件,但扩展程序无效。我认为有一个与Ajax请求有关的问题。
我收到了消息:
无法显示图像。 Google Image没有回复..
我尝试向另一个网站执行Ajax请求,但事实证明该请求是在本地执行的 - 尽管menifest.json文件中有权限。
答案 0 :(得分:5)
popup.js要求您添加cx和api密钥。如果您阅读了代码,那么您应该非常清楚需要更改的内容以及如何获取所需的cx和api密钥。
更新:如果有帮助,这里是google提供的内容与我为使示例工作所做的事情之间的差异(需要使用有效的cx和密钥更新cx和key vars):
59,61c59,66
< // https://developers.google.com/image-search/
< var searchUrl = 'https://ajax.googleapis.com/ajax/services/search/images' +
< '?v=1.0&q=' + encodeURIComponent(searchTerm);
---
> // https://developers.google.com/custom-search/json-api/v1/using_rest
> var cx = 'insert-your-cx-from:https://cse.google.com'
> // WARNING: Hard-coding your api key in code is really insecure
> var key = 'insert-your-key-from:https://console.developers.google.com'
> var searchUrl = 'https://www.googleapis.com/customsearch/v1?searchType=image'
> + '&cx=' + encodeURIComponent(cx)
> + '&key=' + encodeURIComponent(key)
> + '&q=' + encodeURIComponent(searchTerm);
69,72c74,76
< if (!response || !response.responseData || !response.responseData.results ||
< response.responseData.results.length === 0) {
< errorCallback('No response from Google Image search!');
< return;
---
> if (!response || !response.items || !response.items.length) {
> errorCallback('No response from Google Image search')
> return;
74c78
< var firstResult = response.responseData.results[0];
---
> var firstResult = response.items[0];
77,79c81,83
< var imageUrl = firstResult.tbUrl;
< var width = parseInt(firstResult.tbWidth);
< var height = parseInt(firstResult.tbHeight);
---
> var imageUrl = firstResult.image.thumbnailLink;
> var width = parseInt(firstResult.image.thumbnailWidth);
> var height = parseInt(firstResult.image.thumbnailHeight);
答案 1 :(得分:1)