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>
- <!-- a ordem das inclusões, pelo NOSSO padrão é, na prática irrelevante -->
- <!-- ocorre a inclusão de um símbolo não definido $ -->
- <script src="utils.js"></script><!-- importação de utilitários genéricos -->
- <script src="gs3.js"></script><!-- importação do JavaScript (JS) externalizado -->
- <!-- só + tarde é que o símbolo fica definido -->
- <!-- depois da inclusão do script utils.js -->
- <!-- porque é que não houve erro? Porque há cautelas em gs2.js -->
- </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>
- </fieldset>
- <fieldset>
- <legend>Search filters: recency, domain name</legend>
- <!-- a set of 2 inputs will support for the recency -->
- <fieldset>
- <legend>Results' index recency</legend>
- <!-- single-line free input -->
- <label for="idTextRecency">Custom recency:</label>
- <input
- id="idTextRecency"
- type="text"
- value="any"
- placeholder="desired recency"
- size="6"
- >
- <label for="idSelectRecency">pre-defined values:</label>
- <!-- in-built input options -->
- <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>
- <!-- support for domain name filtering -->
- <fieldset>
- <legend>Domain name filter</legend>
- <!-- free input -->
- <label for="idTextDomainFilter">Custom domain name:</label>
- <input type="text"
- id="idTextDomainFilter"
- value="any"
- placeholder="domain filter"
- size="6"
- >
- <!-- input from predefined values -->
- <label for="idSelectDomainFilter">Pre-defined domain hierarchies:</label>
- <select id="idSelectDomainFilter"
- >
- <option value=".pt">.pt</option>
- <option value=".de">.de</option>
- <option value=".fr">.fr</option>
- <option value=".uk">.uk</option>
- <option value=".com">.com</option>
- <option value=".net">.net</option>
- <option value=".gov">.gov</option>
- <option value="any" selected>any (no filter)</option>
- </select>
- </fieldset>
- </fieldset>
- <input
- id="idBtnSearch"
- type="button"
- value="search!"
- >
- </fieldset>
- </form>
- </body>
- </html>
- **** **** gs3.js **** ****
- window.onload = boot; //inicialização + comportamentos
- 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",
- //2021-04-16
- ID_TEXT_RECENCY = "idTextRecency",
- ID_TEXT_DOMAIN_FILTER = "idTextDomainFilter",
- ID_SELECT_DOMAIN_FILTER = "idSelectDomainFilter"
- ;
- ;//cascade declaration
- var oTextSearchExp,
- oBtnSearch,
- oCheckOrderIsRelevant,
- oSelectSafety,
- oTextExpressionsToReject,
- oNumberStartingRank,
- oNumberResultsQuantity,
- oSelectRecency,
- //2021-04-16
- oTextRecency,
- oTextDomainFilter,
- oSelectDomainFilter
- ;
- function boot(){
- //1 - associations
- oTextSearchExp = $(ID_TEXT_SEARCH_EXP);
- oBtnSearch = $(ID_BTN_SEARCH);
- oCheckOrderIsRelevant = $(ID_CHECK_ORDER_IS_RELEVANT);
- oSelectSafety = $(ID_SELECT_SAFETY);
- oTextExpressionsToReject = $(ID_TEXT_EXPRESSIONS_TO_REJECT);
- oNumberStartingRank = $(ID_NUMBER_STARTING_RANK);
- oNumberResultsQuantity = $(ID_NUMBER_RESULTS_QUANTITY);
- oSelectRecency = $(ID_SELECT_RECENCY);
- oTextRecency = $(ID_TEXT_RECENCY);
- oTextDomainFilter = $(ID_TEXT_DOMAIN_FILTER);
- oSelectDomainFilter = $(ID_SELECT_DOMAIN_FILTER);
- //TODO: quality-control
- //2 - set behaviors
- /*
- oBtnSearch.onclick = function(){
- window.alert("Will search for "+oTextSearchExp.value);
- }//anonymous function
- */
- oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
- //using different event handlers
- oSelectRecency.onchange = reactToChangeInRecency;
- oSelectDomainFilter.onchange = reactToChangeInDomainName;
- //using the same event handler
- oSelectRecency.onchange = oSelectDomainFilter.onchange = reactToSelectChange; //syntax error until reactToSelectChange is defined
- }//boot
- /*
- event handler = any function called for processing any event
- any event handler will receive as parameter an object that represents the occurred event
- */
- function reactToSelectChange(
- pTheEvent
- ){
- //pattern for retro-compatibility, for any event handler
- var oTheEvent = pTheEvent ? pTheEvent : window.event;//strike - deprecated
- var oWhereDidTheEventOccur = oTheEvent.target ? oTheEvent.target : oTheEvent.srcElement;
- var id = oWhereDidTheEventOccur.id;
- switch(id){
- case ID_SELECT_RECENCY:
- oTextRecency.value = oWhereDidTheEventOccur.value;
- break;
- case ID_SELECT_DOMAIN_FILTER:
- oTextDomainFilter.value = oWhereDidTheEventOccur.value;
- break;
- }//switch
- }//reactToSelectChange
- function reactToChangeInRecency(){
- oTextRecency.value = oSelectRecency.value;
- }//reactToSelectChange
- function reactToChangeInDomainName(){
- oTextDomainFilter.value = oSelectDomainFilter.value;
- }//reactToChangeInDomainName
- 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 bConsiderRecency = oTextRecency.value.trim()!=="any" //TODO: and different from the empty;
- if (bConsiderRecency){
- strQuery+="&as_qdr="+oTextRecency.value.trim();
- }
- /*
- domain name filtering controlled by as_sitesearch param
- */
- var strUserDomainName = oTextDomainFilter.value.trim();
- var bConsiderDomainNameFiltering = strUserDomainName!="any" //TODO: and different from the empty;
- if (bConsiderDomainNameFiltering){
- strQuery+="&as_sitesearch="+strUserDomainName;
- }
- var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
- window.document.location.href=strSearchUrl;
- }//willBuildTheRightGoogleQueryAndPerformTheSearch
- **** utils.js ****
- //code library
- function $(pId){
- return document.getElementById(pId);
- }//$
Advertisement
Add Comment
Please, Sign In to add comment