am_dot_com

SW20210414

Apr 14th, 2021 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 7.81 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>My Google Searcher</title>
  6.     <script>
  7.         window.onload = boot;
  8.  
  9.         const GOOGLE_SEARCH_BASE = "https://www.google.com/search?";
  10.  
  11.         const
  12.             ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
  13.             ID_BTN_SEARCH = "idBtnSearch",
  14.             ID_CHECK_ORDER_IS_RELEVANT = "idCheckOrderIsRelevant",
  15.             ID_SELECT_SAFETY = "idSelectSafety",
  16.             ID_TEXT_EXPRESSIONS_TO_REJECT ="idTextExpressionsToReject",
  17.             ID_NUMBER_STARTING_RANK = "idNumberStartingRank",
  18.             ID_NUMBER_RESULTS_QUANTITY = "idNumberResultsQuantity",
  19.             ID_SELECT_RECENCY = "idSelectRecency"
  20.             ;//cascade declaration
  21.  
  22.         var oTextSearchExp,
  23.             oBtnSearch,
  24.             oCheckOrderIsRelevant,
  25.             oSelectSafety,
  26.             oTextExpressionsToReject,
  27.             oNumberStartingRank,
  28.             oNumberResultsQuantity,
  29.             oSelectRecency
  30.             ;
  31.  
  32.         function boot(){
  33.             //1 - associations
  34.             oTextSearchExp = document.getElementById(ID_TEXT_SEARCH_EXP);
  35.             oBtnSearch = document.getElementById(ID_BTN_SEARCH);
  36.             oCheckOrderIsRelevant = document.getElementById(ID_CHECK_ORDER_IS_RELEVANT);
  37.             oSelectSafety = document.getElementById(ID_SELECT_SAFETY);
  38.             oTextExpressionsToReject = document.getElementById(ID_TEXT_EXPRESSIONS_TO_REJECT);
  39.             oNumberStartingRank = document.getElementById(ID_NUMBER_STARTING_RANK);
  40.             oNumberResultsQuantity = document.getElementById(ID_NUMBER_RESULTS_QUANTITY);
  41.             oSelectRecency = document.getElementById(ID_SELECT_RECENCY);
  42.             //TODO: quality-control
  43.  
  44.             //2 - set behaviors
  45.             /*
  46.             oBtnSearch.onclick = function(){
  47.                 window.alert("Will search for "+oTextSearchExp.value);
  48.             }//anonymous function
  49.              */
  50.  
  51.             oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
  52.         }//boot
  53.  
  54.         function willBuildTheRightGoogleQueryAndPerformTheSearch(){
  55.             /*
  56.             A Google search is performed by calling a base URL
  57.             https://www.google.com/search?
  58.             and appending it the necessary params
  59.             At least, the search expression must be provided:
  60.  
  61.             q param - is the search expression when the terms' order is NOT relevant
  62.             as_epq param - is the search expression to use, when the terms' order IS relevant
  63.  
  64.             Other params:
  65.             safety - controls the "safety" of the results (off for no safety)
  66.              */
  67.  
  68.             var strSearchExpression = oTextSearchExp.value.trim();
  69.             var bIsOrderRelevant = oCheckOrderIsRelevant.checked; //true (if it is checked) or false (if NOT checked)
  70.  
  71.             //TODO: there is something ugly related to the strSearchExpression
  72.             if (bIsOrderRelevant){
  73.                 //let would NOT work in this code
  74.                 var strQuery = "as_epq="+encodeURIComponent(strSearchExpression);
  75.             }
  76.             else{
  77.                 //let would NOT work in this code
  78.                 var strQuery = "q="+encodeURIComponent(strSearchExpression);
  79.             }
  80.  
  81.             //are there expressions to reject?
  82.             var bThereAreExpresssionsToReject = oTextExpressionsToReject.value.trim() !== "";
  83.             if (bThereAreExpresssionsToReject){
  84.                 /*
  85.                 +=
  86.                 operador concatenação cumulativa
  87.                 a expressão torna-se o que já valia, acrescentada da expressão direita
  88.                  */
  89.                 var strExpressionsToReject = oTextExpressionsToReject.value.trim();
  90.                 strQuery+="&as_eq="+encodeURIComponent(strExpressionsToReject);
  91.             }//if
  92.  
  93.             //starting rank - start param
  94.             var nStartingRank = Number(oNumberStartingRank.value.trim());
  95.             strQuery+="&start="+nStartingRank
  96.  
  97.            //quantity
  98.            var nQuantity = Number(oNumberResultsQuantity.value.trim());
  99.             strQuery+="&num="+nQuantity;
  100.  
  101.             strQuery+="&safety="+oSelectSafety.value;
  102.  
  103.             //recency
  104.             var bConsiderRecency = oSelectRecency.value.trim()!=="any";
  105.             if (bConsiderRecency){
  106.                 strQuery+="&as_qdr="+oSelectRecency.value.trim();
  107.             }
  108.  
  109.             var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
  110.  
  111.             window.document.location.href=strSearchUrl;
  112.         }//willBuildTheRightGoogleQueryAndPerformTheSearch
  113.     </script>
  114. </head>
  115. <body>
  116.     <form
  117.        enctype="application/x-www-form-urlencoded"
  118.    >
  119.         <fieldset>
  120.             <legend>Custom Google Search</legend>
  121.             <fieldset>
  122.                 <legend>What to (not) search for</legend>
  123.                 <label for="idTextSearchExp">Search expression:</label>
  124.                 <input
  125.                    id="idTextSearchExp"
  126.                    type="text"
  127.                    value="blue mushrooms"
  128.                    placeholder="your search expression"
  129.                >
  130.                 <br>
  131.  
  132.                 <label>Terms' order is relevant:</label>
  133.                 <input
  134.                    id="idCheckOrderIsRelevant"
  135.                    type="checkbox"
  136.                >
  137.                 <br>
  138.  
  139.                 <!-- TODO: must make this work
  140.                Expressions to reject
  141.                as_eq param
  142.                -->
  143.                 <label>Expressions to reject:</label>
  144.                 <input
  145.                    value="buy"
  146.                    id="idTextExpressionsToReject"
  147.                    type="text"
  148.                    placeholder="reject these terms"
  149.                >
  150.             </fieldset>
  151.  
  152.             <fieldset>
  153.                 <legend>Rank, quantity and safety of results</legend>
  154.                 <!-- the starting rank of the search results
  155.                start param
  156.                -->
  157.                 <label for="idNumberStartingRank">Starting rank of the results:</label>
  158.                 <input
  159.                    id="idNumberStartingRank"
  160.                    value="0"
  161.                    start="0"
  162.                    step="10"
  163.                    type="number">
  164.                 <br>
  165.  
  166.                 <!--
  167.                num param
  168.                -->
  169.                 <label for="idNumberResultsQuantity">Quantity of results:</label>
  170.                 <input
  171.                    id="idNumberResultsQuantity"
  172.                    type="number"
  173.                    min="10"
  174.                    max="100"
  175.                    value="50"
  176.                    start="0"
  177.                    step="10">
  178.                 <br>
  179.  
  180.                 <label for="idSelectSafety">Results' safety:</label>
  181.                 <select id="idSelectSafety">
  182.                     <option value="images">images</option>
  183.                     <option value="medium">medium</option>
  184.                     <option value="high">high</option>
  185.                     <option selected value="off">off</option>
  186.                 </select>
  187.                 <br>
  188.  
  189.                 <label for="idSelectRecency">Results' indexing recency:</label>
  190.                 <select id="idSelectRecency">
  191.                     <option value="d1">1 day old</option>
  192.                     <option value="w1">1 week old</option>
  193.                     <option value="m1">1 month old</option>
  194.                     <option value="y1">1 year old</option>
  195.                     <option selected value="any">any age</option>
  196.                 </select>
  197.             </fieldset>
  198.  
  199.             <input
  200.                    id="idBtnSearch"
  201.                    type="button"
  202.                    value="search!"
  203.            >
  204.         </fieldset>
  205.     </form>
  206. </body>
  207. </html>
Add Comment
Please, Sign In to add comment