EH Tag Tracker

Track your searches on e-h

Verzia zo dňa 16.05.2017. Pozri najnovšiu verziu.

// ==UserScript==
// @name         EH Tag Tracker
// @namespace    http://tampermonkey.net/
// @version      .320
// @description  Track your searches on e-h
// @author       320
// @match        https://e-hentai.org/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    //Enable the jquery library you need to perform a GET request (Nevermind, it's done in the @require at the top)
//    var script = document.createElement('script');
//    scrip


//------------------------------ Add the "following" button at the top after the page loads, also copy pasted from stack overflow

     // Get the 4th arrow at the top, between Favorites and Home/Settings
    var topBarArrows = document.getElementById("nb");
    var fourthArrow = topBarArrows.getElementsByTagName("img")[3];

    // Make a new arrow, then insert that between Favorites and Home/Settings (before position 4)
    var newImage = document.createElement("img");
    newImage.src = "https://ehgt.org/g/mr.gif";
    topBarArrows.insertBefore(newImage,fourthArrow);

    //Make new Text and insert that text between the (new) arrow and the (old) fourth arrow
    var newText = document.createElement("a");
    newText.innerHTML = " Following";
    newText.href = "#";
    newText.onclick = function() {showFollowing();};

    topBarArrows.insertBefore(newText,fourthArrow);


    // loads the searchName array so the for loop knows how many search terms you have
    var searchName = JSON.parse(window.localStorage.getItem("searchName"));

    //Put a subscribe button next to the "Show File Search" link near search box
    var subscribeButton = document.createElement("a");
    subscribeButton.innerText = "Show Subscribe Dialog";
    subscribeButton.style.marginLeft = "7px";
    subscribeButton.href = "#";
    subscribeButton.id = "myBtn";
    document.getElementById("searchbox").getElementsByClassName("nopm")[1].append(subscribeButton);
    subscribeButton.onclick = function() {showSubscribe();};

    //Let's execute this now because this thing doesn't work
    setupSubscribe();
    //damn it still doesn't work

//-----------------The actual following script

    //Activates when "Following" is clicked
    function showFollowing() {

    //Sets get requests to be synchronous with the code because _javascript_
    //This has to be done here because jQuery hasn't finished loading by
    //              the time the script loads in the "following" button
    jQuery.ajaxSetup({async:false});

        console.log("It's starting");

    //Hide the main page...
    document.getElementsByClassName("ido")[0].style.display = "none";

    //...then display the "Following" page
    var newPage = document.createElement("div");
    newPage.id = "myPage";
    newPage.className = "page";

    var windowContent = document.createElement("div");
    windowContent.className = "page-content";

    var pageTop = document.createElement("span");
    pageTop.className = "close";
    pageTop.innerText = "\nFollowing";
    pageTop.style.fontSize = "200%";

    var pageText = document.createElement("p");
    pageText.innerText = "Your followed searches:";
    pageText.style.fontSize = "150%";

    //put everything together at the end of the body, then center it
    document.body.insertBefore(newPage, document.getElementsByClassName("dp")[0]);
    document.getElementById("myPage").append(windowContent);
    document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pageTop);
    document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pageText);

    document.getElementById("myPage").style.alignContent = "space-between";


//-------------------------Start the search data collection

        var i = 0;

    //If the searchName array is undefined, you haven't saved any searches
    //If the searchName array is defined as an array, start going through each search
    if (searchName == null){
           console.log("Oh, you don't have any saved searches...");
           firstRun();
    }
    else{
       console.log("Oh, you've got some saved searches. Let's start the userscript");
         //start getting data for each searchterm
/*   for (i = 0; i < searchName.length; i++)
   {
        console.log("I've made it into the for loop!\nThe variable i now equals: " + i);

       //Request current search page via GET request copy pasted from stack overflow
       // and then uses the data to fill in the variables that need it
       // Waits five seconds before executing each request (because the setTimeout increases with each for loop)
       // (all information needed is achieved with one search)

           var xhr = "xhr before get";
           $.get(
               searchName[i],
               {},
               function(data) {
                   xhr = data;
                   afterTheGet(xhr , i);
               }
           );

       console.log("at the end of the for loop, the variable i = " + i);

   } //end for loop
*/
        beforeTheGet(0, searchName);
}


  //save time you last updated this in MM / DD format, then puts it on the bottom
  //It doesn't actually work though...
       newTimeUpdated = new Date().getMonth() + " / " + new Date().getDate();
       var dateElement = document.createElement("p");
       dateElement.innerText = "Last Updated: " + newTimeUpdated;
       dateElement.style.fontSize = "110%";
       document.getElementById("myPage").getElementsByClassName("page-content")[0].append(dateElement);

} //end show function

//////---------------------beforeTheGet function (gets a search via GET request)
function beforeTheGet(i, searchName) {
  setTimeout(function() {
        console.log("I've made it into the for loop!\nThe variable i now equals: " + i);

        //Request current search page via GET request copy pasted from stack overflow
        // and then uses the data to fill in the variables that need it
        // Waits five seconds before executing each request (because the setTimeout increases with each for loop)
        // (all information needed is achieved with one search)

        var xhr = "xhr before get";
        $.get(
            searchName[i],
            {},
            function(data) {
                xhr = data;
                console.log("I'm about to enter the afterTheGet function with i = " +i);
                afterTheGet(xhr , i);
            }
        );

      i++;
      if (i < searchName.length){ console.log("I'm about to go into the next loop, with i = "+i); beforeTheGet(i , searchName);}
        console.log("at the end of the for loop, the variable i = " + i);

  }, 5000*i);
}

//-------------------------afterTheGet function (goes through data retrieved from search)
    function afterTheGet(xhr , i){ setTimeout(function(){

        console.log("I'm now in the afterTheGet Function, where i = " + i);

        //redefine all variables in the scope of this function for some reason. These will be read from the localStorage:
          var searchName = JSON.parse(window.localStorage.getItem("searchName"));
          var lastResultNumber = JSON.parse(window.localStorage.getItem("lastResultNumber"));
          var lastResultURL = JSON.parse(window.localStorage.getItem("lastResultURL"));
          var humanName = JSON.parse(window.localStorage.getItem("humanName"));

        var newResultNumber = 12;
        var newResultURL = "url1";
        var newTimeUpdated = "time1";
        var newGalleryPic = "picurl1";

        console.log("The lastresultNumber array is for some reason " +lastResultNumber);

    //Get the number of galleries currently available for your search
       var tempgalleryNumber = (xhr.substring(xhr.lastIndexOf("Showing 1-")+16 , xhr.lastIndexOf("</p><table class")));
       tempgalleryNumber = tempgalleryNumber.replace(",", "");
       newResultNumber = parseInt(tempgalleryNumber);
       console.log("The lastResultNumber array in the new function: " + lastResultNumber[i]);
       console.log("The newResultNumber integer in the new function: " + newResultNumber);


    //Get the URL of the latest gallery available for that tag
       newResultURL = xhr.substring(xhr.indexOf("it5\"><a href=\"")+14 , xhr.indexOf("\" onmouseover=\"show_image_pan" , (xhr.indexOf("it5\"><a href=\"")+15)));
    //   newResultURL = tempgalleryURL;
  //     console.log(newResultURL[i]);
  //     console.log("The tempURL in the new function: " + tempgalleryURL[i]);
       console.log("The newURL in the new function: " + newResultURL);


    //Get the URL of the thumbnail for latest gallery available for that tag
       newGalleryPic = xhr.substring(xhr.indexOf("px\"><img src=\"https://ehgt.org/") + 14, xhr.indexOf(".jpg")+4);
   //    newGalleryPic = tempgalleryPicURL;
       console.log(newGalleryPic);


    // Check whether the new number of galleris is the same as the old

    // Also checks whether the newResultURL is new or not. If it is,
    // a gallery was updated, and that needs to be counted as a new result.
       if (newResultNumber == lastResultNumber[i] && newResultURL != lastResultURL[i])
       {
           newResultNumber = lastResultNumber[i] + 1;
       }


    //Display results on the page
       var searchElement = document.createElement("a");
       searchElement.innerText = ("\n\n\n\n"+humanName[i] + "  ||  New Results: " + (newResultNumber - lastResultNumber[i]) + "\n\n");
       searchElement.style.fontSize = "140%";
       searchElement.href = "https://e-hentai.org/" + searchName[i];

       var searchPicture = document.createElement("img");
       searchPicture.src = newGalleryPic;
       var pictureContainer = document.createElement("a");
       pictureContainer.href = newResultURL;
       pictureContainer.appendChild(searchPicture);

       document.getElementById("myPage").getElementsByClassName("page-content")[0].append(searchElement);
       document.getElementById("myPage").getElementsByClassName("page-content")[0].append(pictureContainer);


        console.log("The searchName array  at " +i +" is now length "+searchName.length);
        console.log("The humanname array  " +i +" is now "+humanName);
        console.log("The lastResultNumb array  " +i +" is now "+lastResultNumber);
        console.log("The newResultNumb integer is  " +i +" is now "+newResultNumber);

        console.log("The lastURL array  " +i +" is now "+lastResultURL);

        //Update the arrays with new stuff...
        lastResultNumber[i] = newResultNumber;
        lastResultURL[i] = newResultURL;

        //Remove the arrays from local storage
        window.localStorage.removeItem("lastResultNumber");
        window.localStorage.removeItem("lastResultURL");

        //Then save the arrays with the new elements back to localstorage
        window.localStorage.setItem("lastResultNumber", JSON.stringify(lastResultNumber));
        window.localStorage.setItem("lastResultURL", JSON.stringify(lastResultURL));

      console.log("Okay, let's just wait 6 seconds before moving onto the next search...");
      console.log("-----------The afterTheGet Function has ended-------------");

   }, 1);} // end afterTheGet function

//-------------------------show subsscribe dialog function
    function setupSubscribe(){


//Set up the Modal using javascript
var myModal = document.createElement("div");
  myModal.className= "modal";
  myModal.id = "myModal";

var modalContent = document.createElement("div");
  modalContent.className = "modal-content";
  myModal.append(modalContent);

var modalHeader = document.createElement("div");
  modalHeader.className = "modal-header";
  modalContent.append(modalHeader);

var closeThing = document.createElement("span");
  closeThing.className = "close";
  closeThing.innerText = "Close";
  modalHeader.append(closeThing);

var headerText = document.createElement("h2");
  headerText.innerText = "Subscribe to Search";
  modalHeader.append(headerText);

var modalBody = document.createElement("div");
  modalBody.className = "modal-body";
  var bodyText = document.createElement("p");
  bodyText.innerText = "Did you want to save this search?\nEnter what you want to name it below:\n";
  bodyText.id = "modalBodyText";
  var bodyTextBox = document.createElement("input");
  bodyTextBox.type = "text";
  bodyTextBox.id = "textBox";
  var bodyConfirmButton = document.createElement("button");
  bodyConfirmButton.innerText = "Save!";
  bodyConfirmButton.onclick = function(){subscribeToSearch();};
  modalBody.append(bodyText);
  modalBody.append(bodyTextBox);
  modalBody.append(bodyConfirmButton);
  modalContent.append(modalBody);

var modalFooter = document.createElement("div");
  modalFooter.className = "modal-footer";
  var footerText = document.createElement("h3");
  footerText.innerText = "The footer of the modal";
  modalContent.append(modalFooter);

//Attach everything to the page
  document.getElementsByClassName("itg")[0].append(myModal);

//-------------------------------CSS
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);";

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);";

closeThing.style.cssText = "    color: white;    float: right;    font-size: 28px;    font-weight: bold;";

modalHeader.style.cssText ="    padding: 2px 16px;    background-color: #806769;    color: white;";

modalBody.style.cssText = "padding: 2px 16px;";

modalFooter.style.cssText = " padding: 2px 16px;    background-color: #806769;    color: white;";

/*
Oh, these don't actually work either
var webkitkeyframeElement = document.createElement("style");
webkitkeyframeElement.innerText = " @-webkit-keyframes animatetop {    from {top:-300px; opacity:0}     to {top:0; opacity:1}}";

var keyframeElement = document.createElement("style");
keyframeElement.innerText = " @keyframes animatetop {    from {top:-300px; opacity:0}    to {top:0; opacity:1}}";
*/
//--------------------- javascript

/*
// When the user clicks anywhere outside of the modal, close it
Oh wow, this doesn't even work
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
*/
}

    function showSubscribe(){
        // Get the modal
        var modal = document.getElementById("myModal");

        console.log("modal is "+modal);
        // Get the button that opens the modal
        var bttn = document.getElementById("myBtn");

        console.log("bttn is "+bttn);
        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];

        console.log("span is "+span);
        // When the user clicks the button, open the modal
        bttn.onclick = function() {
            modal.style.display = "block";
            console.log("I've set the display");
        };

        // When the user clicks on <span> (close), close the modal
        span.onclick = function() {
            modal.style.display = "none";
            console.log("I've changed the span thing");
        };
    }


//--------------------------Function that subscribes you to a search
    function subscribeToSearch(){

        //load in the arrays from localstorage...
        var searchName = JSON.parse(window.localStorage.getItem("searchName"));
        var humanName = JSON.parse(window.localStorage.getItem("humanName"));
        var lastResultNumber = JSON.parse(window.localStorage.getItem("lastResultNumber"));
        var lastResultURL = JSON.parse(window.localStorage.getItem("lastResultURL"));

        var youarehere = (window.location.href);

        //used to check for duplicate searches:
        var isANewArray = "false";
        var tempsearchName;
        if(searchName!==null){
            tempsearchName = searchName.slice();
        }
        else {

            //Prepare empty arrays
            searchName=[];
            tempsearchName = [];
            humanName =[];
            lastResultNumber = [];
            lastResultURL = [];
        }

        //Checks if these arrays are null (i.e. this is the first time you're subscribing to a search)
        if (searchName == null) {

            console.log("This is the first time you're subscribing to a search. Creating localstorage variables for later...");

            //Place the new elements into a new array
            youarehere = youarehere.substring(20, youarehere.length-1);
            tempsearchName = [youarehere];
            humanName = [document.getElementById("textBox").value];
            lastResultNumber = [0];
            lastResultURL = ["urldoesn'tgohere"];

            isANewArray = "true";

        }
        else{

            //If this isn't the first time,
            //Append the new elements after the old elements of the array
            youarehere = youarehere.substring(20, youarehere.length-1);
            tempsearchName.push(youarehere);

            humanName.push(document.getElementById("textBox").value);
            lastResultNumber.push(0);
            lastResultURL.push("urlgoeshere");
        }


        var searchSucessText = document.createElement("p");

        //If this isn't a new array, check if you already have this search
        //If it's a duplicate, reject the request
        if (searchName.indexOf(youarehere) > -1 && isANewArray == "false") {

            //Alert the user the search was not saved
            document.getElementById("modalBodyText").innerText = "Save failed. You're already subscribed to this search.";

        } else {

            console.log("Saving your search...");

            //Delete the arrays already in localstorage...
            window.localStorage.removeItem("searchName");
            window.localStorage.removeItem("humanName");
            window.localStorage.removeItem("lastResultNumber");
            window.localStorage.removeItem("lastResultURL");

            //Then save the arrays with the new elements back to localstorage
            window.localStorage.setItem("searchName", JSON.stringify(tempsearchName));
            window.localStorage.setItem("humanName", JSON.stringify(humanName));
            window.localStorage.setItem("lastResultNumber", JSON.stringify(lastResultNumber));
            window.localStorage.setItem("lastResultURL", JSON.stringify(lastResultURL));

            //Alert the user that the search was saved
            document.getElementById("modalBodyText").innerText = "You've successfully subscribed to the search: " + document.getElementById("textBox").value;

        }

    } //end subscribe to search

//-------------------------misc functions
    //shows a page informing you that you don't have any saved searches.
    function firstRun(){
        var infoElement = document.createElement("a");
        infoElement.innerText = ("\n\n\n\nYou don't have any saved searches.\n\nGet out there and start searching. Once you find a search you want to follow, hit the 'Show Subscribe dialog' under the search bar\n\n");
        infoElement.style.fontSize = "140%";
        document.getElementById("myPage").getElementsByClassName("page-content")[0].append(infoElement);

        var returnElement = document.createElement("a");
        returnElement.innerText = "(Click here to return to the front page(jk, doesn't actually work))";
        returnElement.style.fontSize = "130%";
        returnElement.style.href = "https://e-hentai.org/";
        document.getElementById("myPage").getElementsByClassName("page-content")[0].append(returnElement);
    }

})();