Greasy Fork is available in English.

Nitro Type Userscript Utils

Contains commonly used functions on all my Nitro Type userscripts.

Dette scriptet burde ikke installeres direkte. Det er et bibliotek for andre script å inkludere med det nye metadirektivet // @require https://update.greatest.deepsurf.us/scripts/443718/1042360/Nitro%20Type%20Userscript%20Utils.js

  1. /** Finds the React Component from given dom. */
  2. const findReact = (dom, traverseUp = 0) => {
  3. const key = Object.keys(dom).find((key) => key.startsWith("__reactFiber$"))
  4. const domFiber = dom[key]
  5. if (domFiber == null) return null
  6. const getCompFiber = (fiber) => {
  7. let parentFiber = fiber?.return
  8. while (typeof parentFiber?.type == "string") {
  9. parentFiber = parentFiber?.return
  10. }
  11. return parentFiber
  12. }
  13. let compFiber = getCompFiber(domFiber)
  14. for (let i = 0; i < traverseUp && compFiber; i++) {
  15. compFiber = getCompFiber(compFiber)
  16. }
  17. return compFiber?.stateNode
  18. }
  19.  
  20. /** Create a Console Logger with some prefixing. */
  21. const createLogger = (namespace) => {
  22. const logPrefix = (prefix = "") => {
  23. const formatMessage = `%c[${namespace}]${prefix ? `%c[${prefix}]` : ""}`
  24. let args = [console, `${formatMessage}%c`, "background-color: #D62F3A; color: #fff; font-weight: bold"]
  25. if (prefix) {
  26. args = args.concat("background-color: #4f505e; color: #fff; font-weight: bold")
  27. }
  28. return args.concat("color: unset")
  29. }
  30. return {
  31. info: (prefix) => Function.prototype.bind.apply(console.info, logPrefix(prefix)),
  32. warn: (prefix) => Function.prototype.bind.apply(console.warn, logPrefix(prefix)),
  33. error: (prefix) => Function.prototype.bind.apply(console.error, logPrefix(prefix)),
  34. log: (prefix) => Function.prototype.bind.apply(console.log, logPrefix(prefix)),
  35. debug: (prefix) => Function.prototype.bind.apply(console.debug, logPrefix(prefix)),
  36. }
  37. }