Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.77 KB | None | 0 0
  1. // ==UserScript==
  2. // @name MultiSearch Helper for DaveBot
  3. // @namespace http://terr590.doesntownanyurls.shit/
  4. // @version 0.5.4
  5. // @description Searches question and highlights answers to make them easily recognizable
  6. // @author Terr
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.js
  8. // @match https://multisearchtabs.github.io/*
  9. // @include https://www.google.*
  10. // ==/UserScript==
  11.  
  12. var resultCount = 0;
  13. var question, temp, i = 0, useOG = false;
  14. function searchEach (zEvent) {
  15. console.log(zEvent)
  16. userInput = encodeURI(document.getElementById("input").value + " ");
  17. question = getParameterByName('q');
  18. params = '?whichDetected=' + whichEdit(question);
  19. if(opt[0]) params += '&option1=' + userInput + opt[0];
  20. if(opt[1]) params += '&option2=' + userInput+ opt[1];
  21. if(opt[2]) params += '&option3=' + userInput+ opt[2];
  22. window.location.href = 'http://www.google.com' + params
  23. }
  24.  
  25. function getParameterByName(name, url)
  26. {
  27. if (!url) url = window.location.href;
  28. name = name.replace(/[\[\]]/g, "\\$&");
  29. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  30. results = regex.exec(url);
  31. if (!results) return null;
  32. if (!results[2]) return '';
  33. return decodeURIComponent(results[2].replace(/\+/g, " "));
  34. }
  35.  
  36. function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
  37. {
  38. // the highlightStartTag and highlightEndTag parameters are optional
  39. if ((!highlightStartTag) || (!highlightEndTag)) {
  40. highlightStartTag = "<font style='color:black; background-color:yellow;'>";
  41. highlightEndTag = "</font>";
  42. }
  43.  
  44. // find all occurences of the search term in the given text,
  45. // and add some "highlight" tags to them (we're not using a
  46. // regular expression search, because we want to filter out
  47. // matches that occur within HTML tags and script blocks, so
  48. // we have to do a little extra validation)
  49. var newText = "";
  50. var i = -1;
  51. var lcSearchTerm = searchTerm.toLowerCase();
  52. var lcBodyText = bodyText.toLowerCase();
  53.  
  54. while (bodyText.length > 0) {
  55. i = lcBodyText.indexOf(lcSearchTerm, i+1);
  56. if (i < 0) {
  57. newText += bodyText;
  58. bodyText = "";
  59. } else {
  60. // skip anything inside an HTML tag
  61. if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
  62. // skip anything inside a <script> block
  63. if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
  64. if(lcBodyText.lastIndexOf("}", i) >= lcBodyText.lastIndexOf("{", i)){
  65. newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
  66. //console.log('Highlight: '+ bodyText.substr(i, searchTerm.length));
  67. bodyText = bodyText.substr(i + searchTerm.length);
  68. lcBodyText = bodyText.toLowerCase();
  69. i = -1;
  70. resultCount++;
  71. }
  72. }
  73. }
  74. }
  75. }
  76.  
  77. return newText;
  78. }
  79.  
  80. function highlightSearchTerms(searchText, jelement ,treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
  81. {
  82. // if the treatAsPhrase parameter is true, then we should search for
  83. // the entire phrase that was entered; otherwise, we will split the
  84. // search string so that each word is searched for and highlighted
  85. // individually
  86. resultCount = 0;
  87.  
  88. var searchArray;
  89. if (treatAsPhrase) {
  90. searchArray = [searchText];
  91. } else {
  92. searchArray = searchText.split(/\s+/);
  93. }
  94. console.log('Search array: '+searchArray);
  95.  
  96. if (!document.body || typeof(document.body.innerHTML) == "undefined") {
  97. if (warnOnFailure) {
  98. alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
  99. }
  100. return false;
  101. }
  102.  
  103. var bodyText = jelement.html();
  104. for (var i = 0; i < searchArray.length; i++) {
  105. if (searchArray[i].match(/^\s*$/)){continue;}
  106. bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  107. }
  108.  
  109. jelement.html(bodyText);
  110. return true;
  111. }
  112.  
  113. function hasDuplicates(array)
  114. {
  115. return (new Set(array)).size !== array.length;
  116. }
  117.  
  118. function isNullOrWhiteSpace(str){
  119. return str == null || str.replace(/\s/g, '').length < 1;
  120. }
  121.  
  122. function whichEdit( qToEdit )
  123. {
  124. //console.log('Before whichEdit: ' + qToEdit);
  125. var newQ = qToEdit.toLowerCase();
  126. newQ = newQ.replace(/[.,'“”"\/#!??$%\^&\*;:{}=\_`~()]/g,''); //remove puncuation
  127. //console.log('After puncuation: ' + newQ);
  128. newQ = newQ.replace(/(\s+|^)\S{1,3}(?=(\s+|$))/mgi, ''); //remove small words
  129. //console.log('After small words: ' + newQ);
  130. newQ = newQ.replace(/\b(can't|don't|isn't|aren't|which|whose|where|these|what|ever|with|also|same|part|would|wouldn't|when|about|mean)\b/gi, ''); //remove these words
  131. //console.log('After words: ' + newQ);
  132. newQ = newQ.replace(/\s{2,}/g,' '); // remove extra spaces
  133.  
  134. //console.log('After whichEdit: ' + newQ);
  135. return newQ;
  136. }
  137.  
  138. function editOption( op )
  139. {
  140. op = op.toLowerCase()
  141. var str = ['the ','an ', 'a ', 'in the ', 'on the ', 'at the ', 'le ', 'un '];
  142. var i;
  143. for (i = 0; i < str.length; i++)
  144. {
  145. if(op.startsWith(str[i])) op = op.substring(str[i].length);
  146. }
  147. return op;
  148. }
  149.  
  150. function removeCommonWordsInOptions(op)
  151. {
  152. console.log('Before removeCommonWordsInOptions: ' + op);
  153. var words = [], allWords = [], str = '', i,j;
  154. var unchanged = op;
  155.  
  156. for (i = 0; i < op.length; i++)
  157. {
  158. str += ' ' + op[i];
  159. }
  160.  
  161. words = str.split(/\s+/);
  162. words.shift();
  163.  
  164. if(hasDuplicates(op)) {useOG = true; return unchanged;} //if any options are the same then we better use the original options
  165. if(words.length === op.length) return op; //must me unique, no need to search for common words
  166.  
  167.  
  168. var sorted_arr = words.slice().sort();
  169. var duplicates = [];
  170. for (i = 0; i < sorted_arr.length - 1; i++) {
  171. if (sorted_arr[i + 1] == sorted_arr[i]) {
  172. duplicates.push(sorted_arr[i]);
  173. }
  174. }
  175.  
  176. var temp = [];
  177. $.each(duplicates, function(i, el){
  178. if($.inArray(el, temp) === -1) temp.push(el); //filter out duplicates
  179. });
  180. duplicates = temp;
  181.  
  182. console.log('Duplicates: '+ duplicates )
  183.  
  184. for (i = 0; i < op.length; i++)
  185. {
  186. var optionWords = op[i].split(/\s+/);
  187. for(j = 0; j < optionWords.length; j++)
  188. {
  189. if(duplicates.indexOf(optionWords[j]) > -1)
  190. {
  191. var re = new RegExp(optionWords[j], 'g');
  192. op[i] = op[i].replace(re, '');
  193. }
  194. }
  195. op[i] = op[i].replace(/\s{2,}/g,' '); // remove extra spaces
  196. }
  197.  
  198. if(hasDuplicates(op)) {useOG = true; return unchanged;} //if any options are the same then we better use the original options
  199. console.log('After removeCommonWordsInOptions: ' + op);
  200.  
  201. return op;
  202. }
  203.  
  204. //Script Main:
  205. var domain = window.location.hostname;
  206. var opt = [];
  207. var node, textnode, colorNode, numberNode, frame, params;
  208.  
  209. if( domain.startsWith('www.google.') && getParameterByName('removeMargin'))
  210. {
  211. $('#cnt').css('margin-left', '0px');
  212. $('#center_col').css('margin-left', '0px');
  213. }
  214.  
  215. if(domain === "multisearchtabs.github.io")
  216. {
  217. question = getParameterByName('s1');
  218. if(question === null) return;
  219. i = 0;
  220. while(!isNullOrWhiteSpace(getParameterByName('s'+(i+2))))
  221. {
  222. opt[i] = getParameterByName('s'+(i+2));
  223. i++;
  224. }
  225.  
  226. question = question.replace('&', ' ');
  227.  
  228. console.log('q: '+question + ' opt: '+opt)
  229.  
  230. if(localStorage.getItem('lastLink') === window.location.href)
  231. {
  232. console.log('link visited');
  233. localStorage.removeItem('lastLink');
  234. params = '?whichDetected=' + whichEdit(question);
  235. if(opt[0]) params += '&option1=' + opt[0];
  236. if(opt[1]) params += '&option2=' + opt[1];
  237. if(opt[2]) params += '&option3=' + opt[2];
  238.  
  239. params = encodeURI(params);
  240. console.log('about wo whcih: '+'http://google.com/'+params);
  241.  
  242. window.location.href = 'http://google.com/'+params;
  243. return;
  244. }
  245.  
  246. params = question
  247. params += '&regularQ=true';
  248.  
  249. for(i = 0; i < opt.length; i++)
  250. {
  251. if(opt[i])
  252. {
  253. opt[i] = opt[i].replace('&', ' ');
  254. params += '&option'+(i+1)+'=' + opt[i];
  255. }
  256. }
  257.  
  258. params = encodeURI(params);
  259.  
  260. localStorage.setItem('lastLink', window.location.href);
  261. window.location.href = 'http://google.com/search?q='+params;
  262. }
  263. else if( domain.startsWith('www.google.') && getParameterByName('regularQ'))
  264. {
  265. var notPhrase;
  266. var style = [];
  267. style[0] = 'color:black; background-color:yellow';
  268. style[1] = 'color:black; background-color:lightcoral;';
  269. style[2] = 'color:black; background-color:cyan;';
  270. style[3] = 'color:black; background-color:lightgreen;';
  271. style[4] = 'color:black; background-color:gray;';
  272. style[5] = 'color:white; background-color:black;';
  273.  
  274. if(getParameterByName('tokenize'))
  275. {
  276. temp = getParameterByName('tokenize');
  277. if(temp.startsWith(' ')) temp = temp.substring(1);
  278. console.log(temp);
  279. opt = temp.split(/\s+/);
  280. opt.reverse();
  281. }
  282. else
  283. {
  284. i = 0;
  285. while(!isNullOrWhiteSpace(getParameterByName('option'+(i+1))))
  286. {
  287. opt[i] = getParameterByName('option'+(i+1));
  288. i++;
  289. }
  290. }
  291. if(opt.length < 1){return;}
  292. console.log(domain);
  293.  
  294. var editedOpt = [], phrase = true;
  295.  
  296. //console.log('Opt1: ' + opt);
  297.  
  298. for (i = 0; i < opt.length && i< style.length; i++) {
  299. if(opt[i].length > 6) {phrase = false; break;}
  300. }
  301.  
  302. //console.log('Opt2: ' + opt);
  303. if(phrase == false)
  304. {
  305. for (i = 0; i < opt.length && i< style.length; i++) {
  306. editedOpt[i] = editOption(opt[i]);
  307. }
  308. //console.log('Opt3: ' + editedOpt);
  309. for (i = 0; i < editedOpt.length; i++) {
  310. var res = whichEdit(editedOpt[i]);
  311. if(isNullOrWhiteSpace(res)) {continue;}
  312. editedOpt[i] = res;
  313. }
  314. //console.log('Opt4: ' + editedOpt);
  315. editedOpt = removeCommonWordsInOptions(editedOpt);
  316. //console.log('Opt5: ' + editedOpt)
  317. for (i = 0; i < editedOpt.length; i++) {
  318. if(isNullOrWhiteSpace(editedOpt[i]))
  319. {
  320. useOG = true; break;
  321. }
  322. }
  323. }
  324. else {editedOpt = opt;}
  325.  
  326.  
  327. if(useOG==true) {phrase = true; editedOpt=opt;}
  328. //console.log('Opt6: ' + editedOpt)
  329. var optRes = [];
  330. for (i = 0; i < editedOpt.length && i< style.length; i++) {
  331. highlightSearchTerms(editedOpt[i], $('#rcnt'),phrase,false, "<font style='"+style[i]+"';'>", "</font>");
  332. optRes[i] = resultCount;
  333. console.log((i+1)+'. count: '+ resultCount);
  334. }
  335.  
  336. var resultsElement = document.createElement('div');
  337. var container = document.createElement('div');
  338. var buttContainer = document.createElement('div');
  339. container.appendChild(resultsElement);
  340. container.appendChild(buttContainer);
  341. var referenceNode = document.getElementById('res');
  342. referenceNode.parentNode.insertBefore(container, referenceNode);
  343.  
  344. container.style.cssText = 'display:inline-flex; flex-wrap: nowrap; align-items: center; justify-content: space-between; font-size: large; border: 1px solid #0199d9; border-radius: 4px; padding-left: 1em; padding-right: 1em; width: 100%';
  345. buttContainer.style.cssText = 'display:flex; flex-wrap: nowrap; align-items: flex-end; justify-content: center; flex-direction: column;';
  346. resultsElement.style.cssText = 'flex-basis: auto';
  347.  
  348. node = document.createElement("p");
  349. node.style.cssText = 'font-weight: bold;';
  350. textnode = document.createTextNode("MULTISEARCH RESULTS:");
  351. node.appendChild(textnode);
  352. resultsElement.appendChild(node);
  353.  
  354. for (i = 0; i < opt.length && i< style.length; i++) {
  355. node = document.createElement("p");
  356. colorNode = document.createElement("font");
  357. colorNode.style.cssText = style[i];
  358. numberNode = document.createElement("font");
  359. numberNode.style.cssText = 'font-weight: bold;';
  360. textnode = document.createTextNode( (i+1)+'. ' + opt[i]);
  361. colorNode.appendChild(textnode);
  362. textnode = document.createTextNode(' '+optRes[i]);
  363. numberNode.appendChild(textnode);
  364. node.appendChild(colorNode);
  365. node.appendChild(numberNode);
  366. resultsElement.appendChild(node);
  367. }
  368.  
  369. if(getParameterByName('tokenize') == null)
  370. {
  371. var aLink = document.createElement('a');
  372. aLink.setAttribute('target', '_blank');
  373.  
  374. question = getParameterByName('q');
  375. params = '?whichDetected=' + whichEdit(question);
  376. if(opt[0]) params += '&option1=' + opt[0];
  377. if(opt[1]) params += '&option2=' + opt[1];
  378. if(opt[2]) params += '&option3=' + opt[2];
  379.  
  380. params = encodeURI(params);
  381.  
  382. aLink.setAttribute('href', 'http://google.com/'+params+'&optImg=true');
  383. aLink.appendChild(document.createTextNode("OPTION IMAGES"));
  384. aLink.style.cssText = 'display:block; margin-bottom: 0.7em; margin-top: 0.7em; color:white; background-color: #5EB2D1; padding: 10px 10px; border-radius: 4px; text-decoration: none;';
  385. buttContainer.appendChild(aLink);
  386.  
  387. var aInput = document.createElement('input');
  388. aInput.type = 'text';
  389. aInput.id = 'input';
  390. buttContainer.appendChild(aInput);
  391.  
  392. var aButton = document.createElement('button');
  393. aButton.id = 'aButton'
  394. //aLink.setAttribute('href', 'http://google.com/'+params);
  395. aButton.appendChild(document.createTextNode("SEARCH EACH OPTION"));
  396. aButton.style.cssText = 'display:block; margin-bottom: 0.7em; color:white; background-color: #0199d9; padding: 10px 10px; border-radius: 4px; text-decoration: none;';
  397. buttContainer.appendChild(aButton);
  398. document.getElementById("aButton").addEventListener ("click", searchEach, false);
  399.  
  400.  
  401.  
  402.  
  403. }
  404. }
  405. else if( domain.startsWith('www.google.') && getParameterByName('whichDetected') !==null)
  406. {
  407. console.log('whichDetected');
  408. question=getParameterByName('whichDetected');
  409.  
  410. temp = getParameterByName('option1');
  411. if(temp) opt[0] = temp;
  412. temp = getParameterByName('option2');
  413. if(temp) opt[1] = temp;
  414. temp = getParameterByName('option3');
  415. if(temp) opt[2] = temp;
  416.  
  417. var isImgSearch = getParameterByName('optImg');
  418. var link;
  419.  
  420. document.body.innerHTML = "";
  421. document.head.innerHTML = "";
  422. document.body.style.cssText = 'margin: 0px ;padding: 0px; overflow:hidden;';
  423. for (i = 0; i < opt.length; i++)
  424. {
  425. node = document.createElement("div");
  426. node.id = 'searchBox'+(i+1);
  427. node.style.cssText = 'display: inline-block; width: 33%; height: 100%;';
  428. frame = document.createElement("iframe");
  429. frame.style.cssText = 'display: inline-block; position:fixed; width: 100%; height: 100%;';
  430. frame.id = 'gframe'+(i+1);
  431. node.appendChild(frame);
  432. document.body.appendChild(node);
  433. console.log('search?q='+question+' '+opt[i]+'&regularQ=true&removeMargin=true&option1='+opt[i]);
  434. if(isImgSearch)
  435. {
  436. link = 'search?q='+opt[i]+'&tbm=isch';
  437. }
  438. else
  439. {
  440. link = 'search?q='+opt[i]+'&regularQ=true&removeMargin=true&tokenize='+question;
  441. }
  442. $('#gframe'+(i+1)).attr("src", link);
  443. }
  444.  
  445.  
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement