Giter Club home page Giter Club logo

hijackajax's Introduction

ajax请求劫持

用于三方代码或者chrome extension在无法修改原代码逻辑的情况下劫持页面原有ajax请求,对发送内容和响应内容进行修改。

hijackAjax(...rule)

劫持XMLHttpRequest

Kind: global function

Param Type Description
...rule Object 劫持规则

Properties

Name Type Description
rule.url string | RegExp | Array.<string> 原请求地址,若传入的参数未携带search参数,则匹配时也会忽略search参数;正则匹配时不会忽略search参数; 若域名和location域名相同,会忽略域名;字符串时支持结尾统配符*
[rule.before] overrideRequest 发送前的处理
[rule.after] overrideResponse 对返回值的处理, 默认情况下rule.after仅在readyState===4的时候调用,可通过rule.callAfterEveryState修改
[rule.callAfterEveryState] boolean 每次readystatechange变更时都调用rule.after去修改res

Example

hijackAjax({
	url: '/article/list',
	before: req => {
		req.url = 'recommend/list';
		let data = JSON.parse(req.data);
		data.name = "test";
		req.data = JSON.stringify(data)
		return req;
	},
	after: res => {
		let data = JSON.parse(res.responseText);
		data.name = 'test';
		res.responseText = JSON.stringify(data);
		return res;
	}
})

Typedefs

overrideRequestreq
overrideResponse*

overrideRequest ⇒ req

Kind: global typedef Returns: req - 修改后的请求数据,在传入的req上修改后返回

Param Type Description
req Object 原有请求数据

Properties

Name Type Description
req.url string 原有url,字符串,不包含search参数
req.method string 原有method
req.headers Object 原有headers,键值对象,不会传入null
req.search Object 原有search参数,键值对象,不会传入null
req.withCredentials boolean 原有的withCredentials
req.user string | null 原有的open传入的user,默认为null
req.password string | password 原有的open传入的password, 默认为null
req.data * 原有的data
req.async boolean 原有的async
req.mimeType string 原有的mimeType, overrideMimeType设置的mimeType
req.timeout number 原有的超时时间

overrideResponse ⇒ *

Kind: global typedef Returns: * - 修改后的res,在传入的res上修改后返回

Param Type Description
res Object 原有的res

Properties

Name Type Description
res.headers Object 原有headers,键值对象,永远不会传入null
res.status number 原有status
res.statusText string 原有statusText
res.responseType string 原有responseType
res.responseURL string 原有responseURL
res.response string 原有response
res.responseText string 原有responseText
res.responseXML string 原有responseXML

hijackajax's People

Contributors

replace5 avatar

Stargazers

 avatar YinYue avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.