Skip to Content

tryGCSESearch()

Run a search through Google Custom Search Engine for the website.

JavaScript July 2, 2019

Usage

JavaScript
nebula.tryGCSESearch(phrase)

Parameters

phrase
(Required) (String) The keyphrase to search Google for
Default: None

Request or provide clarification »

Additional Notes

Both the Google Custom Search Engine ID and Google Browser API Key are required in Nebula Options.

This search will only search the current site, but the results are external (from Google’s indexes) rather than internal (WordPress DB).

Source File

Located in /assets/js/nebula.js on line 2314.

Note: This function contains 1 to-do comment.

JavaScript
nebula.tryGCSESearch = function(phrase){
    if ( nebula.site.options.nebula_cse_id.length && nebula.site.options.nebula_google_browser_api_key.length ){
        var queryParams = {
            cx: nebula.site.options.nebula_cse_id,
            key: nebula.site.options.nebula_google_browser_api_key,
            num: 10,
            q: phrase,
            alt: 'JSON'
        };
        var API_URL = 'https://www.googleapis.com/customsearch/v1?';

        // Send the request to the custom search API
        jQuery.getJSON(API_URL, queryParams, function(response){
            if ( response.items && response.items.length ){ //@todo "Nebula" 0: Use optional chaining
                if ( response.items[0].link !== window.location.href ){
                    nebula.showSuggestedGCSEPage(response.items[0].title, response.items[0].link);
                }
            }
        });
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.tryGCSESearch = function(phrase){
    //Write your own code here, leave it blank, or return false.
}