GayRomeo Optimizer

By marvinate Last update Feb 1, 2012 — Installed 1,758 times.

There are 3 previous versions of this script.

// ==UserScript==
// @name           GayRomeo Optimizer
// @namespace      http://marvinate.wordpress.com
// @description    Optimize GayRomeo (and PlanetRomeo) pages
// @include        http*://*.gayromeo.com/*
// @include        http*://*.planetromeo.com/*
// @include        http*://83.98.143.20/*
// @version        1.2.1
// ==/UserScript==

// Version 1.1 [2011-11-14]
// [*] Changed internal system to use real objects instead of procedural stuff
//
// Version 1.1a [2011-11-23]
// [*] Optimized JavaScript code - yes, back to procedural stuff... just shut up ;-)
//
// Version 1.2 [2012-01-14]
// [+] Integrated callback to GR-Tools
//
// Version 1.2.1 [2012-02-01]
// [*] Added GayRomeo IP address as default include
// [*] Special handling for club pages (thanks to gymnazein)


// -----------------------------------------------------------------------------
// ---  Search list enhancements  ----------------------------------------------
// -----------------------------------------------------------------------------

if(document.location.href.indexOf("/search/index.php") > -1 || document.location.href.indexOf("/search/?") > -1) {

  function insertNextPageLink(pNextPageIndex) {
    var targetPageIndex                 = pNextPageIndex ? pNextPageIndex : grSearchPageIndex + 1;
    if(grNextPageUrl != null && targetPageIndex <= 20) {

      var nextPageLink                  = document.createElement("a");
      nextPageLink.setAttribute("style", "display: block; text-align: left; border: 1px solid #ffffff; padding: 5px 5px 5px 5px;");
      nextPageLink.setAttribute("name", "gayromeooptimizer_" + targetPageIndex);
      nextPageLink.setAttribute("href", "#gayromeooptimizer_" + targetPageIndex);
      nextPageLink.addEventListener("click", function() { appendSearchList(targetPageIndex); }, true);
      nextPageLink.addEventListener("mouseover", function() { appendSearchList(targetPageIndex); }, true);
      nextPageLink.innerHTML            = "Load next page...";
      grOptimizeAreaElement.replaceContent(nextPageLink);

    }
  }

  function appendSearchList(pNextPageIndex) {

    var loadingMessageDiv               = document.createElement("div");
    loadingMessageDiv.setAttribute("style", "border: 1px solid #ffffff; padding: 5px 5px 5px 5px;");
    loadingMessageDiv.innerHTML         = "Loading next page...";
    grOptimizeAreaElement.replaceContent(loadingMessageDiv);

    var thisPageParameterStart          = document.location.href.indexOf("?");
    var thisPageBaseUrl                 = thisPageParameterStart < 0 ? document.location.href : document.location.href.substring(0, thisPageParameterStart);
    var nextPageOriginalUrl             = thisPageBaseUrl + grNextPageUrl.getAttribute("href");
    var nextPageMatchedArray            = /(.*?)resultPage=\d+(.*)/.exec(nextPageOriginalUrl);
    var nextPageUrl                     = nextPageMatchedArray == null ? nextPageOriginalUrl : (nextPageMatchedArray[1] + "&resultPage=" + pNextPageIndex + nextPageMatchedArray[2]);

    MV_sendRequest({
      url: nextPageUrl,
      xpath: "//table[2]",
      onComplete: appendSearchListFromResponse,
      onError: appendSearchListError,
      pageIndex: pNextPageIndex
    });

  }

  function appendSearchListFromResponse(pElement, pRequest, pResponse) {

    optimizeListTable(pElement);

    var nextPageTableHeaderContent      = "";
    nextPageTableHeaderContent         += "Result list page " + pRequest.pageIndex;

    var nextPageTableHeader             = document.createElement("div");
    nextPageTableHeader.setAttribute("style", "padding: 5px 5px 5px 5px; border: 1px solid #ffffff");
    nextPageTableHeader.innerHTML       = nextPageTableHeaderContent;

    var nextPageContentArea             = document.createElement("div");
    nextPageContentArea.appendChild(nextPageTableHeader);
    nextPageContentArea.appendChild(pElement);
    grOptimizeAreaElement.parentNode.insertBefore(nextPageContentArea, grOptimizeAreaElement);

    insertNextPageLink(pRequest.pageIndex + 1);

    // retrigger GRT-Tools if installed
    // (http://userscripts.org/scripts/show/33184)
    var grt                             = (top.wrappedJSObject||top).GRT || {};
    if(grt.retrigger) {
      grt.retrigger();
    }

  }

  function appendSearchListError(pRequest, pResponse, pException) {
    var errorElementContent             = "Cannot load next page";
    if(pException != null) {
      errorElementContent              += " [" + e + "]";
    }
    var errorElement                    = document.createElement("div");
    errorElement.innerHTML              = errorElementContent;
    errorElement.setAttribute("style", "border: 1px solid red; background-color: red; padding: 5px 5px 5px; font-weight: bold;");
    grOptimizeAreaElement.replaceContent(errorElement);
  }

  function optimizeListTable(pTable) {
  }

  MV_removeElementsByPath("//div[contains(@id, 'div-gpt-ad')]");

  var grSearchPageIndexRegexArray       = /.*?resultPage=(\d+)/.exec(document.location.href);
  var grSearchPageIndex                 = grSearchPageIndexRegexArray == null ? 1 : parseInt(grSearchPageIndexRegexArray[1]);
  var grNextPageUrl                     = document.evaluate("//body/table[3]/tbody/tr/td/a[last()]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  var grOptimizeAreaElement             = document.createElement("div");
  grOptimizeAreaElement.replaceContent  = function(pNewContent) {
    this.innerHTML                      = "";
    this.appendChild(pNewContent);
  };

  var contentTableElement               = document.evaluate("html/body/table[2]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  var optimizeAreaParentElement         = document.createElement("div");
  optimizeAreaParentElement.appendChild(grOptimizeAreaElement);
  if(contentTableElement != null) {
    contentTableElement.parentNode.insertBefore(grOptimizeAreaElement, contentTableElement.nextSibling);
    optimizeListTable(document.evaluate("//table[2]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0));
    insertNextPageLink();
  }

}

if(document.location.href.indexOf("/auswertung/pix/popup") > -1) {
  var imageElementsResult               = document.evaluate("//img", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  for(var i = 0; i < imageElementsResult.snapshotLength; i++) {
    imageElementsResult.snapshotItem(i).addEventListener("click", function() {
      window.close();
    }, false);
  }
}


// -----------------------------------------------------------------------------
// ---  Includes  --------------------------------------------------------------
// -----------------------------------------------------------------------------

// Include start [xmlhttpUtil.js]
/**
 * Sends the request to the remote system and evaluates the response which
 * must be valid HTML and contain a specified element identifiable by an XPath
 * expression
 *
 * Expected properties in the request are:
 * url
 *   the URL to which the request will be made
 * xpath
 *   the XPath expression that must evaluate to an element that will be
 *   extracted from the response received by the remote system
 * onComplete
 *   a function that will be called when the result has been received and the
 *   content should be displayed
 * onError
 *   a function that will be called if the request cannot be sent or the
 *   response received is invalid
 */
function MV_sendRequest(pRequest) {

  var processResponse = function(pResponse) {
    var responseHtmlStart                 = pResponse.responseText.indexOf("<html");
    var responseHtmlEnd                   = pResponse.responseText.indexOf("</html>");
    if(responseHtmlStart < 0 || responseHtmlEnd < 0) {
      pRequest.onError(pRequest, pResponse, "Invalid HTML document received"); // Invalid response received
    } else {
      var responseHtmlElement             = document.createElement("html");
      responseHtmlElement.innerHTML       = pResponse.responseText.substring(responseHtmlStart, responseHtmlEnd + "</html>".length);
      var responseXpathResult             = document.evaluate(pRequest.xpath, responseHtmlElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
      if(responseXpathResult.snapshotLength <= 0) {
        pRequest.onError(pRequest, pResponse, "Invalid HTML document received"); // Invalid response received
      } else {
        pRequest.onComplete(responseXpathResult.snapshotItem(0), pRequest, pResponse);
      }
    }
  };

  try {
    GM_xmlhttpRequest({
      method: "GET",
      url: pRequest.url,
      onload: processResponse,
      onerror: function(pResponse) { pRequest.onError(pRequest, pResponse, null); }
    });
  } catch(e) {
    pRequest.onError(pRequest, pResponse, null);
  }

}
// Include end [xmlhttpUtil.js]
// Include start [domUtil.js]
function MV_removeElementsByPath(pPath) {
  var pathResult                        = document.evaluate(pPath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  if(pathResult.snapshotLength > 0) {
    for(var i=0; i < pathResult.snapshotLength; i++) {
      var pathNode                      = pathResult.snapshotItem(i);
      pathNode.parentNode.removeChild(pathNode);
    }
  }
}

function MV_getElementByPath(pPath) {
  var pathResult                        = document.evaluate(pPath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  return pathResult.snapshotLength <= 0 ? null : pathResult.snapshotItem(0);
}

function MV_createButton(pAttributes, pClickListener) {
  pAttributes.type                      = "button";
  var resultElement                     = MV_createElement("input", pAttributes);
  if(pClickListener != null) {
    resultElement.addEventListener("click", pClickListener, true);
  }
  return resultElement;
}

function MV_createElement(pElementName, pAttributes, pInnerHtml) {
  var resultElement                     = document.createElement(pElementName);
  for(attributeName in pAttributes) {
    resultElement.setAttribute(attributeName, pAttributes[attributeName]);
  }
  if(pInnerHtml != null) {
    resultElement.innerHTML           = pInnerHtml;
  }
  return resultElement;
}
// Include end [domUtil.js]