-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
86 lines (83 loc) · 2.55 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* @Author: wujiyu
* @Date: 2016-11-16 15:47:53
* @Last Modified by: wujiyu
* @Last Modified time: 2016-11-16 16:20:15
*/
var GmUtils = (function () {
function GmUtils(environment) {
this.env = environment;
}
GmUtils.prototype.setEnv = function (environment) {
this.env = environment;
};
GmUtils.prototype.getAll = function () {
var rtnVal = GM_listValues();
this.log('getAll:' + rtnVal);
return rtnVal;
};
GmUtils.prototype.getVal = function (key, defaultVal) {
var rtnVal = GM_getValue(key, defaultVal);
this.log('getVal:' + rtnVal);
return rtnVal;
};
GmUtils.prototype.setVal = function (key, val) {
GM_setValue(key, val);
};
GmUtils.prototype.delVal = function (key) {
GM_deleteValue(key);
};
GmUtils.prototype.log = function (message) {
//level 是可选的,默认值是0。其有效的值有:0 - 信息、1 - 警告、2 - 错误。
if (this.env.toLowerCase() === 'debug') {
GM_log('调试信息: ' + message);
}
else {
//GM_log(message);
}
};
GmUtils.prototype.addStyle = function(css){
GM_addStyle(css);
};
GmUtils.prototype.DOMAttrModified = function(obj, fn){
obj.addEventListener("DOMAttrModified", fn, false);
};
return GmUtils;
})();
function getDoc(url, callback) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
headers: {
'User-agent': window.navigator.userAgent,
'Content-type': null
},
onload: function (responseDetail) {
var doc = '';
if (responseDetail.status == 200) {
// For Firefox, Chrome 30+ Supported
doc = new DOMParser().parseFromString(responseDetail.responseText, 'text/html');
if (doc === undefined) {
doc = document.implementation.createHTMLDocument("");
doc.querySelector('html').innerHTML = responseText;
}
}
callback(doc, responseDetail.finalUrl);
}
});
}
function postDoc(url, callback, data) {
GM_xmlhttpRequest({
anonymous: true,
method: 'POST',
url: url,
headers: {
'User-agent': window.navigator.userAgent,
'Content-type': 'application/x-www-form-urlencoded'
},
data: data,
onload: function (responseDetail) {
callback(responseDetail.responseText, responseDetail.finalUrl);
}
});
}