https://developer.chrome.com/extensions/getstarted
这个教程还可以吗?我已经下载了所有提到的文件,但扩展程序无效.我认为有一个与Ajax请求有关的问题.
我收到了消息:
无法显示图像.Google Image没有回应..
我试图向另一个网站执行Ajax请求,但事实证明该请求是在本地执行的 - 尽管menifest.json文件中有权限.
在此示例中,在第60行的popup.js内,调用https://ajax.googleapis.com/ajax/services/search/images.这已经过期了.
您可以通过提供查询参数进行验证.例如, https://ajax.googleapis.com/ajax/services/search/images?v = 1.0&q = chrome会回复"此API不再可用.".
此论坛讨论备用搜索API列表. https://groups.google.com/forum/#!topic/Google-AJAX-Search-API/Ao9TbQbYgHo
是的,所以这仍然是破碎的.我修改了popup.js文件(下面的链接)以使用googleapis(在UberHans发布的备选方案中提到).我试图找到PR的源代码回复,但没有这样的运气.
popup.js要求您添加cx和api密钥.如果您阅读了代码,那么您应该非常清楚需要更改的内容以及如何获取所需的cx和api密钥.
https://gist.githubusercontent.com/CrashenX/c4f80340b67e87f13753fb30554f6f01/raw/d8f335d9abe699a63e15d8baec10542e9989a88c/popup.js
更新:如果它有帮助,这里是谷歌提供的和我为使样本工作所做的事情之间的差异(cx和关键变量需要使用有效的cx和密钥来更新):
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);