Remove Bing Character Limit

Removes character limit from search input field

目前為 2023-06-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Remove Bing Character Limit
// @author       NOLIMIT
// @namespace    NOLIMIT
// @version      2.1
// @description  Removes character limit from search input field
// @match        https://www.bing.com/search?*
// @run-at       document-end
// @grant        none
// @license MIT

// ==/UserScript==

const DEBUG = false;

// Define an async function that waits for an element to appear in the DOM
async function waitForElement(root, selector) {
  return new Promise((resolve, reject) => {
    // Check if the element already exists
    if (root.querySelector(selector)) {
      // Resolve the promise with the element
      resolve(root.querySelector(selector));
    } else {
      // Create a mutation observer to watch for changes in the DOM
      const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
          // If new nodes are added
          if (mutation.type === "childList") {
            // Check if the element exists now
            if (root.querySelector(selector)) {
              // Resolve the promise with the element
              resolve(root.querySelector(selector));
              // Disconnect the observer and clear the timeout
              observer.disconnect();
              clearTimeout(timeout);
            }
          }
        });
      });
      // Start observing the root element and its descendants for changes
      observer.observe(root, { childList: true, subtree: true });
      // Set a timeout to reject the promise after 10 seconds
      const timeout = setTimeout(() => {
        // Disconnect the observer and reject the promise with an error
        observer.disconnect();
        reject(new Error("Timeout"));
      }, 10000);
    }
  });
}

const MAX_RETRIES = 3;
let retries = 0;

// Define an async function that removes the character limit from the search input field
const removeLimitation = async () => {
  try {
    // Wait for the main host element to appear
    const mainHost = await waitForElement(document, ".cib-serp-main");
    // Get the shadow root of the main host element
    const mainRoot = mainHost.shadowRoot;
    // Wait for the second host element to appear inside the main root
    const secondHost = await waitForElement(mainRoot, "#cib-action-bar-main");
    // Get the shadow root of the second host element
    const secondRoot = secondHost.shadowRoot;
    // Wait for the search input element to appear inside the second root
    const searchInput = await waitForElement(secondRoot, "#searchbox");
    // Remove the maxlength attribute from the search input element
    searchInput.removeAttribute("maxlength");
    // Wait for the letter counter element to appear inside the second root
    const letterCounter = await waitForElement(secondRoot, ".letter-counter");
    // Change the inner HTML of the letter counter element to an infinity symbol
    letterCounter.innerHTML = "∞";
    console.log("Character limit was removed.");

    // Create a mutation observer to watch for changes in the second root
    const observer = new MutationObserver((mutationsList) => {
      for (const mutation of mutationsList) {
        // If nodes are removed or replaced
        if (!searchInput.isConnected) {
          // Call the removeLimitation function again and disconnect the observer
          removeLimitation();
          observer.disconnect();
        }
      }
    });

    // Start observing the second root element and its children for changes
    observer.observe(secondRoot, { childList: true });
  } catch (error) {
    console.error(error);
    // If an error occurs, retry up to MAX_RETRIES times
    if (retries < MAX_RETRIES) {
      retries++;
      console.log(`Retrying... (${retries}/${MAX_RETRIES})`);
      removeLimitation();
    } else {
      console.log("Failed to remove character limit.");
    }
  }
};

// Call the removeLimitation function when the script runs
removeLimitation();