am_dot_com

SW20210416

Apr 16th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.91 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.  
  7. <!-- a ordem das inclusões, pelo NOSSO padrão é, na prática irrelevante -->
  8. <!-- ocorre a inclusão de um símbolo não definido $ -->
  9. <script src="utils.js"></script><!-- importação de utilitários genéricos -->
  10. <script src="gs3.js"></script><!-- importação do JavaScript (JS) externalizado -->
  11. <!-- só + tarde é que o símbolo fica definido -->
  12. <!-- depois da inclusão do script utils.js -->
  13. <!-- porque é que não houve erro? Porque há cautelas em gs2.js -->
  14. </head>
  15. <body>
  16. <form
  17. enctype="application/x-www-form-urlencoded"
  18. >
  19. <fieldset>
  20. <legend>Custom Google Search</legend>
  21. <fieldset>
  22. <legend>What to (not) search for</legend>
  23. <label for="idTextSearchExp">Search expression:</label>
  24. <input
  25. id="idTextSearchExp"
  26. type="text"
  27. value="blue mushrooms"
  28. placeholder="your search expression"
  29. >
  30. <br>
  31.  
  32. <label>Terms' order is relevant:</label>
  33. <input
  34. id="idCheckOrderIsRelevant"
  35. type="checkbox"
  36. >
  37. <br>
  38.  
  39. <!-- TODO: must make this work
  40. Expressions to reject
  41. as_eq param
  42. -->
  43. <label>Expressions to reject:</label>
  44. <input
  45. value="buy"
  46. id="idTextExpressionsToReject"
  47. type="text"
  48. placeholder="reject these terms"
  49. >
  50. </fieldset>
  51.  
  52. <fieldset>
  53. <legend>Rank, quantity and safety of results</legend>
  54. <!-- the starting rank of the search results
  55. start param
  56. -->
  57. <label for="idNumberStartingRank">Starting rank of the results:</label>
  58. <input
  59. id="idNumberStartingRank"
  60. value="0"
  61. start="0"
  62. step="10"
  63. type="number">
  64. <br>
  65.  
  66. <!--
  67. num param
  68. -->
  69. <label for="idNumberResultsQuantity">Quantity of results:</label>
  70. <input
  71. id="idNumberResultsQuantity"
  72. type="number"
  73. min="10"
  74. max="100"
  75. value="50"
  76. start="0"
  77. step="10">
  78. <br>
  79.  
  80. <label for="idSelectSafety">Results' safety:</label>
  81. <select id="idSelectSafety">
  82. <option value="images">images</option>
  83. <option value="medium">medium</option>
  84. <option value="high">high</option>
  85. <option selected value="off">off</option>
  86. </select>
  87. <br>
  88. </fieldset>
  89.  
  90. <fieldset>
  91. <legend>Search filters: recency, domain name</legend>
  92. <!-- a set of 2 inputs will support for the recency -->
  93. <fieldset>
  94. <legend>Results' index recency</legend>
  95. <!-- single-line free input -->
  96. <label for="idTextRecency">Custom recency:</label>
  97. <input
  98. id="idTextRecency"
  99. type="text"
  100. value="any"
  101. placeholder="desired recency"
  102. size="6"
  103. >
  104.  
  105. <label for="idSelectRecency">pre-defined values:</label>
  106. <!-- in-built input options -->
  107. <select id="idSelectRecency">
  108. <option value="d1">1 day old</option>
  109. <option value="w1">1 week old</option>
  110. <option value="m1">1 month old</option>
  111. <option value="y1">1 year old</option>
  112. <option selected value="any">any age</option>
  113. </select>
  114. </fieldset>
  115.  
  116. <!-- support for domain name filtering -->
  117. <fieldset>
  118. <legend>Domain name filter</legend>
  119. <!-- free input -->
  120. <label for="idTextDomainFilter">Custom domain name:</label>
  121. <input type="text"
  122. id="idTextDomainFilter"
  123. value="any"
  124. placeholder="domain filter"
  125. size="6"
  126. >
  127.  
  128. <!-- input from predefined values -->
  129. <label for="idSelectDomainFilter">Pre-defined domain hierarchies:</label>
  130. <select id="idSelectDomainFilter"
  131. >
  132. <option value=".pt">.pt</option>
  133. <option value=".de">.de</option>
  134. <option value=".fr">.fr</option>
  135. <option value=".uk">.uk</option>
  136. <option value=".com">.com</option>
  137. <option value=".net">.net</option>
  138. <option value=".gov">.gov</option>
  139. <option value="any" selected>any (no filter)</option>
  140. </select>
  141. </fieldset>
  142. </fieldset>
  143.  
  144. <input
  145. id="idBtnSearch"
  146. type="button"
  147. value="search!"
  148. >
  149. </fieldset>
  150. </form>
  151. </body>
  152. </html>
  153.  
  154.  
  155. **** **** gs3.js **** ****
  156.  
  157. window.onload = boot; //inicialização + comportamentos
  158.  
  159. const GOOGLE_SEARCH_BASE = "https://www.google.com/search?";
  160.  
  161. const
  162. ID_TEXT_SEARCH_EXP = "idTextSearchExp", //better this way, for internal documentation
  163. ID_BTN_SEARCH = "idBtnSearch",
  164. ID_CHECK_ORDER_IS_RELEVANT = "idCheckOrderIsRelevant",
  165. ID_SELECT_SAFETY = "idSelectSafety",
  166. ID_TEXT_EXPRESSIONS_TO_REJECT ="idTextExpressionsToReject",
  167. ID_NUMBER_STARTING_RANK = "idNumberStartingRank",
  168. ID_NUMBER_RESULTS_QUANTITY = "idNumberResultsQuantity",
  169. ID_SELECT_RECENCY = "idSelectRecency",
  170. //2021-04-16
  171. ID_TEXT_RECENCY = "idTextRecency",
  172. ID_TEXT_DOMAIN_FILTER = "idTextDomainFilter",
  173. ID_SELECT_DOMAIN_FILTER = "idSelectDomainFilter"
  174. ;
  175. ;//cascade declaration
  176.  
  177. var oTextSearchExp,
  178. oBtnSearch,
  179. oCheckOrderIsRelevant,
  180. oSelectSafety,
  181. oTextExpressionsToReject,
  182. oNumberStartingRank,
  183. oNumberResultsQuantity,
  184. oSelectRecency,
  185. //2021-04-16
  186. oTextRecency,
  187. oTextDomainFilter,
  188. oSelectDomainFilter
  189. ;
  190.  
  191. function boot(){
  192. //1 - associations
  193. oTextSearchExp = $(ID_TEXT_SEARCH_EXP);
  194. oBtnSearch = $(ID_BTN_SEARCH);
  195. oCheckOrderIsRelevant = $(ID_CHECK_ORDER_IS_RELEVANT);
  196. oSelectSafety = $(ID_SELECT_SAFETY);
  197. oTextExpressionsToReject = $(ID_TEXT_EXPRESSIONS_TO_REJECT);
  198. oNumberStartingRank = $(ID_NUMBER_STARTING_RANK);
  199. oNumberResultsQuantity = $(ID_NUMBER_RESULTS_QUANTITY);
  200. oSelectRecency = $(ID_SELECT_RECENCY);
  201. oTextRecency = $(ID_TEXT_RECENCY);
  202. oTextDomainFilter = $(ID_TEXT_DOMAIN_FILTER);
  203. oSelectDomainFilter = $(ID_SELECT_DOMAIN_FILTER);
  204.  
  205. //TODO: quality-control
  206.  
  207. //2 - set behaviors
  208. /*
  209. oBtnSearch.onclick = function(){
  210. window.alert("Will search for "+oTextSearchExp.value);
  211. }//anonymous function
  212. */
  213.  
  214. oBtnSearch.onclick = willBuildTheRightGoogleQueryAndPerformTheSearch;
  215.  
  216. //using different event handlers
  217. oSelectRecency.onchange = reactToChangeInRecency;
  218. oSelectDomainFilter.onchange = reactToChangeInDomainName;
  219.  
  220. //using the same event handler
  221. oSelectRecency.onchange = oSelectDomainFilter.onchange = reactToSelectChange; //syntax error until reactToSelectChange is defined
  222. }//boot
  223.  
  224. /*
  225. event handler = any function called for processing any event
  226. any event handler will receive as parameter an object that represents the occurred event
  227. */
  228. function reactToSelectChange(
  229. pTheEvent
  230. ){
  231. //pattern for retro-compatibility, for any event handler
  232. var oTheEvent = pTheEvent ? pTheEvent : window.event;//strike - deprecated
  233. var oWhereDidTheEventOccur = oTheEvent.target ? oTheEvent.target : oTheEvent.srcElement;
  234.  
  235. var id = oWhereDidTheEventOccur.id;
  236. switch(id){
  237. case ID_SELECT_RECENCY:
  238. oTextRecency.value = oWhereDidTheEventOccur.value;
  239. break;
  240. case ID_SELECT_DOMAIN_FILTER:
  241. oTextDomainFilter.value = oWhereDidTheEventOccur.value;
  242. break;
  243. }//switch
  244. }//reactToSelectChange
  245.  
  246. function reactToChangeInRecency(){
  247. oTextRecency.value = oSelectRecency.value;
  248. }//reactToSelectChange
  249.  
  250. function reactToChangeInDomainName(){
  251. oTextDomainFilter.value = oSelectDomainFilter.value;
  252. }//reactToChangeInDomainName
  253.  
  254. function willBuildTheRightGoogleQueryAndPerformTheSearch(){
  255. /*
  256. A Google search is performed by calling a base URL
  257. https://www.google.com/search?
  258. and appending it the necessary params
  259. At least, the search expression must be provided:
  260.  
  261. q param - is the search expression when the terms' order is NOT relevant
  262. as_epq param - is the search expression to use, when the terms' order IS relevant
  263.  
  264. Other params:
  265. safety - controls the "safety" of the results (off for no safety)
  266. */
  267.  
  268. var strSearchExpression = oTextSearchExp.value.trim();
  269. var bIsOrderRelevant = oCheckOrderIsRelevant.checked; //true (if it is checked) or false (if NOT checked)
  270.  
  271. //TODO: there is something ugly related to the strSearchExpression
  272. if (bIsOrderRelevant){
  273. //let would NOT work in this code
  274. var strQuery = "as_epq="+encodeURIComponent(strSearchExpression);
  275. }
  276. else{
  277. //let would NOT work in this code
  278. var strQuery = "q="+encodeURIComponent(strSearchExpression);
  279. }
  280.  
  281. //are there expressions to reject?
  282. var bThereAreExpresssionsToReject = oTextExpressionsToReject.value.trim() !== "";
  283. if (bThereAreExpresssionsToReject){
  284. /*
  285. +=
  286. operador concatenação cumulativa
  287. a expressão torna-se o que já valia, acrescentada da expressão direita
  288. */
  289. var strExpressionsToReject = oTextExpressionsToReject.value.trim();
  290. strQuery+="&as_eq="+encodeURIComponent(strExpressionsToReject);
  291. }//if
  292.  
  293. //starting rank - start param
  294. var nStartingRank = Number(oNumberStartingRank.value.trim());
  295. strQuery+="&start="+nStartingRank
  296.  
  297. //quantity
  298. var nQuantity = Number(oNumberResultsQuantity.value.trim());
  299. strQuery+="&num="+nQuantity;
  300.  
  301. strQuery+="&safety="+oSelectSafety.value;
  302.  
  303. //recency
  304. /*
  305. var bConsiderRecency = oSelectRecency.value.trim()!=="any";
  306. if (bConsiderRecency){
  307. strQuery+="&as_qdr="+oSelectRecency.value.trim();
  308. }
  309. */
  310. var bConsiderRecency = oTextRecency.value.trim()!=="any" //TODO: and different from the empty;
  311. if (bConsiderRecency){
  312. strQuery+="&as_qdr="+oTextRecency.value.trim();
  313. }
  314.  
  315. /*
  316. domain name filtering controlled by as_sitesearch param
  317. */
  318. var strUserDomainName = oTextDomainFilter.value.trim();
  319. var bConsiderDomainNameFiltering = strUserDomainName!="any" //TODO: and different from the empty;
  320. if (bConsiderDomainNameFiltering){
  321. strQuery+="&as_sitesearch="+strUserDomainName;
  322. }
  323. var strSearchUrl = GOOGLE_SEARCH_BASE+strQuery;
  324.  
  325. window.document.location.href=strSearchUrl;
  326. }//willBuildTheRightGoogleQueryAndPerformTheSearch
  327.  
  328. **** utils.js ****
  329.  
  330. //code library
  331. function $(pId){
  332. return document.getElementById(pId);
  333. }//$
Advertisement
Add Comment
Please, Sign In to add comment