lib:textjack

3/5/2025, 7:16:43 PM

目前為 2025-03-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        lib:textjack
// @version     4
// @match       *://*/*
// @run-at      document-start
// @author      rssaromeo
// @license     GPLv3
// @include     *
// @grant       none
// @icon        data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAMAAABiM0N1AAAAAXNSR0IB2cksfwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAHJQTFRFAAAAEIijAo2yAI60BYyuF4WaFIifAY6zBI2wB4usGIaZEYigIoiZCIyrE4igG4iYD4mjEomhFoedCoqpDIqnDomlBYyvE4efEYmiDYqlA42xBoytD4mkCYqqGYSUFYidC4qoC4upAo6yCoupDYqmCYur4zowOQAAACZ0Uk5TAO////9vr////1+/D/+/L+/Pf/////+f3///////H4////////+5G91rAAACgUlEQVR4nM2Y22KjIBCGidg1264liZqDadK03X3/V2wNKHMC7MpF/xthHD5mgERAqZhWhfYqH6K+Qf2qNNf625hCoFj9/gblMUi5q5jLkXLCKudgyiRm0FMK82cWJp1fLbV5VmvJbCIc0GCYaFqqlDJgADdBjncqAXYobm1xh72aFMflbysteFfdy2Yi1XGOm5HGBzQ1dq7TzEoxjeNTjQZb7VA3e1c7+ImgasAgQ9+xusNVNZIo5xmOMgihIS2PbCQIiHEUdTvhxCcS/kPomfFI2zHy2PkWmA6aNatIJpKFJyekyy02xh5Y3DI9T4aOT6VhIUrsNTFp1pf79Z4SIIVDegl6IJO6cHiL/GimIZDhgTu/BlYWCQzHMl0zBWT/T3KAhtxOuUB9FtBrpsz0RV4xsjHmW+UCaffcSy/5viMGer0/6HdFNMZBq/vjJL38H9Dqx4Fuy0Em12DbZy+9pGtiDijbglwAehyj11n0tRD3WUBm+lwulE/8h4BuA+iWAQQnteg2Xm63WQLTpnMnpjdge0Mgu/GRPsV4xdjQ94Lfi624fabhDkfUqIKNrM64Q837v8yL0prasepCgrtvw1sJpoqanGEX7b5mQboNW8eawXaWXTMfMGxub472hzWzHSn6Sg2G9+6TAyRruE71s+zAzjWaknoyJCQzwxrghH2k5FDT4eqWunuNxyN9QCGcxVod5oADbYnIUkDTGZEf1xDJnSFteQ3KdsT8zYDMQXcHxsevcLH1TrsABzkNPyA/L7b0jg704viMMlpQI96WsHknCt/3YH0kOEo9zcGkwrFK39ck72rmoehmKqo2RKlilzSy/nJKEV45CT38myJp456fezktHjN5aeMAAAAASUVORK5CYII=
// @description 3/5/2025, 7:16:43 PM
// @namespace https://greasyfork.org/users/1184528
// ==/UserScript==

let obslist = []
var textJackList = []
Object.assign(window, console)
const a = loadlib("allfuncs")

function replaceText(text) {
  var oldtext = text
  for (var cb of textJackList) {
    text = cb(text)
    if (text !== oldtext) {
      return (text = replaceText(text))
    }
  }
  return text
}
// Function to handle text changes
function handleTextChange(mutations) {
  disableObservers()
  mutations.forEach((mutation) => {
    if (mutation.type === "childList") {
      mutation.addedNodes.forEach((node) => {
        if (node.nodeType === Node.TEXT_NODE) {
          // Filter text for text nodes
          var newtext = replaceText(node.textContent)
          if (node.textContent != newtext) {
            node.textContent = newtext
            // log(1, node.textContent , newtext)
          }
        } else if (node.nodeType === Node.ELEMENT_NODE) {
          // debugger
          // If it's an element, check its text content
          node.childNodes.forEach((child) => {
            if (child.nodeType === Node.TEXT_NODE) {
              var newtext = replaceText(child.textContent)
              if (child.textContent != newtext) {
                child.textContent = newtext
                // log(2, child.textContent , newtext)
              }
            }
          })
          // Observe any shadow roots
          if (node.shadowRoot) {
            createObserver(node.shadowRoot)
          }
        }
      })
    } else if (mutation.type === "characterData") {
      // Filter text for character data changes
      mutation.target.textContent = replaceText(
        mutation.target.textContent
      )
    }
  })
  enableObservers()
}

// Create a MutationObserver for the main document
function createObserver(elem) {
  var obs = new MutationObserver((mutations) => {
    handleTextChange(mutations)
    mutations.forEach((mutation) => {
      mutation.addedNodes.forEach((node) => {
        // If the added node is a shadow host, observe its shadow root
        if (node.nodeType === Node.ELEMENT_NODE && node.shadowRoot) {
          createObserver(node.shadowRoot)
        }
      })
    })
  })

  // Start observing the document body
  obslist.push([
    obs,
    elem,
    {
      childList: true,
      subtree: true,
      characterData: true,
    },
  ])
}
// Function to enable all observers
function enableObservers() {
  try {
    disableObservers()
  } catch (e) {}
  // warn(obslist, 1)
  obslist.forEach(([observer, target, opts]) =>
    observer.observe(target, opts)
  )
}

// Function to disable all observers
function disableObservers() {
  // warn(obslist, 0)
  obslist.forEach(([observer]) => observer.disconnect())
}

// Optionally, filter existing text content on page load
function filterExistingText(node) {
  disableObservers()
  if (node.nodeType === Node.TEXT_NODE) {
    node.textContent = replaceText(node.textContent)
  } else if (node.nodeType === Node.ELEMENT_NODE) {
    node.childNodes.forEach(filterExistingText)
    // If the element has a shadow root, filter its content
    if (node.shadowRoot) {
      filterExistingText(node.shadowRoot)
    }
  }
  enableObservers()
}

// Filter existing text in the document
var waitingForBody = false
if (document.body) {
  setup()
} else {
  waitingForBody = true
}

loadlib("libloader").savelib("textjack", function newTextJack(cb) {
  textJackList.push(cb)
  if (document.body) {
    setup()
  } else {
    waitingForBody = true
  }
})
a.bodyload().then(() => {
  if (waitingForBody) {
    setup()
  }
})

function setup() {
  filterExistingText(document.body)
  createObserver(document.body)
  enableObservers()
}