Javascript Google自定义搜索引擎

问题描述 投票:0回答:1

问题-错误

我收到此错误,

  • 错误:CSE.js:130 Uncaught ReferenceError: google is not defined

问题-描述

  • 但是,如果我在Web工具中使用chrome控制台,则如果使用google.search....,它将起作用。

  • 不确定我的代码为何不加载google,请提供帮助。

代码

    // Generate CSE Script
    let scr  = document.createElement('script'),
    head = document.head || document.getElementsByTagName('head')[0];
    scr.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cse_cx;
    scr.async = false; // optionally
    head.insertBefore(scr, head.firstChild);

    // Get Results Element
    let element = google.search.cse.element.getElement('searchresults-only0');

执行代码后,等等。

  • 这样做会起作用

enter image description here

javascript google-chrome google-custom-search
1个回答
0
投票

因此添加“延迟”有效,

动态注入脚本后,我用setTimeout(function(){ /*CODE HERE*/ }, 1000);包裹了代码。

© www.soinside.com 2019 - 2024. All rights reserved.