npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@palerock/hooker-js

v0.5.9056

Published

用于劫持方法,进行AOP切面操作

Downloads

2

Readme

Hooker JS gitee.png github.png

用于劫持方法,对目标进行 AOP 切面操作
基于 ES5 语法,用于快速开发


快速入门

eHook.hookBefore(window,'alert',function(method,args){
	args[0] = args[0] + '[被劫持的参数]';
});
alert('hello eHook'); // hello eHook[被劫持的参数]

全局对象:eHookaHook

  • eHook:包含aop劫持的基本方法
  • aHook:包含 Ajax Url 劫持的基本方法

引入方法

  1. 下载源码中的 hooker.jshooker-mini.js 位于目录 /build,然后通过 <script> 标签引入,该方式引入即为全局对象:eHookaHook
  2. npm / yarn 引入
    	npm i @palerock/hooker-js
    	# 或
    	yarn add @palerock/hooker-js
    然后通过 import 或 require 引入
    	var hookJS = require('@palerock/hooker-js');
    	var eHook = hookJS.eHook;
    	var aHook = hookJS.aHook;
    	// more...

API 文档

eHook对象

hookBefore(parent, methodName, before, context)

在指定方法前执行

parent: 类型:object,必须,指定方法所在的对象

methodName: 类型:string,必须,指定方法的名称

before: 类型:function,必须,回调方法,该方法在指定方法运行前执行 回调参数:

  • method: 指定的原方法
  • args: 原方法运行的参数(在此改变参数值会影响后续指定方法的参数值)

context:
类型:object,可选,回调方法的上下文

返回值:
类型:number,劫持id(用于解除劫持)


hookCurrent(parent, methodName, current, context)

劫持方法的运行,在对制定方法进行该劫持的时候,指定方法不会主动执行,替换为执行参数中的current方法

注意:该方法只能对指定方法进行一次劫持,若再次使用该方法劫持就会覆盖之前的劫持[可以和hookBefore,hookAfter共存,且hookBefore和hookAfter可以对同个指定方法多次劫持]

parent:
类型:object,必须,指定方法所在的对象

methodName:
类型:string,必须,指定方法的名称

current:
类型:function,必须,回调方法,该方法在指定方法被调用时执行 回调参数及返回值:

  • parent: 指定方法所在的对象,类型: object
  • method: 指定的原方法,类型: function
  • args: 原方法的参数,类型: array
  • 返回值: 规定被劫持方法的返回值,类型: *

context:
类型:object,可选,回调方法的上下文

返回值:
类型:number,劫持id(用于解除劫持)

例:

eHook.hookCurrent(Math, 'max', function (p, m, args) {
     return m.apply(Math, args) + 1;
});
console.log(Math.max(1, 8)); // 9

hookAfter(parent, methodName, after, context)

在指定方法后执行

parent:
类型:object,必须,指定方法所在的对象

methodName:
类型:string,必须,指定方法的名称

after:
类型:function,必须,回调方法,该方法在指定方法运行后执行
回调参数及返回值:

  • method: 指定的原方法,类型: function
  • args: 原方法的参数,类型: array
  • result: 原方法的返回值,类型: *
  • 返回值: 规定被劫持方法的返回值,类型: *

context:
类型:object,可选,回调方法的上下文

返回值:
类型:number,劫持id(用于解除劫持)

例:

eHook.hookAfter(Math, 'max', function (m, args, result) {
     return result + 1;
});
console.log(Math.max(1,8)); // 9

hookReplace(parent, methodName, replace, context)

劫持替换指定方法

注意:该方法会覆盖指定劫持方法在之前所进行的一切劫持,也不能重复使用,并且不和hookAfter,hookCurrent,hookBefore共存,在同时使用的情况下,优先使用hookReplace而不是其他的方法

parent:
类型:object,必须,指定方法所在的对象

methodName:
类型:string,必须,指定方法的名称

replace:
类型:function,必须,回调方法,该方法的返回值便是替换的方法
回调参数及返回值:

  • method: 指定的原方法,类型: function
  • 返回值: 规定被替换的方法内容,类型: function

context:
类型:object,可选,回调方法的上下文

返回值:
类型:number,劫持id(用于解除劫持)

例:

eHook.hookReplace(Math,'max',function (m) {
     return Math.min;
});
console.log(Math.max(1, 8)); // 1

文档持续撰写中...


相关地址

如有疑问请评论或留言