Hide Search Engine ID for Google Custom Search Element API 2.0

user2261248

I am implementing Google Custom Search Element API 2.0 in an ASP.NET MVC project. In the documentation they require that the following javascript is included in the view, with the <gcse:search> element following.

<script>
  (function() {
    var cx = 'xxxxxxxx:yyyyyyyy';
    var gcse = document.createElement('script');
    gcse.type = 'text/javascript';
    gcse.async = true;
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
        '//www.google.com/cse/cse.js?cx=' + cx;
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(gcse, s);
  })();
</script>
<gcse:search></gcse:search>

However, the search engine id is visible on the line:

var cx = 'xxxxxxxx:yyyyyyyy';

In a browser, selecting View Source (or similar) allows the user to view the script and the search engine id.

How do I ensure that no-one can see my id?

user2261248

I have found a way around this. By moving the Google script to its own function that takes the Search Engine Id as a parameter:

function buildSearchElement(cx)
{
    var gcse = document.createElement("script");
    gcse.type = "text/javascript";
    gcse.async = true;
    gcse.src = (document.location.protocol === "https:" ? "https:" : "http:") +
        "//www.google.com/cse/cse.js?cx=" + cx;
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(gcse, s);
};

I then call a method on my controller that returns the Search Engine Id upon page load. When this completes, the callback is the buildSearchElement function:

(function ()
{
    var httpRequest = new XMLHttpRequest();

    httpRequest.onreadystatechange = function()
    {
        if (httpRequest.readyState === 4 && httpRequest.status === 200)
        {
            buildSearchElement(httpRequest.responseText);
        }
    }

    httpRequest.open("GET", "/GetSearchElementId");
    httpRequest.send();
})();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Google App Engine Search API

From Dev

Search API on Google App Engine

From Dev

Search API on Google App Engine

From Dev

Google Custom Search API - Reverse image search

From Dev

How do I get a developer id for google custom search api?

From Dev

Using Typeahead with Google Custom Search Engine

From Dev

Autocomplete custom google search engine for places

From Dev

Autocomplete custom google search engine for places

From Dev

Using Typeahead with Google Custom Search Engine

From Dev

Limit date range in Google Custom Search Engine

From Dev

Android and Google Custom Search API

From Dev

Google Custom Search API javascript

From Dev

Google Custom Search API javascript

From Dev

Google CSE (Custom Search Engine) different search button and fields?

From Dev

Google chrome is redirecting any search result to a custom Search engine

From Dev

How to style Google Custom Search Engine so that it doesn't display as a block element

From Dev

Google App engine Search API Cursor not not updating

From Dev

Which custom search engine?

From Dev

Want to show and hide wordpress page from google search engine

From Dev

Google Custom Search Engine Schema.org structured data and returned results through API

From Dev

Query google search engine?

From Dev

Query google search engine?

From Dev

Executing default query in google custom search engine V2 code

From Dev

Search image on Google images with the new Custom Search API?

From Dev

Search image on Google images with the new Custom Search API?

From Dev

download images with google custom search api

From Dev

Google Custom Search - Query database/API?

From Dev

Google WebSearch API custom search throws TypeErrors

From Dev

Implementing Google custom search API in iOS

Related Related

  1. 1

    Google App Engine Search API

  2. 2

    Search API on Google App Engine

  3. 3

    Search API on Google App Engine

  4. 4

    Google Custom Search API - Reverse image search

  5. 5

    How do I get a developer id for google custom search api?

  6. 6

    Using Typeahead with Google Custom Search Engine

  7. 7

    Autocomplete custom google search engine for places

  8. 8

    Autocomplete custom google search engine for places

  9. 9

    Using Typeahead with Google Custom Search Engine

  10. 10

    Limit date range in Google Custom Search Engine

  11. 11

    Android and Google Custom Search API

  12. 12

    Google Custom Search API javascript

  13. 13

    Google Custom Search API javascript

  14. 14

    Google CSE (Custom Search Engine) different search button and fields?

  15. 15

    Google chrome is redirecting any search result to a custom Search engine

  16. 16

    How to style Google Custom Search Engine so that it doesn't display as a block element

  17. 17

    Google App engine Search API Cursor not not updating

  18. 18

    Which custom search engine?

  19. 19

    Want to show and hide wordpress page from google search engine

  20. 20

    Google Custom Search Engine Schema.org structured data and returned results through API

  21. 21

    Query google search engine?

  22. 22

    Query google search engine?

  23. 23

    Executing default query in google custom search engine V2 code

  24. 24

    Search image on Google images with the new Custom Search API?

  25. 25

    Search image on Google images with the new Custom Search API?

  26. 26

    download images with google custom search api

  27. 27

    Google Custom Search - Query database/API?

  28. 28

    Google WebSearch API custom search throws TypeErrors

  29. 29

    Implementing Google custom search API in iOS

HotTag

Archive