EH Search Tracker

Track your searches on e*hentai

  1. // ==UserScript==
  2. // @name EH Search Tracker
  3. // @namespace http://tampermonkey.net/
  4. // @version .50
  5. // @description Track your searches on e*hentai
  6. // @author 320
  7. // @match https://e-hentai.org/*
  8. // @match https://exhentai.org/*
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. //Please note this code makes use of jQuery
  17.  
  18. //------------------------------ Add the "following" button at the top after the page loads, code copy pasted from stack overflow
  19.  
  20. // Get the 4th arrow at the top, between Favorites and Home/Settings
  21. var topBarArrows = document.getElementById("nb");
  22. var fourthArrow = topBarArrows.getElementsByTagName("img")[3];
  23.  
  24. // Make a new arrow, then insert that between Favorites and Home/Settings (before position 4)
  25. var newImage = document.createElement("img");
  26. newImage.src = "https://ehgt.org/g/mr.gif";
  27. topBarArrows.insertBefore(newImage,fourthArrow);
  28.  
  29. //Make new Text and insert that text between the (new) arrow and the (old) fourth arrow
  30. var newText = document.createElement("a");
  31. newText.innerHTML = " Following";
  32. newText.href = "#";
  33. newText.onclick = function() {showFollowing();};
  34.  
  35. topBarArrows.insertBefore(newText,fourthArrow);
  36.  
  37.  
  38. // loads the searchName array so the for loop knows how many search terms you have
  39. var searchName = JSON.parse(window.localStorage.getItem("searchName"));
  40.  
  41. //Put a subscribe button next to the "Show File Search" link near search box
  42. var subscribeButton = document.createElement("a");
  43. subscribeButton.innerText = "Show Subscribe Dialog";
  44. subscribeButton.style.marginLeft = "7px";
  45. subscribeButton.href = "#";
  46. subscribeButton.id = "myBtn";
  47. document.getElementById("searchbox").getElementsByClassName("nopm")[1].append(subscribeButton);
  48. subscribeButton.onclick = function() {showSubscribe();};
  49.  
  50. //Let's execute this now because this thing doesn't work
  51. setupSubscribe();
  52. //damn it still doesn't work
  53.  
  54. //-----------------The actual following script
  55.  
  56. //Activates when "Following" is clicked
  57. function showFollowing() {
  58.  
  59. //Sets get requests to be synchronous with the code because _javascript_
  60. //This has to be done here because jQuery hasn't finished loading by
  61. // the time the script loads in the "following" button
  62. jQuery.ajaxSetup({async:false});
  63.  
  64. console.log("It's starting");
  65.  
  66. //Hide the main page...
  67. document.getElementsByClassName("ido")[0].style.display = "none";
  68.  
  69. //...then display the "Following" page
  70. var newPage = document.createElement("div");
  71. newPage.id = "myPage";
  72. newPage.className = "page";
  73.  
  74. var windowContent = document.createElement("div");
  75. windowContent.className = "page-content";
  76.  
  77. var pageTop = document.createElement("span");
  78. pageTop.className = "close";
  79. pageTop.innerText = "\nFollowing";
  80. pageTop.style.fontSize = "200%";
  81.  
  82. var pageText = document.createElement("p");
  83. pageText.innerText = "Your followed searches:";
  84. pageText.style.fontSize = "150%";
  85.  
  86. //put everything together at the end of the body, then center it
  87. document.body.insertBefore(newPage, document.getElementsByClassName("dp")[0]);
  88. document.getElementById("myPage").append(windowContent);
  89. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pageTop);
  90. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pageText);
  91.  
  92. document.getElementById("myPage").style.alignContent = "space-between";
  93.  
  94.  
  95. //-------------------------Start the search data collection
  96.  
  97. var i = 0;
  98.  
  99. //If the searchName array is undefined, you haven't saved any searches
  100. //If the searchName array is defined as an array, start going through each search
  101. if (searchName == null){
  102. console.log("Oh, you don't have any saved searches...");
  103. firstRun();
  104. }
  105. else{
  106. console.log("Oh, you've got some saved searches. Let's start the userscript");
  107.  
  108. //start getting data for each searchterm
  109. beforeTheGet(0, searchName);
  110. }
  111.  
  112. //Adds a "Reorder" button to the bottom of the page
  113.  
  114. //lolol next version maybe
  115.  
  116. //save time you last updated this in MM / DD format, then puts it on the bottom
  117. //It doesn't actually work though...
  118. var newTimeUpdated = new Date().getMonth() + " / " + new Date().getDate();
  119. var dateElement = document.createElement("p");
  120. dateElement.innerText = "Last Updated: " + newTimeUpdated;
  121. dateElement.style.fontSize = "110%";
  122. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(dateElement);
  123.  
  124. } //end show function
  125.  
  126.  
  127. //////---------------------beforeTheGet function (gets a search via GET request)
  128. function beforeTheGet(i, searchName) {
  129. setTimeout(function() {
  130. console.log("I've made it into the for loop!\nThe variable i now equals: " + i);
  131.  
  132. //Request current search page via GET request also copy pasted from stack overflow
  133. // and then uses the data to fill in the variables that need it
  134. // Waits five seconds before executing each request (because the setTimeout increases with each for loop)
  135. // (all information needed is achieved with one search)
  136.  
  137. var xhr = "xhr before get";
  138. $.get(
  139. searchName[i],
  140. {},
  141. function(data) {
  142. xhr = data;
  143. console.log("I'm about to enter the afterTheGet function with i = " +i);
  144. afterTheGet(xhr , i);
  145. }
  146. );
  147.  
  148. i++;
  149. if (i < searchName.length){ console.log("I'm about to go into the next loop, with i = "+i); beforeTheGet(i , searchName);}
  150. console.log("at the end of the for loop, the variable i = " + i);
  151.  
  152. }, 5000*i);
  153. }
  154.  
  155. //-------------------------afterTheGet function (goes through data retrieved from search)
  156. function afterTheGet(xhr , i){ setTimeout(function(){
  157.  
  158. console.log("I'm now in the afterTheGet Function, where i = " + i);
  159.  
  160. //redefine all variables in the scope of this function for some reason. These will be read from the localStorage:
  161. var searchName = JSON.parse(window.localStorage.getItem("searchName"));
  162. var lastResultNumber = JSON.parse(window.localStorage.getItem("lastResultNumber"));
  163. var lastResultURL = JSON.parse(window.localStorage.getItem("lastResultURL"));
  164. var humanName = JSON.parse(window.localStorage.getItem("humanName"));
  165.  
  166. var newResultNumber = 12;
  167. var newResultURL1 = "url1";
  168. var newResultURL2 = "url1";
  169. var newResultURL3 = "url1";
  170. var newResultURL4 = "url1";
  171. var newResultURL5 = "url1";
  172. var newTimeUpdated = "time1";
  173. var newGalleryPic1 = "picurl1"; //what the fuck are you doing use an array
  174. var newGalleryPic2 = "picurl1";
  175. var newGalleryPic3 = "picurl1";
  176. var newGalleryPic4 = "picurl1";
  177. var newGalleryPic5 = "picurl1";
  178.  
  179. console.log("The lastresultNumber array is for some reason " +lastResultNumber);
  180.  
  181.  
  182. //Wrap the html data in string form in order to use crap like "getElementsByClass" etc
  183. var wrapper = document.createElement("div");
  184. wrapper.innerHTML = xhr;
  185.  
  186. //Get the number of galleries currently available for your search
  187. var tempgalleryNumber = wrapper.getElementsByClassName("ip")[0].innerHTML.substring(16);
  188. tempgalleryNumber = tempgalleryNumber.replace(",", "");
  189. newResultNumber = parseInt(tempgalleryNumber);
  190. console.log("The lastResultNumber array in the new function: " + lastResultNumber[i]);
  191. console.log("The newResultNumber integer in the new function: " + newResultNumber);
  192.  
  193. //Get the URL of the latest gallery available for that tag
  194. //If the user is using list view, get it from where "it5" in the wrapper
  195. //If the user is using thumbnail view, get it from where "id2" is
  196. if (xhr.indexOf("it5") > -1) {
  197. //We list view nao (works!)
  198. //Get the five latest gallery urls for the list search
  199. newResultURL1 = wrapper.getElementsByClassName("it5")[0].firstChild.href;
  200. newResultURL2 = wrapper.getElementsByClassName("it5")[1].firstChild.href;
  201. newResultURL3 = wrapper.getElementsByClassName("it5")[2].firstChild.href;
  202. newResultURL4 = wrapper.getElementsByClassName("it5")[3].firstChild.href;
  203. newResultURL5 = wrapper.getElementsByClassName("it5")[4].firstChild.href;
  204.  
  205. }
  206. else
  207. {
  208. //we thumbnail view nao (I think it works now!)
  209. newResultURL1 = wrapper.getElementsByClassName("id2")[0].firstChild.href;
  210. newResultURL2 = wrapper.getElementsByClassName("id2")[1].firstChild.href;
  211. newResultURL3 = wrapper.getElementsByClassName("id2")[2].firstChild.href;
  212. newResultURL4 = wrapper.getElementsByClassName("id2")[3].firstChild.href;
  213. newResultURL5 = wrapper.getElementsByClassName("id2")[4].firstChild.href;
  214. }
  215.  
  216. console.log("The first newURL in the new function: " + newResultURL1);
  217. console.log("The 2 newURL in the new function: " + newResultURL2);
  218. console.log("The 3 newURL in the new function: " + newResultURL3);
  219. console.log("The 4 newURL in the new function: " + newResultURL4);
  220. console.log("The first newURL in the new function: " + newResultURL5);
  221.  
  222. //Get the URL of the thumbnail for latest gallery available for that tag
  223. if ((window.location.href).indexOf("e-hen") > -1){
  224. if (xhr.indexOf("it5") > -1) { //ehen list view
  225. newGalleryPic1 = wrapper.getElementsByClassName("it2")[0].firstChild.src;
  226. newGalleryPic2 = wrapper.getElementsByClassName("it2")[1].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[1].innerHTML.indexOf(".jpg")+4)
  227. newGalleryPic2 = "https://" + newGalleryPic2.replace("~", "/");
  228.  
  229. newGalleryPic3 = wrapper.getElementsByClassName("it2")[2].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[2].innerHTML.indexOf(".jpg")+4)
  230. newGalleryPic3 = "https://" + newGalleryPic3.replace("~", "/");
  231.  
  232. newGalleryPic4 = wrapper.getElementsByClassName("it2")[3].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[3].innerHTML.indexOf(".jpg")+4)
  233. newGalleryPic4 = "https://" + newGalleryPic4.replace("~", "/");
  234.  
  235. newGalleryPic5 = wrapper.getElementsByClassName("it2")[4].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[4].innerHTML.indexOf(".jpg")+4)
  236. newGalleryPic5 = "https://" + newGalleryPic5.replace("~", "/");
  237. }
  238. else //ehen thumb view
  239. {
  240. newGalleryPic1 = wrapper.getElementsByClassName("id3")[0].firstChild.firstChild.src;
  241. newGalleryPic2 = wrapper.getElementsByClassName("id3")[1].firstChild.firstChild.src;
  242. newGalleryPic3 = wrapper.getElementsByClassName("id3")[2].firstChild.firstChild.src;
  243. newGalleryPic4 = wrapper.getElementsByClassName("id3")[3].firstChild.firstChild.src;
  244. newGalleryPic5 = wrapper.getElementsByClassName("id3")[4].firstChild.firstChild.src;
  245. }
  246. }
  247. else {
  248.  
  249. if (xhr.indexOf("it5") > -1) { //panda list view
  250. console.log("panda list view")
  251. newGalleryPic1 = wrapper.getElementsByClassName("it2")[0].firstChild.src;
  252. newGalleryPic2 = wrapper.getElementsByClassName("it2")[1].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[1].innerHTML.indexOf(".jpg")+4)
  253. newGalleryPic2 = "https://" + newGalleryPic2.replace("~", "/");
  254.  
  255. newGalleryPic3 = wrapper.getElementsByClassName("it2")[2].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[2].innerHTML.indexOf(".jpg")+4)
  256. newGalleryPic3 = "https://" + newGalleryPic3.replace("~", "/");
  257.  
  258. newGalleryPic4 = wrapper.getElementsByClassName("it2")[3].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[3].innerHTML.indexOf(".jpg")+4)
  259. newGalleryPic4 = "https://" + newGalleryPic4.replace("~", "/");
  260.  
  261. newGalleryPic5 = wrapper.getElementsByClassName("it2")[4].innerHTML.substring(6,wrapper.getElementsByClassName("it2")[4].innerHTML.indexOf(".jpg")+4)
  262. newGalleryPic5 = "https://" + newGalleryPic5.replace("~", "/");
  263. }
  264. else //panda thumb view
  265. {
  266. console.log("panda thumb view")
  267. newGalleryPic1 = wrapper.getElementsByClassName("id3")[0].firstChild.firstChild.src;
  268. newGalleryPic2 = wrapper.getElementsByClassName("id3")[1].firstChild.firstChild.src;
  269. newGalleryPic3 = wrapper.getElementsByClassName("id3")[2].firstChild.firstChild.src;
  270. newGalleryPic4 = wrapper.getElementsByClassName("id3")[3].firstChild.firstChild.src;
  271. newGalleryPic5 = wrapper.getElementsByClassName("id3")[4].firstChild.firstChild.src;
  272. }
  273. }
  274.  
  275. // newGalleryPic = tempgalleryPicURL;
  276. console.log(newGalleryPic1);
  277. console.log(newGalleryPic2);
  278. console.log(newGalleryPic3);
  279. console.log(newGalleryPic4);
  280. console.log(newGalleryPic5);
  281.  
  282.  
  283. // Check whether the new number of galleries is the same as the old
  284.  
  285. // Also checks whether the newResultURL is new or not. If it is,
  286. // a gallery was updated, and that needs to be counted as a new result.
  287. if (newResultNumber == lastResultNumber[i] && newResultURL1 != lastResultURL[i])
  288. {
  289. newResultNumber = lastResultNumber[i] + 1;
  290. }
  291.  
  292.  
  293. //Display results on the page
  294. var searchElement = document.createElement("a");
  295. searchElement.innerText = ("\n\n\n\n"+humanName[i] + " || New Results: " + (newResultNumber - lastResultNumber[i]) + "\n\n");
  296. searchElement.style.fontSize = "160%";
  297. searchElement.style.fontWeight = "800";
  298.  
  299. //Get proper URL for the search
  300. if ((window.location.href).indexOf("e-hen") > -1){
  301. searchElement.href = "https://e-hentai.org" + searchName[i];
  302. }
  303. else {
  304. searchElement.href = "https://exhentai.org" + searchName[i];
  305. }
  306.  
  307. //Create five pictures for the five latest galleries
  308. //fuuuuuuuck I should have made these arrays...
  309. var searchPicture1 = document.createElement("img");
  310. searchPicture1.src = newGalleryPic1;
  311. var pictureContainer1 = document.createElement("a");
  312. pictureContainer1.href = newResultURL1;
  313. pictureContainer1.appendChild(searchPicture1);
  314.  
  315. var searchPicture2 = document.createElement("img");
  316. searchPicture2.src = newGalleryPic2;
  317. var pictureContainer2 = document.createElement("a");
  318. pictureContainer2.href = newResultURL2;
  319. pictureContainer2.appendChild(searchPicture2);
  320.  
  321. var searchPicture3 = document.createElement("img");
  322. searchPicture3.src = newGalleryPic3;
  323. var pictureContainer3 = document.createElement("a");
  324. pictureContainer3.href = newResultURL3;
  325. pictureContainer3.appendChild(searchPicture3);
  326.  
  327. var searchPicture4 = document.createElement("img");
  328. searchPicture4.src = newGalleryPic4;
  329. var pictureContainer4 = document.createElement("a");
  330. pictureContainer4.href = newResultURL4;
  331. pictureContainer4.appendChild(searchPicture4);
  332.  
  333. var searchPicture5 = document.createElement("img");
  334. searchPicture5.src = newGalleryPic5;
  335. var pictureContainer5 = document.createElement("a");
  336. pictureContainer5.href = newResultURL5;
  337. pictureContainer5.appendChild(searchPicture5);
  338.  
  339. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(searchElement);
  340. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer1);
  341. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer2);
  342. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer3);
  343. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer4);
  344. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer5);
  345.  
  346.  
  347. console.log("The searchName array at " +i +" is now length "+searchName.length);
  348. console.log("The humanname array " +i +" is now "+humanName);
  349. console.log("The lastResultNumb array " +i +" is now "+lastResultNumber);
  350. console.log("The newResultNumb integer is " +i +" is now "+newResultNumber);
  351.  
  352. console.log("The lastURL array " +i +" is now "+lastResultURL);
  353.  
  354. //Update the arrays with new stuff...
  355. lastResultNumber[i] = newResultNumber;
  356. lastResultURL[i] = newResultURL1;
  357.  
  358. //Remove the arrays from local storage
  359. window.localStorage.removeItem("lastResultNumber");
  360. window.localStorage.removeItem("lastResultURL");
  361.  
  362. //Then save the arrays with the new elements back to localstorage
  363. window.localStorage.setItem("lastResultNumber", JSON.stringify(lastResultNumber));
  364. window.localStorage.setItem("lastResultURL", JSON.stringify(lastResultURL));
  365.  
  366. console.log("-----------The afterTheGet Function has ended-------------");
  367. console.log("Okay, let's just wait 6 seconds before moving onto the next search...");
  368.  
  369.  
  370. }, 1);} // end afterTheGet function
  371.  
  372. //-------------------------show subsscribe dialog function
  373. function setupSubscribe(){
  374.  
  375.  
  376. //Set up the Modal using javascript (Copy pasted from W3schools)
  377. var myModal = document.createElement("div");
  378. myModal.className= "modal";
  379. myModal.id = "myModal";
  380.  
  381. var modalContent = document.createElement("div");
  382. modalContent.className = "modal-content";
  383. myModal.append(modalContent);
  384.  
  385. var modalHeader = document.createElement("div");
  386. modalHeader.className = "modal-header";
  387. modalContent.append(modalHeader);
  388.  
  389. var closeThing = document.createElement("span");
  390. closeThing.className = "close";
  391. closeThing.innerText = "Close";
  392. modalHeader.append(closeThing);
  393.  
  394. var headerText = document.createElement("h2");
  395. headerText.innerText = "Subscribe to Search";
  396. modalHeader.append(headerText);
  397.  
  398. var modalBody = document.createElement("div");
  399. modalBody.className = "modal-body";
  400. var bodyText = document.createElement("p");
  401. bodyText.innerText = "Did you want to save this search?\nEnter what you want to name it below:\n";
  402. bodyText.id = "modalBodyText";
  403. var bodyTextBox = document.createElement("input");
  404. bodyTextBox.type = "text";
  405. bodyTextBox.id = "textBox";
  406. var bodyConfirmButton = document.createElement("button");
  407. bodyConfirmButton.innerText = "Save!";
  408. bodyConfirmButton.onclick = function(){subscribeToSearch();};
  409. var bodyDeleteButton = document.createElement("button");
  410. bodyDeleteButton.innerText = "Delete!";
  411. bodyDeleteButton.onclick = function(){deleteSearch();};
  412. var makeshiftspacer = document.createElement("a");
  413. makeshiftspacer.innerText = " or ";
  414. modalBody.append(bodyText);
  415. modalBody.append(bodyTextBox);
  416. modalBody.append(bodyConfirmButton);
  417. modalBody.appendChild(makeshiftspacer);
  418. modalBody.append(bodyDeleteButton);
  419. modalContent.append(modalBody);
  420.  
  421. var modalFooter = document.createElement("div");
  422. modalFooter.className = "modal-footer";
  423. var footerText = document.createElement("h3");
  424. footerText.innerText = "The footer of the modal";
  425. modalContent.append(modalFooter);
  426.  
  427. //Attach everything to the page
  428. document.getElementsByClassName("itg")[0].append(myModal);
  429.  
  430. //-------------------------------static CSS
  431. myModal.style.cssText = "display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4);";
  432.  
  433. modalBody.style.cssText = "padding: 2px 16px;";
  434.  
  435. closeThing.style.cssText = " color: white; float: right; font-size: 28px; font-weight: bold;";
  436.  
  437. //-------------------------------dynamic CSS (supposed to change on panda)
  438. if ((window.location.href).indexOf("e-hen") > -1){
  439. modalContent.style.cssText = " position: relative; background-color: #FFFFFF; margin: auto; padding: 0; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);";
  440.  
  441. modalHeader.style.cssText =" padding: 2px 16px; background-color: #732626; color: white;";
  442.  
  443. modalFooter.style.cssText = " padding: 2px 16px; background-color: #732626; color: white;";
  444. }
  445. else
  446. {
  447. modalContent.style.cssText = " position: relative; background-color: #4f535b; margin: auto; padding: 0; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);";
  448.  
  449. modalHeader.style.cssText =" padding: 2px 16px; background-color: #34353b; color: white;";
  450.  
  451. modalFooter.style.cssText = " padding: 2px 16px; background-color: #34353b; color: white;";
  452. }
  453.  
  454.  
  455. /*
  456. Oh wow, these don't even work
  457. var webkitkeyframeElement = document.createElement("style");
  458. webkitkeyframeElement.innerText = " @-webkit-keyframes animatetop { from {top:-300px; opacity:0} to {top:0; opacity:1}}";
  459.  
  460. var keyframeElement = document.createElement("style");
  461. keyframeElement.innerText = " @keyframes animatetop { from {top:-300px; opacity:0} to {top:0; opacity:1}}";
  462. */
  463.  
  464.  
  465. /*
  466. // When the user clicks anywhere outside of the modal, close it
  467. Oh, this doesn't work either
  468. window.onclick = function(event) {
  469. if (event.target == modal) {
  470. modal.style.display = "none";
  471. }
  472. */
  473. }
  474.  
  475. //--------------------- javascript
  476. function showSubscribe(){
  477. // Get the modal
  478. var modal = document.getElementById("myModal");
  479.  
  480. console.log("modal is "+modal);
  481. // Get the button that opens the modal
  482. var bttn = document.getElementById("myBtn");
  483.  
  484. console.log("bttn is "+bttn);
  485. // Get the <span> element that closes the modal
  486. var span = document.getElementsByClassName("close")[0];
  487.  
  488. console.log("span is "+span);
  489. // When the user clicks the button, open the modal
  490. bttn.onclick = function() {
  491. modal.style.display = "block";
  492. console.log("I've set the display");
  493. };
  494.  
  495. // When the user clicks on <span> (close), close the modal
  496. span.onclick = function() {
  497. modal.style.display = "none";
  498. console.log("I've changed the span thing");
  499. };
  500. }
  501.  
  502.  
  503. //--------------------------Function that subscribes you to a search
  504. function subscribeToSearch(){
  505.  
  506. //load in the arrays from localstorage...
  507. var searchName = JSON.parse(window.localStorage.getItem("searchName"));
  508. var humanName = JSON.parse(window.localStorage.getItem("humanName"));
  509. var lastResultNumber = JSON.parse(window.localStorage.getItem("lastResultNumber"));
  510. var lastResultURL = JSON.parse(window.localStorage.getItem("lastResultURL"));
  511.  
  512. var youarehere = (window.location.href);
  513.  
  514. //used to check for duplicate searches:
  515. var isANewArray = "false";
  516. var tempsearchName;
  517. if(searchName!==null){
  518. tempsearchName = searchName.slice();
  519. }
  520. else {
  521.  
  522. //Prepare empty arrays
  523. searchName=[];
  524. tempsearchName = [];
  525. humanName =[];
  526. lastResultNumber = [];
  527. lastResultURL = [];
  528. }
  529.  
  530. //Checks if these arrays are null (i.e. this is the first time you're subscribing to a search)
  531. if (searchName == null) {
  532.  
  533. console.log("This is the first time you're subscribing to a search. Creating localstorage variables for later...");
  534.  
  535. //Place the new elements into a new array
  536. youarehere = youarehere.substring(20, youarehere.length-1);
  537. tempsearchName = [youarehere];
  538. humanName = [document.getElementById("textBox").value];
  539. lastResultNumber = [0];
  540. lastResultURL = ["urldoesn'tgohere"];
  541.  
  542. isANewArray = "true";
  543.  
  544. }
  545. else{
  546.  
  547. //If this isn't the first time,
  548. //Append the new elements after the old elements of the array
  549. youarehere = youarehere.substring(20, youarehere.length-1);
  550. tempsearchName.push(youarehere);
  551.  
  552. humanName.push(document.getElementById("textBox").value);
  553. lastResultNumber.push(0);
  554. lastResultURL.push("urlgoeshere");
  555. }
  556.  
  557.  
  558. var searchSucessText = document.createElement("p");
  559.  
  560. //If this isn't a new array, check if you already have this search
  561. //If it's a duplicate, reject the request
  562. if (searchName.indexOf(youarehere) > -1 && isANewArray == "false") {
  563.  
  564. //Alert the user the search was not saved
  565. document.getElementById("modalBodyText").innerText = "Save failed. You're already subscribed to this search.\nYou can remove it by clicking the \"Delete!\" button";
  566.  
  567. } else {
  568.  
  569. console.log("Saving your search...");
  570.  
  571. //Delete the arrays already in localstorage...
  572. window.localStorage.removeItem("searchName");
  573. window.localStorage.removeItem("humanName");
  574. window.localStorage.removeItem("lastResultNumber");
  575. window.localStorage.removeItem("lastResultURL");
  576.  
  577. //Then save the arrays with the new elements back to localstorage
  578. window.localStorage.setItem("searchName", JSON.stringify(tempsearchName));
  579. window.localStorage.setItem("humanName", JSON.stringify(humanName));
  580. window.localStorage.setItem("lastResultNumber", JSON.stringify(lastResultNumber));
  581. window.localStorage.setItem("lastResultURL", JSON.stringify(lastResultURL));
  582.  
  583. //Alert the user that the search was saved
  584. document.getElementById("modalBodyText").innerText = "You've successfully subscribed to the search: " + document.getElementById("textBox").value;
  585.  
  586. }
  587.  
  588. } //end subscribe to search
  589.  
  590. //=====--------------------------Function that deletes a search
  591.  
  592. function deleteSearch(){
  593.  
  594. //load in the arrays from localstorage...
  595. var searchName = JSON.parse(window.localStorage.getItem("searchName"));
  596. var humanName = JSON.parse(window.localStorage.getItem("humanName"));
  597. var lastResultNumber = JSON.parse(window.localStorage.getItem("lastResultNumber"));
  598. var lastResultURL = JSON.parse(window.localStorage.getItem("lastResultURL"));
  599.  
  600. var youarehere = (window.location.href).substring(20, window.location.href.length - 1);
  601.  
  602. //used to check for duplicate searches:
  603. var isANewArray = "false";
  604. var tempsearchName;
  605. if(searchName!==null){
  606. tempsearchName = searchName.slice();
  607. }
  608. else {
  609.  
  610. //Prepare empty arrays
  611. searchName=[];
  612. tempsearchName = [];
  613. humanName =[];
  614. lastResultNumber = [];
  615. lastResultURL = [];
  616. }
  617.  
  618. //Checks if these arrays are null
  619. if (searchName == null) {
  620.  
  621. console.log("You don't have any searches to delete");
  622. isANewArray = "true";
  623.  
  624. }
  625. var searchSucessText = document.createElement("p");
  626.  
  627. //If this isn't a new array, check if you already have this search
  628. //If it's a duplicate, reject the request
  629. if (searchName.indexOf(youarehere) > -1 && isANewArray == "false") {
  630. //This search exists in the array.
  631. //Delete it using the indexOf to find it
  632. console.log("Deleting search...");
  633. var deletdis = searchName.indexOf(youarehere);
  634.  
  635. tempsearchName.splice(deletdis, 1);
  636. humanName.splice(deletdis, 1);
  637. lastResultNumber.splice(deletdis, 1);
  638. lastResultURL.splice(deletdis, 1);
  639.  
  640. //Delete the arrays already in localstorage...
  641. window.localStorage.removeItem("searchName");
  642. window.localStorage.removeItem("humanName");
  643. window.localStorage.removeItem("lastResultNumber");
  644. window.localStorage.removeItem("lastResultURL");
  645.  
  646. //Then save the arrays with the new elements back to localstorage
  647. window.localStorage.setItem("searchName", JSON.stringify(tempsearchName));
  648. window.localStorage.setItem("humanName", JSON.stringify(humanName));
  649. window.localStorage.setItem("lastResultNumber", JSON.stringify(lastResultNumber));
  650. window.localStorage.setItem("lastResultURL", JSON.stringify(lastResultURL));
  651.  
  652. document.getElementById("modalBodyText").innerText = "You've deleted this search.";
  653.  
  654.  
  655. } else {
  656.  
  657. //Alert the user the search wasn't deleted
  658. document.getElementById("modalBodyText").innerText = "You need to be subscribed to a search in order to delete it.";
  659.  
  660. }
  661. }
  662.  
  663. //-------------------------misc functions
  664. //shows a page informing you that you don't have any saved searches.
  665. function firstRun(){
  666. var infoElement = document.createElement("a");
  667. infoElement.innerText = ("\n\n\n\nYou don't have any saved searches, so get out there and start searching.\nOnce you find a search you want to follow, hit the 'Show Subscribe dialog' link under the search bar\n\n");
  668. infoElement.style.fontSize = "140%";
  669. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(infoElement);
  670.  
  671. var returnElement = document.createElement("a");
  672. returnElement.innerText = "(Click here to return to the front page)\n\n";
  673. returnElement.style.fontSize = "130%";
  674. returnElement.href = "";
  675. document.getElementById("myPage").getElementsByClassName("page-content")[0].append(returnElement);
  676. }
  677.  
  678. })();