DirectLink Censys

add direct links to censys

// ==UserScript==
// @name         DirectLink Censys
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  add direct links to censys
// @author       SH3LL
// @match        https://search.censys.io/search*
// ==/UserScript==


(function() {
    'use strict';
    const topMenu = document.querySelector('ul.nav-topmenu');
    const resultSet = document.getElementById('resultset');

    if (!topMenu || !resultSet) {
        console.log("Error: 'ul.nav-topmenu' or 'resultset' not found on this page.");
        return;
    }

    let port = "";

    // PORT QUERY
    const portPrompt = prompt("Port number to append in the url (empty=none/80):");
    if (portPrompt !== null) {
      port = portPrompt.trim();
      if (!isNaN(port) && port !== "") {
          port = ":" + port;
      } else if (port !== ""){
          alert("Invalid port number");
          port = ""; // Reset port if invalid
      }
    }

    // OPEN ALL BUTTON
    const openAllLink = document.createElement('a');
    openAllLink.href = '#'; //  Set an empty href (or a meaningful one if needed)
    openAllLink.textContent = '🌍️ Open All Links';
    openAllLink.style.color='red'
    openAllLink.style.marginLeft = '10px'; // Add some margin for better spacing
    openAllLink.addEventListener('click', openAllDirectLinks);
    topMenu.appendChild(openAllLink); // Aggiunge il link a ul.nav-topmenu

    // LINK GENERATION
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length > 0) {
                addAllDirectLinks();
            }
        });
    });
    const config = { childList: true, subtree: true };
    observer.observe(resultSet, config);

    function addAllDirectLinks() {
        observer.disconnect();
        const searchResultLinks = resultSet.querySelectorAll('.SearchResult.result a.SearchResult__title-text');
        searchResultLinks.forEach(link => {
            let host_url = link.children[1].innerText; // Assumes the host URL is in the second child element.  Check your HTML!
            if (!host_url) {
              console.warn("Host URL not found in link element. Skipping.");
              return; // Skip if host_url is not found.
            }

            const MyDirectLink = document.createElement('a');
            MyDirectLink.href = "http://" + host_url + port;
            MyDirectLink.innerText = '🌍️->Direct';
            MyDirectLink.style.color='green'
            MyDirectLink.style.marginLeft = '5px';
            MyDirectLink.classList.add('my-direct-link');
            MyDirectLink.target="_blank"; // Opens in new tab
            link.parentNode.insertBefore(MyDirectLink, link.nextSibling);
        });
        observer.observe(resultSet, config);
    }

    function openAllDirectLinks(event) {
        event.preventDefault();
        resultSet.querySelectorAll('a.my-direct-link').forEach(link => {
            window.open(link.href, '_blank');
        });
    }

    addAllDirectLinks();

})();