Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>My Google Searcher</title>
- <script>
- window.onload = boot;
- const GOOGLE_SEARCH_BASE = "https://www.google.com/search?";
- const
- ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
- ID_BTN_SEARCH = "idBtnSearch",
- ID_CHECK_ORDER_IS_RELEVANT = "idCheckOrderIsRelevant",
- ID_SELECT_SAFETY = "idSelectSafety",
- ID_TEXT_EXPRESSIONS_TO_REJECT ="idTextExpressionsToReject",
- ID_NUMBER_STARTING_RANK = "idNumberStartingRank",
- ID_NUMBER_RESULTS_QUANTITY = "idNumberResultsQuantity",
- ID_SELECT_RECENCY = "idSelectRecency"
- ;//cascade declaration
- var oTextSearchExp,
- oBtnSearch,
- oCheckOrderIsRelevant,
- oSelectSafety,
- oTextExpressionsToReject,
- oNumberStartingRank,
- oNumberResultsQuantity,
- oSelectRecency
- ;
- function boot(){
- //1 - associations
- oTextSearchExp = document.getElementById(ID_TEXT_SEARCH_EXP);
- oBtnSearch = document.getElementById(ID_BTN_SEARCH);
- oCheckOrderIsRelevant = document.getElementById(ID_CHECK_ORDER_IS_RELEVANT);
- oSelectSafety = document.getElementById(ID_SELECT_SAFETY);
- oTextExpressionsToReject = document.getElementById(ID_TEXT_EXPRESSIONS_TO_REJECT);
- oNumberStartingRank = document.getElementById(ID_NUMBER_STARTING_RANK);
- oNumberResultsQuantity = document.getElementById(ID_NUMBER_RESULTS_QUANTITY);
- oSelectRecency = document.getElementById(ID_SELECT_RECENCY);
- //TODO: quality-control
- //2 - set behaviors
- /*
- oBtnSearch.onclick = function(){
- window.alert("Will search for "+oTextSearchExp.value);
- }//anonymous function
- */
- oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
- }//boot
- function willBuildTheRightGoogleQueryAndPerformTheSearch(){
- /*
- A Google search is performed by calling a base URL
- https://www.google.com/search?
- and appending it the necessary params
- At least, the search expression must be provided:
- q param - is the search expression when the terms' order is NOT relevant
- as_epq param - is the search expression to use, when the terms' order IS relevant
- Other params:
- safety - controls the "safety" of the results (off for no safety)
- */
- var strSearchExpression = oTextSearchExp.value.trim();
- var bIsOrderRelevant = oCheckOrderIsRelevant.checked; //true (if it is checked) or false (if NOT checked)
- //TODO: there is something ugly related to the strSearchExpression
- if (bIsOrderRelevant){
- //let would NOT work in this code
- var strQuery = "as_epq="+encodeURIComponent(strSearchExpression);
- }
- else{
- //let would NOT work in this code
- var strQuery = "q="+encodeURIComponent(strSearchExpression);
- }
- //are there expressions to reject?
- var bThereAreExpresssionsToReject = oTextExpressionsToReject.value.trim() !== "";
- if (bThereAreExpresssionsToReject){
- /*
- +=
- operador concatenação cumulativa
- a expressão torna-se o que já valia, acrescentada da expressão direita
- */
- var strExpressionsToReject = oTextExpressionsToReject.value.trim();
- strQuery+="&as_eq="+encodeURIComponent(strExpressionsToReject);
- }//if
- //starting rank - start param
- var nStartingRank = Number(oNumberStartingRank.value.trim());
- strQuery+="&start="+nStartingRank
- //quantity
- var nQuantity = Number(oNumberResultsQuantity.value.trim());
- strQuery+="&num="+nQuantity;
- strQuery+="&safety="+oSelectSafety.value;
- //recency
- var bConsiderRecency = oSelectRecency.value.trim()!=="any";
- if (bConsiderRecency){
- strQuery+="&as_qdr="+oSelectRecency.value.trim();
- }
- var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
- window.document.location.href=strSearchUrl;
- }//willBuildTheRightGoogleQueryAndPerformTheSearch
- </script>
- </head>
- <body>
- <form
- enctype="application/x-www-form-urlencoded"
- >
- <fieldset>
- <legend>Custom Google Search</legend>
- <fieldset>
- <legend>What to (not) search for</legend>
- <label for="idTextSearchExp">Search expression:</label>
- <input
- id="idTextSearchExp"
- type="text"
- value="blue mushrooms"
- placeholder="your search expression"
- >
- <br>
- <label>Terms' order is relevant:</label>
- <input
- id="idCheckOrderIsRelevant"
- type="checkbox"
- >
- <br>
- <!-- TODO: must make this work
- Expressions to reject
- as_eq param
- -->
- <label>Expressions to reject:</label>
- <input
- value="buy"
- id="idTextExpressionsToReject"
- type="text"
- placeholder="reject these terms"
- >
- </fieldset>
- <fieldset>
- <legend>Rank, quantity and safety of results</legend>
- <!-- the starting rank of the search results
- start param
- -->
- <label for="idNumberStartingRank">Starting rank of the results:</label>
- <input
- id="idNumberStartingRank"
- value="0"
- start="0"
- step="10"
- type="number">
- <br>
- <!--
- num param
- -->
- <label for="idNumberResultsQuantity">Quantity of results:</label>
- <input
- id="idNumberResultsQuantity"
- type="number"
- min="10"
- max="100"
- value="50"
- start="0"
- step="10">
- <br>
- <label for="idSelectSafety">Results' safety:</label>
- <select id="idSelectSafety">
- <option value="images">images</option>
- <option value="medium">medium</option>
- <option value="high">high</option>
- <option selected value="off">off</option>
- </select>
- <br>
- <label for="idSelectRecency">Results' indexing recency:</label>
- <select id="idSelectRecency">
- <option value="d1">1 day old</option>
- <option value="w1">1 week old</option>
- <option value="m1">1 month old</option>
- <option value="y1">1 year old</option>
- <option selected value="any">any age</option>
- </select>
- </fieldset>
- <input
- id="idBtnSearch"
- type="button"
- value="search!"
- >
- </fieldset>
- </form>
- </body>
- </html>
Add Comment
Please, Sign In to add comment