Skip to content

Commit 5c4ef10

Browse files
author
wen.du
committed
1.Release npm plugin "ajax-hook"
2.Modify domo and readme.md 3.Use webpack as module bundler
1 parent b4ad653 commit 5c4ef10

13 files changed

+206
-23
lines changed

.gitignore

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
.idea
33
.DS_Store
4+
/node_modules
45

README.md

100644100755
+29-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Hook Javascript global XMLHttpRequest object。 And change the default AJAX
99

1010
## How to use
1111

12-
1. include the script file "wendu.ajaxhook.js"
12+
### **Using by script tag**
13+
14+
1. include the script file "ajaxhook.js"
1315

1416
```html
15-
<script src="wendu.ajaxhook.js"></script>
17+
<script src="ajaxhook.js"></script>
1618
```
1719

1820
2. hook the callbacks and functions you want .
@@ -54,6 +56,31 @@ The result :
5456

5557
**See the demo "demo.html" for more details.**
5658

59+
### Using in commonJs module build environment
60+
61+
Suppose you are using webpack as your module bundler, firstly Install ajax-hook plugin:
62+
63+
```javascript
64+
npm install ajax-hook --save-dev
65+
```
66+
And then require the ajax-hook module:
67+
```javascript
68+
const ah=require("ajax-hook")
69+
ah.hookAjax({
70+
onreadystatechange:function(xhr){
71+
...
72+
}
73+
onload:function(xhr){
74+
...
75+
},
76+
...
77+
})
78+
...
79+
ah.unHookAjax()
80+
```
81+
82+
83+
5784
## API
5885

5986
### hookAjax(ob)

demo.html demon/demo.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
99
<meta name="keywords" content="Ajax hook Demo">
1010
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
11-
<script src="wendu.ajaxhook.min.js"></script>
11+
<script src="../dist/wendu.ajaxhook.min.js"></script>
1212

1313
</head>
1414
<style>

demon/demoCommonJs.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="zh-cmn-Hans">
4+
<meta charset="UTF-8">
5+
<title>Ajax hook Demo</title>
6+
<meta name="renderer" content="webkit">
7+
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
8+
<meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
9+
<meta name="keywords" content="Ajax hook Demo">
10+
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
11+
<script src="../dist/test.min.js"></script>
12+
13+
</head>
14+
<style>
15+
html {
16+
font-size: 20px;
17+
-webkit-user-select: none;
18+
}
19+
</style>
20+
<body>
21+
<div style="background: #000; font-size: 38px; color: #ffef68; text-shadow: 2px 2px 10px #ffef68; width: 400px; height: 400px; text-align: center">
22+
<div style="padding-top: 100px"> Ajax Hook !</div>
23+
</div>
24+
open console panel to view log.
25+
<script>
26+
// $.get().done(function(d){
27+
// console.log(d.substr(0,30)+"...")
28+
// //use original XMLHttpRequest
29+
// console.log("unhook")
30+
// $.get().done(function(d){
31+
// console.log(d.substr(0,10))
32+
// })
33+
// })
34+
35+
</script>
36+
</body>
37+
</html>

demon/test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Created by du on 16/12/10.
3+
*/
4+
5+
const ah=require("ajax-hook")
6+
7+
ah.hookAjax({
8+
onreadystatechange:function(xhr){
9+
console.log("onreadystatechange called: %O",xhr)
10+
//return true
11+
12+
},
13+
onload:function(xhr){
14+
console.log("onload called: %O",xhr)
15+
xhr.responseText="hook"+xhr.responseText;
16+
//return true;
17+
},
18+
open:function(arg,xhr){
19+
console.log("open called: method:%s,url:%s,async:%s",arg[0],arg[1],arg[2],xhr)
20+
arg[1]+="?hook_tag=1";
21+
//统一添加请求头
22+
23+
},
24+
send:function(arg,xhr){
25+
console.log("send called: %O",arg[0])
26+
xhr.setRequestHeader("_custom_header_","ajaxhook")
27+
},
28+
setRequestHeader:function(arg,xhr){
29+
console.log("setRequestHeader called!",arg)
30+
}
31+
})
32+
33+
$.get().done(function(d){
34+
console.log(d.substr(0,30)+"...")
35+
//use original XMLHttpRequest
36+
console.log("unhook")
37+
ah.unHookAjax()
38+
$.get().done(function(d){
39+
console.log(d.substr(0,10))
40+
})
41+
42+
})

dist/test.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/wendu.ajaxhook.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "ajax-hooks",
3+
"version": "1.0.0",
4+
"author": {
5+
"name": "wendux",
6+
"email": "824783146@qq.com",
7+
"url": "https://github.com/wendux"
8+
},
9+
"description": "Hook Javascript global XMLHttpRequest object。 And change the default AJAX request and response .",
10+
"keywords": [
11+
"ajax hook",
12+
"hook ajax",
13+
"ajax-hook",
14+
"wendux"
15+
],
16+
"respository": {
17+
"type": "git",
18+
"url": "https://github.com/wendux/Neat/Ajax-hook"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/wendux/Ajax-hook/issues"
22+
},
23+
"scripts": {
24+
"build": "webpack"
25+
},
26+
"devDependencies": {
27+
"ajax-hook": "^1.0.0",
28+
"webpack": "^1.13.2"
29+
}
30+
}

src/.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/extend

wendu.ajaxhook.js src/ajaxhook.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
/*
22
* author: wendu
3-
* email: duwen32767@163.com
3+
* email: 824783146@qq.com
44
* source code: https://github.com/wendux/Ajax-hook
55
**/
6-
76
!function (ob) {
87
var realXMLHttpRequest;
98
ob.hookAjax = function (funs) {
10-
"use strict";
119
realXMLHttpRequest = realXMLHttpRequest || XMLHttpRequest
1210
XMLHttpRequest = function () {
1311
this.xhr = new realXMLHttpRequest;
1412
for (var attr in this.xhr) {
1513
var type = "";
1614
try {
1715
type = typeof this.xhr[attr]
18-
} catch (e) {
19-
20-
}
16+
} catch (e) {}
2117
if (type === "function") {
2218
this[attr] = hookfun(attr);
2319
} else {
@@ -31,16 +27,16 @@
3127

3228
function getFactory(attr) {
3329
return function () {
34-
return this[attr+"_"]||this.xhr[attr]
30+
return this[attr + "_"] || this.xhr[attr]
3531
}
3632
}
3733

3834
function setFactory(attr) {
3935
return function (f) {
4036
var xhr = this.xhr;
41-
var that=this;
42-
if(attr.indexOf("on")!=0){
43-
this[attr+"_"]=f;
37+
var that = this;
38+
if (attr.indexOf("on") != 0) {
39+
this[attr + "_"] = f;
4440
return;
4541
}
4642
if (funs[attr]) {
@@ -56,18 +52,17 @@
5652
function hookfun(fun) {
5753
return function () {
5854
var args = [].slice.call(arguments)
59-
if (funs[fun] && funs[fun].call(this, args,this.xhr)) {
55+
if (funs[fun] && funs[fun].call(this, args, this.xhr)) {
6056
return;
6157
}
6258
this.xhr[fun].apply(this.xhr, args);
6359
}
6460
}
6561
return realXMLHttpRequest;
6662
}
67-
6863
ob.unHookAjax = function () {
6964
if (realXMLHttpRequest) XMLHttpRequest = realXMLHttpRequest;
70-
realXMLHttpRequest=undefined;
65+
realXMLHttpRequest = undefined;
7166
}
72-
7367
}(window)
68+
//}(module.exports)

src/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "ajax-hook",
3+
"version": "1.0.0",
4+
"main":"ajaxhook.js",
5+
"description": "Hook Javascript global XMLHttpRequest object。 And change the default AJAX request and response.",
6+
7+
"keywords": [
8+
"ajax hook",
9+
"hook ajax",
10+
"ajax-hook",
11+
"wendux"
12+
],
13+
"license": "MIT",
14+
15+
"author": {
16+
"name":"wendux",
17+
"email":"824783146@qq.com",
18+
"url":"https://github.com/wendux"
19+
},
20+
"respository": {
21+
"type": "git",
22+
"url": "https://github.com/wendux/Neat/Ajax-hook"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/wendux/Ajax-hook/issues"
26+
},
27+
"homepage": "https://github.com/wendux"
28+
}

webpack.config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Created by du on 16/9/24.
3+
*/
4+
var path = require('path');
5+
var fs = require("fs");
6+
var webpack = require('webpack');
7+
module.exports = {
8+
entry: {
9+
"wendu.ajaxhook": "./src/ajaxhook.js",
10+
"test" :["./demon/test.js"]
11+
},
12+
output: {
13+
path: "./dist",
14+
filename: "[name].min.js"
15+
},
16+
17+
plugins: [
18+
new webpack.optimize.UglifyJsPlugin({
19+
compress: {
20+
warnings: true
21+
}
22+
}),
23+
]
24+
}
25+
26+

wendu.ajaxhook.min.js

-6
This file was deleted.

0 commit comments

Comments
 (0)