-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryIPattribution.js
More file actions
114 lines (107 loc) · 4.14 KB
/
QueryIPattribution.js
File metadata and controls
114 lines (107 loc) · 4.14 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ==UserScript==
// @name CAPUBBS QueryIPattribution
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 增加了一个可以免费查询的api,ipv4和ipv6都能查。但ipv4下校园网识别不佳。
// @author FFFomalhaut
// @match https://*.chexie.net/bbs/online/*
// @icon https://chexie.net/assets/images/capu.jpg
// @grant none
// @require https://chexie.net/assets/js/jquery.min.js
// @require file:///C:/Users/admin/Desktop/PlayWithBBS/QueryIPattribution/data-utf8.js
// ==/UserScript==
(function() {
'use strict';
/* global localData, $ */
function compare(ip1, ip2) {
var list1 = ip1.split(".");
var list2 = ip2.split(".");
var oct1,oct2,i=0;
while ((oct1=parseInt(list1[i])) && (oct2=parseInt(list2[i])) && ++i) {
if (oct1 < oct2) {
return -1;
}
if (oct1 > oct2) {
return 1;
}
}
return 0;
}
function locateAndFill_api(td_ip) {
$.get("https://api.mir6.com/api/ip",{
"ip": td_ip.innerHTML,
"type": "json",
}, "json").done(function (data) {
var simp_location = data.data.location;
if ((data.data.country == "中国") && (data.data.province != "")) {
simp_location = data.data.location.match(/(?<=\s).*/);
}
var attribution = simp_location +"<br>"+ data.data.isp +"<br>"+ data.data.net;
$(td_ip).next().html(attribution);
}).fail(function(errorThrown) {
$(td_ip).next().html(errorThrown);
})
}
function locateAndFill_local(td_ip) {
var l=0, r=localData.length;
var ip = td_ip.innerHTML;
while (l<r) {
var mid = (l+r)/2 |0
if (compare(ip,localData[mid][0]) == -1) {
r=mid;
}
else if (compare(ip,localData[mid][1]) == 1) {
l=mid+1;
}
else {
var attribution = localData[mid][2].replace(" ","<br>");
$(td_ip).next().html(attribution);
return;
}
}
}
if ($("tr:first").children().length == 6) {
$("colgroup > :eq(2)").after($("<col width='200'>"));
$("tr:first > :eq(2)").after($("<th>IP归属地</th>"));
$("tbody").children().not(":first").each(function(i,tr) {
var td_ip = tr.childNodes[2];
$(td_ip).after($("<td></td>"));
locateAndFill_api(td_ip);
})
$("tbody").append($("<tr><td id='v4' colspan='6'>IPv4归属地数据来源:<a href='https://api.mir6.com' target='_blank'>api.mir6.com</a></td></tr>"));
$("tbody").append($("<tr><td id='v6' colspan='6'>IPv6归属地数据来源:<a href='https://api.mir6.com' target='_blank'>api.mir6.com</a></td></tr>"));
$("table").after($(
`<div>
选择IPv4数据来源
<select id='select' onchange=reload()>
<option value='cz88'>cz88.net</option>
<option value='mir' selected>api.mir6.com</option>
</select>
</div>`
));
}
window.reload = function() {
var href;
if ($("#select").prop("selectedIndex") == 0) {
$("tbody").children().slice(1,-2).each(function(i,tr) {
var td_ip = tr.childNodes[2];
if (td_ip.innerHTML.includes(":")) {
return;
}
locateAndFill_local(td_ip);
});
href = $("#select > option").eq(0).html();
} else if ($("#select").prop("selectedIndex") == 1) {
$("tbody").children().slice(1,-2).each(function(i,tr) {
var td_ip = tr.childNodes[2];
if (td_ip.innerHTML.includes(":")) {
return;
}
locateAndFill_api(td_ip);
});
href = $("#select > option").eq(1).html();
}
$("#v4").html("IPv4归属地数据来源:<a href='https://"+href+"' target='_blank'>"+href+"</a>");
}
// Your code here...
})();