简体   繁体   English

如何获得用户点击搜索引擎结果页面的内容?

[英]How to get what users click on the search engine result page?

I am creating a react project and using google search engine to allow users to search on google in my page. 我正在创建一个反应项目,并使用谷歌搜索引擎来允许用户在我的页面上搜索谷歌。 But I don't know how to get what users click on the search results page. 但是我不知道如何让用户点击搜索结果页面。 Below is my source code: 下面是我的源代码:

componentDidMount() {
    const myCallback = function() {
      if (document.readyState === 'complete') {
        google.search.cse.element.render(
          {
            div: 'root',
            tag: 'search',
          });
      } else {
        google.setOnLoadCallback(() => {
          google.search.cse.element.render(
            {
              div: 'root',
              tag: 'search',
            });
        }, true);
      }
    };

    window.__gcse = {
      parsetags: 'explicit',
      callback: myCallback,
    };

    (function() {
      const cx = '015923670062127816633:npuitytcpqy';
      const gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = `https://cse.google.com/cse.js?cx=${cx}&searchType=image`;
      // gcse.src = `https://www.googleapis.com/customsearch/v1?key=YOUR_API_KEY&cx=${cs}&q=flower&searchType=image&fileType=jpg&imgSize=small&alt=json`;
      const s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    }());
    console.log(window.__gcse);
  }

  render() {
    return (<div className="content">
      <div className="gcse-searchbox" resultsUrl="http://www.google.com" newWindow="true" queryParameterName="search" />
    </div>);
  }

Users can search on the search box and it will show the search results in a popup dialog. 用户可以在搜索框中进行搜索,它将在弹出对话框中显示搜索结果。 It will open a new browser tag if users click one of the search results. 如果用户单击搜索结果之一,它将打开一个新的浏览器标签。 Now I want to prevent opening a new tab, instead get what users clicked and do something in my page. 现在,我要防止打开一个新选项卡,而是获取用户单击的内容并在我的页面中执行操作。 For example, if users click an image, I'd like to get the image link and render this image on my component. 例如,如果用户单击图像,我想获得图像链接并将该图像呈现在我的组件上。 How can I implement it through react? 如何通过反应来实现?

First, visit the Google Custom Search Control Panel. 首先,访问Google自定义搜索控制面板。 After selecting the relevant search engine, click on Look and feel . 选择相关的搜索引擎后,点击外观 Make sure the Layout tab is selected, and choose the Full-width layout (or perhaps one of the others depending on how exactly you want it to look on your page). 确保选择了“ 布局”选项卡,然后选择“ 全角”布局(或者可能选择其他一种,具体取决于您希望它在页面上显示的外观如何)。 Finally, click on Save & Get Code and use the code it provides. 最后,单击“ 保存并获取代码”并使用其提供的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM