MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / unHook

Method unHook

scripts/libs/js-hook/index.js:415–479  ·  view source on GitHub ↗

* Cancel the hook of a certain function * @param parentObj {Object} - required to cancel the parent object that the hook function depends on * @param hookMethods {Object|Array|RegExp|string} - required to cancel the function name of the hooked function or the matching rule of the function name

(parentObj, hookMethods, type, fn)

Source from the content-addressed store, hash-verified

413 * @returns {boolean}
414 */
415 unHook (parentObj, hookMethods, type, fn) {
416 if (!util.isRef(parentObj) || !hookMethods) {
417 return false
418 }
419
420 const t = this
421 hookMethods = t._getObjKeysByRule(parentObj, hookMethods)
422 hookMethods.forEach(methodName => {
423 if (!t.isAllowHook(parentObj, methodName)) {
424 return false
425 }
426
427 const hookMethod = parentObj[methodName]
428
429 if (!t.isHook(hookMethod)) {
430 return false
431 }
432
433 const hookMethodProperties = hookMethod[t.hookPropertiesKeyName]
434 const originMethod = hookMethodProperties.originMethod
435
436 if (type) {
437 const hookKeyName = type + 'Hooks'
438 const hooks = hookMethodProperties[hookKeyName] || []
439
440 if (fn) {
441 /* Delete the specified hook function under the specified type */
442 for (let i = 0; i < hooks.length; i++) {
443 if (fn === hooks[i]) {
444 hookMethodProperties[hookKeyName].splice(i, 1)
445 util.debug.log(`[unHook ${hookKeyName} func] ${util.toStr(parentObj)} ${methodName}`, fn)
446 break
447 }
448 }
449 } else {
450 /* Delete all hook functions under the specified type */
451 if (Array.isArray(hookMethodProperties[hookKeyName])) {
452 hookMethodProperties[hookKeyName] = []
453 util.debug.log(`[unHook all ${hookKeyName}] ${util.toStr(parentObj)} ${methodName}`)
454 }
455 }
456 } else {
457 /* Completely restore the hooked function */
458 if (util.isFn(originMethod)) {
459 parentObj[methodName] = originMethod
460 delete parentObj[methodName][t.hookPropertiesKeyName]
461
462 // Object.keys(hookMethod).forEach(keyName => {
463 // if (/Hooks$/.test(keyName) && Array.isArray(hookMethod[keyName])) {
464 // hookMethod[keyName] = []
465 // }
466 // })
467 //
468 // hookMethod.isHook = false
469 // parentObj[methodName] = originMethod
470 // delete parentObj[methodName].originMethod
471 // delete parentObj[methodName].hookMethod
472 // delete parentObj[methodName].isHook

Callers

nothing calls this directly

Calls 3

_getObjKeysByRuleMethod · 0.80
isAllowHookMethod · 0.80
isHookMethod · 0.80

Tested by

no test coverage detected