• We just launched and are currently in beta. Join us as we build and grow the community.

Integrate a Custom Google Programmable Search Engine on Websites

amen541

Reels Creator
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
56
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you can learn how to Integrate a Custom Google Programmable Search Engine for websites using JavaScript. The main purpose of this tutorial is to provide students and beginners with a reference for learning to integrate the google search engine into a website. Here is a simple web page script that demonstrates the integration of the google search engine into a simple website.

What is Google Programmable Search Engine?

The Google Programmable Search Engine was formerly called Google Co-op and Google Custom Search. It is a platform provided by Google that allows web developers to integrate and enhance their website search feature to provide the indexed web pages of their site to their visitors. Google also allows to developers to customize the Search Engine according to the site's needs.

Getting Started

Before we proceed to the coding part, we must create first the custom Programmable Search Engine in Google. To do that follow the instructions below.

How to Create a Google Programmable Search Engine?

  1. Make sure that you have already created a Google Account
  2. Log in to your Google Account in your browser.
  3. Browse https://programmablesearchengine.google.com/
  4. Next, click the Get Start button on the web page.
  5. Next, fill in all the required fields on the form like the following image.
    google-pse-create-1.jpg

  6. Then, Google will generate a code that you will need for integration.
    google-pse-create-2.jpg

  7. Click the Customize button if you want to customize the search engine.

How to use the Google Programmable Search Engine generated Code?

We can simply integrate the Google Programmable Search Engine that we created using the generated code that was provided after the successful creation of our Search Engine. The below script demonstrates the usage of the generated code.

Page Interface

Here's the HTML file script known as index.html. It contains the page layout elements and the generated code. Make sure to change the Search engine ID with the one you created.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Google Programmable Search Engine</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  11. <script

    async src

    =

    "https://cse.google.com/cse.js?cx=03a19a7f685254faf"

    >
  12. </

    script

    >
  13. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >

  14. </

    head

    >
  15. <body

    >
  16. <div

    id

    =

    "wrapper"

    >
  17. <div

    class

    =

    "page-title"

    >

    Google Programmable Search Engine</

    div

    >
  18. <hr

    style

    =

    "margin:auto; width:25px"

    >
  19. <div

    id

    =

    "gpse-wrapper"

    >
  20. <div

    class

    =

    "gcse-search"

    ></

    div

    >
  21. </

    div

    >
  22. </

    div

    >
  23. </

    html

    >

Stylesheet

Here's the custom CSS code for the design of the web page. The file is known as

class="text-danger">style.css.
  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet'

    )

    ;
  2. :

    root

    {
  3. --secular-font

    :

    'Secular One'

    ,

    sans-serif

    ;
  4. }
  5. *

    {
  6. box-sizing

    :

    border-box

    ;
  7. }
  8. body *

    {
  9. font-family

    :

    'Rubik'

    ,

    sans-serif

    ;
  10. }
  11. /**
  12. Page Design
  13. */
  14. body,
  15. html{
  16. height

    :

    100%

    ;
  17. width

    :

    100%

    ;
  18. margin

    :

    0

    ;
  19. padding

    :

    0

    ;
  20. }
  21. body{
  22. background-color

    :

    #B9F3E4

    ;
  23. }
  24. /* Page Wrapper */
  25. #wrapper

    {
  26. width

    :

    100%

    ;
  27. padding

    :

    3em

    5em

    ;
  28. }
  29. .page-title{
  30. font-size

    :

    2.5rem

    ;
  31. font-weight

    :

    500

    ;
  32. color

    :

    #fff

    ;
  33. letter-spacing

    :

    3px

    ;
  34. font-family

    :

    var

    (

    --secular-font

    )

    ;
  35. text-align

    :

    center

    ;
  36. text-shadow

    :

    0px

    0px

    3px

    #2020208c

    ;
  37. }
  38. #gpse-wrapper

    {
  39. position

    :

    relative

    ;
  40. width

    :

    720px

    ;
  41. margin

    :

    auto

    ;
  42. margin-top

    :

    3em

    ;
  43. background

    :

    #fff

    ;
  44. padding

    :

    2em

    2em

    ;
  45. }

Here are the snapshots of the overall result of the script that I provided above.

Google Search Box on the Web Page

Google Search Result on the Web Page

How to Integrate Google Programmable Search Engine using JavaScript?

We can also integrate the Google Programmable Search Engine using JavaScript to place the search engine's search box and result in the custom element or specific area of the page. Check out the sample web page script below to know about how to achieve it using JavaScript.

Page Interface

Here is the HTML file script known as custom.html. It contains the page layout and custom search engine elements.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Google Programmable Search Engine</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  11. <!-- Change cx parameter value with your created custom search engine ID -->
  12. <script

    async src

    =

    "https://cse.google.com/cse.js?cx=03a19a7f685254faf"

    >
  13. </

    script

    >
  14. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >

  15. </

    head

    >
  16. <body

    >
  17. <div

    id

    =

    "wrapper"

    >
  18. <div

    class

    =

    "page-title"

    >

    Google Programmable Search Engine - Custom</

    div

    >
  19. <hr

    style

    =

    "margin:auto; width:25px"

    >
  20. <div

    id

    =

    "gpse-wrapper"

    >
  21. <div

    id

    =

    "gpse-custom-search-box"

    ></

    div

    >
  22. </

    div

    >
  23. <div

    id

    =

    "gpse-result-wrapper"

    >
  24. <div

    id

    =

    "gpse-custom-result-box"

    ></

    div

    >
  25. </

    div

    >
  26. </

    div

    >
  27. <script

    src

    =

    "custom-gpse.js"

    ></

    script

    >
  28. </

    html

    >

Stylesheet

Here's the CSS file script known as style.css. It contains the CSS code for the design of the page layout and custom search engine elements.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet'

    )

    ;
  2. :

    root

    {
  3. --secular-font

    :

    'Secular One'

    ,

    sans-serif

    ;
  4. }
  5. *

    {
  6. box-sizing

    :

    border-box

    ;
  7. }
  8. body *

    {
  9. font-family

    :

    'Rubik'

    ,

    sans-serif

    ;
  10. }
  11. /**
  12. Page Design
  13. */
  14. body,
  15. html{
  16. height

    :

    100%

    ;
  17. width

    :

    100%

    ;
  18. margin

    :

    0

    ;
  19. padding

    :

    0

    ;
  20. }
  21. body{
  22. background-color

    :

    #B9F3E4

    ;
  23. }
  24. /* Page Wrapper */
  25. #wrapper

    {
  26. width

    :

    100%

    ;
  27. padding

    :

    3em

    5em

    ;
  28. }
  29. .page-title{
  30. font-size

    :

    2.5rem

    ;
  31. font-weight

    :

    500

    ;
  32. color

    :

    #fff

    ;
  33. letter-spacing

    :

    3px

    ;
  34. font-family

    :

    var

    (

    --secular-font

    )

    ;
  35. text-align

    :

    center

    ;
  36. text-shadow

    :

    0px

    0px

    3px

    #2020208c

    ;
  37. }
  38. #gpse-wrapper

    {
  39. position

    :

    relative

    ;
  40. width

    :

    720px

    ;
  41. margin

    :

    auto

    ;
  42. margin-top

    :

    3em

    ;
  43. background

    :

    #fff

    ;
  44. padding

    :

    2em

    2em

    ;
  45. }
  46. #gpse-result-wrapper

    {
  47. position

    :

    relative

    ;
  48. width

    :

    720px

    ;
  49. margin

    :

    auto

    ;
  50. margin-top

    :

    1em

    ;
  51. background

    :

    #fff

    ;
  52. padding

    :

    2em

    2em

    ;
  53. }
  54. #gpse-result-wrapper

    :

    has(

    #gpse-custom-result-box

    :

    empty

    )

    {
  55. display

    :

    none

    ;
  56. }

Rendering Google Programmable Search Engine

Here's the JavaScript file script known as custom-gpse.js. It contains the codes that render the search box and search result to the specific page element.

  1. /** Element Selector */
  2. const

    SearchBoxHolder =

    document.getElementById

    (

    'gpse-custom-search-box'

    )
  3. const

    SearchResultHolder =

    document.getElementById

    (

    'gpse-custom-result-box'

    )
  4. var

    params =

    new

    URLSearchParams(

    location.search

    )

  5. window.onload

    =

    function

    (

    )

    {
  6. if

    (

    document.readyState

    ==

    'complete'

    )

    {
  7. /** Render Google Custom Searchbox */
  8. if

    (

    SearchBoxHolder !==

    null

    )
  9. renderSearchBox(

    )

    ;

  10. /** Render Google Custom Result Box if search query exsits */
  11. if

    (

    SearchResultHolder !==

    null

    &&

    params.has

    (

    'search'

    )

    ==

    true

    )

    {
  12. renderResultBox(

    )

    ;
  13. }
  14. }
  15. }
  16. const

    renderSearchBox =

    (

    )

    =>

    {
  17. /** Rendering Searchbox to custom Element */
  18. google.search

    .cse

    .element

    .render

    (
  19. {
  20. div:

    SearchBoxHolder.id

    ,
  21. tag:

    'searchbox-only'

    ,
  22. attributes:

    {
  23. resultsUrl:

    'https://localhost/google-programmable-se/custom.html?search=gpse'
  24. }
  25. }
  26. )

    ;
  27. }

  28. const

    renderResultBox =

    (

    )

    =>

    {
  29. /** Rendering Search Result to custom Element */
  30. google.search

    .cse

    .element

    .render

    (
  31. {
  32. div:

    SearchResultHolder.id

    ,
  33. tag:

    'searchresults-only'
  34. }
  35. )

    ;
  36. }

Here are the snapshots of the overall result of the web page scripts that I provided above.

The search box on the Web Page

Search Result on the Web Page

There you go! I have also provided the complete source code zip file on this site and it is free to download. Feel free to download and modify it. The download button is located below this tutorial's content.

That's it! I hope this Integrate a Custom Google Programmable Search Engine on Websites Tutorial will help you with what you are looking for and will be useful for your current and future website or web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,193

322,229

322,238

Top