【问题标题】:random image from google set as background using query for image keyword使用图像关键字查询将来自谷歌的随机图像设置为背景
【发布时间】:2018-09-11 17:27:48
【问题描述】:

我有这个代码:

<html>
<head>
    <title></title>
    <script src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load('search', '1');
    google.setOnLoadCallback(OnLoad);
    var search;

    //i suggest instead of this to make keywords list so first to pick random keyword than to do search and pick random image
    var keyword = 'cute kittens';

    function OnLoad()
    {
        search = new google.search.ImageSearch();

        search.setSearchCompleteCallback(this, searchComplete, null);

        search.execute(keyword);
    }

    function searchComplete()
    {
        if (search.results && search.results.length > 0)
        {
            var rnd = Math.floor(Math.random() * search.results.length);

            //you will probably use jQuery and something like: $('body').css('background-image', "url('" + search.results[rnd]['url'] + "')");
            document.body.style.backgroundImage = "url('" + search.results[rnd]['url'] + "')";
        }
    }
    </script>
</head>
<body>

它应该给我一张小猫的照片然后做背景,但是它没有给我任何背景,有人知道如何解决这个问题吗?如果你这样做,请帮忙。这可能看起来像重复,但是我从以前的帖子中获得了代码,但它不起作用,所以请不要标记为重复,我尝试以多种方式修改代码,但没有奏效,当我的时候我丢失了不同的编辑设备关闭,所以我没有这些。 (我是一个糟糕的开发人员,没有保存)

【问题讨论】:

    标签: javascript image random google-image-search


    【解决方案1】:

    该 API 似乎自 2011 年以来已被弃用。Google 可能已完全关闭该服务,因为他们的弃用政策允许他们根据以下条款之一这样做

    如果出现以下情况,Google 保留自行决定立即停止提供服务的全部或任何部分弃用版本的权利,恕不另行通知:

    • 提供已弃用的服务版本可能会造成重大的经济负担,这由 Google 在其合理的善意判断中确定;或
    • 提供已弃用的服务版本可能会造成安全风险或重大技术负担,这由 Google 在其合理的善意判断中确定。

    https://developers.google.com/terms/deprecation

    我把你的代码放在一个 JSfiddle 里:https://jsfiddle.net/zcthpd4L/6/ 并且遇到了错误:

    Uncaught TypeError: Cannot read property 'ImageSearch' of undefined
    at OnLoad ((index):52)
    at (index):70
    

    这似乎不是由于配置问题,很可能是由于该服务不再可用。最好的办法是使用自定义搜索 API:https://developers.google.com/custom-search/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多