Camwhores.tv user preview inside messages

Sends background requests to every visible user by extracting user IDs and crawls for user info to display it ahead of time, resulting in less clicks, more info.

As of 2024-04-04. See the latest version.

// ==UserScript==
// @name         Camwhores.tv user preview inside messages
// @namespace    https://sleazyfork.org/users/1281730-vipprograms
// @version      0.8
// @description  Sends background requests to every visible user by extracting user IDs and crawls for user info to display it ahead of time, resulting in less clicks, more info.
// @author       vipprograms
// @match        https://www.camwhores.tv/my/messages/*
// @grant        GM_xmlhttpRequest
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAMUExURQAAAP8ANwwA/////7gbQJkAAAABdFJOUwBA5thmAAAAAWJLR0QDEQxM8gAAAAd0SU1FB+gDHhIuCjXV/h8AAAA4SURBVAjXY2ANDQ1gEA0NDWEIYWBgZAhgAAIUghEiC1YHBhpMDRpIhBbXghUMXKtWLWBgWqHVAACjlwz/pN0YPwAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyNC0wMy0zMFQxODo0NjowOSswMDowME+iXNIAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjQtMDMtMzBUMTg6NDY6MDkrMDA6MDA+/+RuAAAAKHRFWHRkYXRlOnRpbWVzdGFtcAAyMDI0LTAzLTMwVDE4OjQ2OjEwKzAwOjAwMNiA/AAAAABJRU5ErkJggg==
// ==/UserScript==

(function() {
    'use strict';



    // Check if on the messages page
    if (window.location.href == "https://www.camwhores.tv/my/messages/") {

        // Function to apply styles to unread notifications
        // Function to apply styles to unread notifications
        function applyStylesToNotifications() {
            console.log("Applying styles to notifications");
            var unreadNotifications = document.querySelectorAll('.unread-notification');
            unreadNotifications.forEach(function(notification) {
                notification.style.top = "0";
                notification.style.left = "0";
            });
        }

        // Function to process thumbnails
        function processThumbnails() {
            // console.log("Processing thumbnails");
            var thumbnails = document.querySelectorAll('strong.title');
            if (thumbnails.length === 0) {
                console.log("Thumbnails not found. Retrying in 1000 milliseconds.");
                setTimeout(function() {
                    processThumbnails(); // Retry processing thumbnails
                }, 1000);
                return false; // Return false if thumbnails not found
            }

            thumbnails.forEach(function(thumbnail) {
                var usernameText = thumbnail.textContent.trim();
                if (usernameText) {
                    var usernameLink = document.querySelector('a[title="' + usernameText + '"]');
                    if (usernameLink) {
                        var href = usernameLink.getAttribute('href');
                        var userIdMatch = href.match(/\/(\d+)\/$/);
                        if (userIdMatch && userIdMatch.length > 1) {
                            var userId = userIdMatch[1];
                            var usernameUrl = "https://www.camwhores.tv/members/" + userId + "/";
                            GM_xmlhttpRequest({
                                method: "GET",
                                url: usernameUrl,
                                onload: function(response) {
                                    var parser = new DOMParser();
                                    var htmlDoc = parser.parseFromString(response.responseText, "text/html");
                                    var infoMessage = htmlDoc.querySelector('.info-message');
                                    var usernameLink = document.querySelector('a[title="' + usernameText + '"]');



                                    console.log(usernameText + ": " + usernameUrl)






                                  // console.log(usernameLink)

                                    if (infoMessage && infoMessage.textContent.includes("is in your friends list.")) {
                                        if (usernameLink) {
                                            var divElement = document.createElement('div');
                                            divElement.textContent = "Friends ✅";
                                            divElement.style.position = "absolute";
                                            divElement.style.top = "0";
                                            divElement.style.right = "0";
                                            divElement.style.backgroundColor = "black";
                                            divElement.style.color = "white";
                                            divElement.style.padding = "3px 5px";

                                                if (usernameLink) {
                                                  var firstDivElement = usernameLink.querySelector('div.img');
                                                  if (firstDivElement) {
                                                      firstDivElement.appendChild(divElement);
                                                }
                                            }
                                        }
                                    }
                                    if(infoMessage && infoMessage.textContent.includes("wants to be your friend.")){
                                      var divElement = document.createElement('div');
                                            divElement.textContent = "👀";
                                            divElement.title = "Wants to be your friend!";
                                            divElement.style.position = "absolute";
                                            divElement.style.bottom = "0";
                                            divElement.style.left = "0";
                                            // divElement.style.backgroundColor = "aquamarine";
                                            divElement.style.color = "white";
                                            divElement.style.fontSize = "1rem";
                                            divElement.style.padding = ".5rem .25rem";

                                            if (usernameLink) {
                                                var firstDivElement = usernameLink.querySelector('div.img');
                                                if (firstDivElement) {
                                                    firstDivElement.appendChild(divElement);
                                                }
                                            }
                                    }

                                    var titleVideosElement = htmlDoc.evaluate('//*[@id="list_videos_uploaded_videos"]/div[1]/h2', htmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                                    var videoNumber = 0;

                                    if (titleVideosElement && titleVideosElement.singleNodeValue) {
                                      var titleVideosText = titleVideosElement.singleNodeValue.textContent;
                                      if (titleVideosText) {
                                        var matches = titleVideosText.match(/\(([^)]+)\)/);
                                        videoNumber = matches ? matches[1] : 0;
                                      }
                                    }

                                    var divElement = document.createElement('div');
                                    divElement.textContent = videoNumber;
                                    divElement.style.position = "absolute";
                                    divElement.style.bottom = "0";
                                    divElement.style.right = "0";
                                    divElement.style.backgroundColor = "CadetBlue";
                                    divElement.style.color = "white";
                                    divElement.style.padding = "3px 5px";

                                    if (usernameLink) {
                                      var firstDivElement = usernameLink.querySelector('div.img');
                                      if (firstDivElement) {
                                        firstDivElement.appendChild(divElement);
                                      }
                                    }

                                }
                            });
                        } else {
                            console.log("User ID not found in href for:", usernameText);
                        }
                    } else {
                        console.log("Username link not found for:", usernameText);
                    }
                }
            });
            return true; // Return true if thumbnails processed successfully
        }

        // Function to add event listener to buttons
        function addButtonEventListener() {
            console.log("Adding event listener to buttons");
            document.addEventListener('click', function(event) {
                var target = event.target;
                if (target && target.matches('a[data-action="ajax"]')) {
                    // console.log("Button clicked");
                    setTimeout(function() {
                        applyStylesToNotifications();
                        processThumbnails();
                    }, 1000);
                }
            });
        }

        // Call the functions to apply styles and process thumbnails
        console.log("Calling functions to apply styles and process thumbnails");

        // Delay execution of initialization code by 1 second
        setTimeout(function() {
            applyStylesToNotifications();
            var thumbnailsProcessed = processThumbnails();
            if (!thumbnailsProcessed) {
                console.log("Processing thumbnails failed. Retrying in 1000 milliseconds.");
                setTimeout(function() {
                    processThumbnails(); // Retry processing thumbnails
                }, 1000);
            }
            addButtonEventListener();
        }, 1000);



    }else{







        // Function to fetch the URL and extract title, content, and joined date
      function fetchAndExtract(url) {
          GM_xmlhttpRequest({
              method: "GET",
              url: url,
              onload: function(response) {
                  // Parse the response text into a HTML document
                  var parser = new DOMParser();
                  var htmlDoc = parser.parseFromString(response.responseText, "text/html");

                  // Get the text of the specified elements
                  var titleElement = htmlDoc.evaluate('//*[@id="list_videos_uploaded_videos"]/div[1]/h2', htmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                  var contentElement = htmlDoc.evaluate('//*[@id="list_videos_uploaded_videos_items"]/div[1]/a/div[3]/div[1]/em', htmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                  var joinedDateElement = htmlDoc.evaluate('/html/body/div[2]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[3]/em', htmlDoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                  var joinedDateText = joinedDateElement.singleNodeValue.textContent;

                  // Check if the elements exist
                  if (titleElement && titleElement.singleNodeValue && contentElement && contentElement.singleNodeValue && joinedDateElement && joinedDateElement.singleNodeValue) {
                      var titleText = titleElement.singleNodeValue.textContent;
                      // Extract text inside parentheses from title
                      var extractedTitle = titleText.match(/\(([^)]+)\)/);
                      if (extractedTitle && extractedTitle.length > 1) {
                          var contentText = contentElement.singleNodeValue.textContent;
                          var generatedText = "<div id=gen_joined_text>Joined " + joinedDateText + "<br>" + extractedTitle[1] + " videos, last " + contentText + "</div>";
                          // Add generated text to the visible page
                          var messageDiv = document.evaluate('//*[@id="list_messages_my_conversation_messages_items"]/div/div[3]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                          if (messageDiv && messageDiv.singleNodeValue) {
                              messageDiv.singleNodeValue.innerHTML += generatedText;
                          }
                      } else {
                          console.log('Text inside parentheses in title not found.');
                      }
                  } else {
                      console.log('Title, content, or joined date element not found. Probably no videos uploaded yet.');
                      // Add "No videos" to the visible page
                      var messageDiv = document.evaluate('//*[@id="list_messages_my_conversation_messages_items"]/div/div[3]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
                      if (messageDiv && messageDiv.singleNodeValue) {
                          messageDiv.singleNodeValue.innerHTML += "<div id=gen_joined_text>Joined " + joinedDateText + "<br>" + "<b>No videos</b></div>";
                      }
                  }
              }
          });
      }



      // Get the href attribute of the first message
      var messageLink = document.evaluate('//*[@id="list_messages_my_conversation_messages_items"]/div/div[1]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);

      // Check if the element exists
      if (messageLink && messageLink.singleNodeValue) {
          // Get the URL from the message link
          var messageUrl = messageLink.singleNodeValue.href;
          // Fetch and extract title, content, and joined date from the URL
          fetchAndExtract(messageUrl);
      } else {
          console.log('Message link not found.');
      }

      // Remove class "bottom" from specified element
      var bottomElement = document.evaluate('//*[@id="list_messages_my_conversation_messages_items"]/div/div[3]/form/div', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
      if (bottomElement && bottomElement.singleNodeValue) {
          bottomElement.singleNodeValue.style.marginTop = "1em";
          bottomElement.singleNodeValue.style.marginBottom = "1em";
          bottomElement.singleNodeValue.classList.remove("bottom");
      } else {
          console.log('Bottom element not found.');
          setTimeout(function() {
            var gen_joined_text = document.getElementById('gen_joined_text');
            gen_joined_text.innerHTML = "<br>" + gen_joined_text.innerHTML;
            // gen_joined_text.style.marginTop = "1em";

            // gen_joined_text.classList.add("custom-margin-top");
        }, 200); // Adjust the delay time as needed


      }
    }
})();