Skip to content
This repository was archived by the owner on Jul 2, 2020. It is now read-only.

Commit 546a300

Browse files
committed
add BUPT-portal using condition
1 parent 0d27e43 commit 546a300

File tree

6 files changed

+177
-42
lines changed

6 files changed

+177
-42
lines changed

css/popup.css

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ h1 {
112112
text-align: center;
113113
}
114114

115-
.error-template {padding: 40px 15px;text-align: center;}
116-
.error-actions {margin-top:15px;margin-bottom:15px;}
117-
.error-actions .btn { margin-right:10px; }
115+
.error-template {
116+
padding: 40px 15px;
117+
text-align: center;
118+
}
119+
120+
.error-template button{
121+
margin: 20px auto 10px auto;
122+
}

html/popup.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h1>北邮校园网登录</h1>
3535
<div id="opt" class="col-xs-8 col-xs-offset-2">
3636
<div class="form-inline">
3737
<div class="btn-group">
38-
<button type="button" id="btn-login" class="btn btn-primary ladda-button" data-style="zoom-out">登录</button>
38+
<button type="button" id="btn-login" class="btn btn-primary" data-style="zoom-out">登录</button>
3939
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
4040
<span class="caret"></span>
4141
</button>
@@ -290,9 +290,16 @@ <h3 class="panel-title">Navigation</h3>
290290
<h1>Oops!</h1>
291291
<h2>404 Not Found</h2>
292292
<hr>
293-
<div class="error-details">
293+
<div id="out-error">
294294
您当前尚未接入北邮校园网环境
295295
</div>
296+
<div id="in-error">
297+
您当前接入的为BUPT-portal,并且未登录<br> 点击下面的按钮使用首选账户接入校园网并登陆
298+
<button type="button" id="btn-inlogin" class="btn btn-primary">接入校园网并登陆</button>
299+
</div>
300+
<div class="alert alert-danger" style="display:none" id="inlogin-fail">
301+
首选账号为空,请自行接入校园网
302+
</div>
296303
</div>
297304
</div>
298305
<!-- end of 404 -->

js/background.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ bkpage.isOverDay = false;
2020

2121
bkpage.setting = {};
2222

23+
/**
24+
* 创建并弹出一个notifications
25+
* @param str 显示字符串的内容
26+
*/
2327
bkpage.MakeNotice = function (str) {
2428
chrome.notifications.create({
2529
"iconUrl": buptbase.paths.icon_on,
@@ -53,6 +57,49 @@ bkpage.subInt = function(mstr, start, end){
5357
return parseInt(mstr);
5458
}
5559

60+
/**
61+
* 获取首选账号,与popup.js重复
62+
*/
63+
bkpage.GetSuperUser = function(){
64+
var super_user = localStorage.getItem('su');
65+
var result = {};
66+
67+
if (super_user !== null) {
68+
super_user = JSON.parse(super_user);
69+
var key;
70+
for (key in super_user)
71+
result.username = key;
72+
result.password = super_user[key];
73+
}
74+
75+
return result;
76+
}
77+
78+
/**
79+
* 尝试登录接入校园网(BUPT=poral)
80+
*/
81+
bkpage.TryPortal = function(){
82+
var su = bkpage.GetSuperUser();
83+
if (su.username){
84+
$.ajax({
85+
type: "POST",
86+
dataType: "html",
87+
url: buptbase.urls.portal_serverin,
88+
data: {'user':su.username,'pass':su.password},
89+
success : function (result) {
90+
buptbase.log("bk: in portal");
91+
// 再一次尝试自动登录
92+
bkpage.GetNetStatus(true, undefined);
93+
},
94+
error : function (data) {
95+
buptbase.log("bk: out portal");
96+
}
97+
});
98+
} else {
99+
buptbase.log("bk: out portal");
100+
}
101+
}
102+
56103
/**
57104
* buptnet.CheckNetStatus简化版,在打开浏览器的时候检查网络
58105
* @param isLoad 未登录时是否根据设置自动登录
@@ -66,9 +113,7 @@ bkpage.GetNetStatus = function (isLoad, hfunc){
66113
success : function (result, status) {
67114
// 获取页面标题判断账户状态
68115
var retstr = result;
69-
recc = retstr
70-
result = $(result);
71-
var title = result.filter('title').get(0).innerText;
116+
var title = buptbase.GetInner(retstr, '<title>');
72117
if (title == "上网注销窗"){
73118
chrome.browserAction.setIcon({path: buptbase.paths.icon_on});
74119
bkpage.state = 1;
@@ -85,6 +130,14 @@ bkpage.GetNetStatus = function (isLoad, hfunc){
85130
bkpage.Login();
86131
}
87132
}
133+
} else if (title == "北京邮电大学无线网准入认证\n") {
134+
// portal时候的重定向
135+
chrome.browserAction.setIcon({path: buptbase.paths.icon_off});
136+
bkpage.state = 0;
137+
if(noTry == undefined && true == bkpage.setting['auto']){
138+
bkpage.TryPortal();
139+
buptbase.log("try")
140+
}
88141
}
89142
// 在登录的情况下定时回调 hfunc
90143
if (hfunc != undefined && bkpage.state == 1){
@@ -101,27 +154,18 @@ bkpage.GetNetStatus = function (isLoad, hfunc){
101154
* 登录首选账号
102155
*/
103156
bkpage.Login = function (){
104-
var info = {};
157+
var info = bkpage.GetSuperUser();
158+
159+
buptbase.log(info.username);
105160

106-
var su = JSON.parse(localStorage.getItem('su'));
107-
if (su != null){
108-
for (var key in su){
109-
info.username = key;
110-
info.passwd = su[key];
111-
buptbase.log(key);
112-
}
113-
}
114161
$.ajax({
115162
type: "POST",
116163
dataType: "html",
117164
url: buptbase.urls.server + buptbase.urls.login,
118165
//0MKKey也得提交
119-
data: {'DDDDD':info.username,'upass':info.passwd, 'savePWD':'0','0MKKey':''},
166+
data: {'DDDDD':info.username,'upass':info.password, 'savePWD':'0','0MKKey':''},
120167
success : function (result) {
121-
122-
result = $(result);
123-
var title = result.filter('title').get(0).innerText;
124-
168+
var title = buptbase.GetInner(result, '<title>');
125169
if (title == "登录成功窗"){
126170
bkpage.MakeNotice('自动登录成功\n登录账号:'+info.username);
127171
localStorage.setItem('cuser', info.username);

js/base.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ buptbase.urls.logsuccess = '/3.htm'
3030
buptbase.urls.logoff = '/F.htm'
3131
//外网ip查询api
3232
buptbase.urls.testip = 'http://pv.sohu.com/cityjson?ie=utf-8'
33+
//接入BUPT-portal时候的校园网网关,登录后即接入校园网
34+
//但还需经过buptbase.urls.server才能访问外网
35+
buptbase.urls.portal_server = 'http://10.3.8.214'
36+
buptbase.urls.portal_serverin = 'http://10.3.8.214/login'
3337

3438
// debug console输出开关
3539
buptbase.debug = true;
@@ -109,7 +113,10 @@ buptbase.SaveData = function(data, filename){
109113
return br;
110114
}
111115

112-
// 计算十进制下的显示版本
116+
/**
117+
* 计算十进制下的显示版本
118+
* @param flow 原始数据十进制下的存储版本
119+
*/
113120
buptbase.ConvertFlow = function(flow){
114121
if (flow != null){
115122
var flow0, flow1, flow3;
@@ -160,3 +167,23 @@ buptbase.getMyTime = function() {
160167
}
161168
return t;
162169
}
170+
171+
/**
172+
* 处理截取返回字符串str中start和end中的内容,如果为标签则传入start即可
173+
* @param str 需要处理的字符串
174+
* @param start 开头字符串,可以与end重复
175+
* @param end 结束字符串,未传参默认当做<>补充/
176+
* @return 截取的中间字符串
177+
*/
178+
buptbase.GetInner = function(str, start, end){
179+
var pos = 0;
180+
if (end == undefined){
181+
var end = start.replace('<', '</');
182+
}
183+
if (end == start){
184+
pos = str.indexOf(start);
185+
return str.substring(pos, str.indexOf(end, pos + 1)).replace(start,'');
186+
}else{
187+
return str.substring(str.indexOf(start), str.indexOf(end)).replace(start,'');
188+
}
189+
}

js/popup.js

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ String.prototype.replaceAll = function(s1,s2){
1111
  return this.replace(new RegExp(s1,"gm"),s2);
1212
};
1313

14-
15-
// 生成默认用户数据
16-
function GenDeUserdata() {
17-
var userdata = {};
18-
userdata.username = "";
19-
userdata.passwd = "";
20-
userdata.flow = [];
21-
return userdata;
22-
}
23-
2414
buptnet = {};
2515

2616
// 0:未登录,1:已登录
@@ -43,7 +33,7 @@ buptnet.env = {};
4333
buptnet.Script2Json = function (scriptStr){
4434
var retStr = [];
4535

46-
scriptStr= scriptStr.replaceAll(';','\n').replaceAll('\'','\"');
36+
scriptStr = scriptStr.replaceAll(';','\n').replaceAll('\'','\"');
4737
scriptStr = scriptStr.match(/.+/g);
4838
scriptStr.forEach(function(element) {
4939
retStr.push(element.replace(/([\w_]+)=(.*)/,"\"$1\":$2"));
@@ -53,17 +43,19 @@ buptnet.Script2Json = function (scriptStr){
5343
return JSON.parse(retStr);
5444
}
5545

56-
57-
58-
// 充值的流量,返回单位 MB
46+
/**
47+
* 充值的流量,返回单位 MB
48+
*/
5949
buptnet.GetExFlow = function (){
6050
var exFlow = buptnet.state_data['fee'];
6151
// exFlow/10000=元,一元一GB
6252
exFlow = parseInt(exFlow/10000*1024*1024);
6353
return buptbase.ConvertFlow(exFlow);
6454
}
6555

66-
// 已用流量,返回单位 MB
56+
/**
57+
* 已用流量,返回单位 MB
58+
*/
6759
buptnet.GetFlow = function (){
6860
var flow = buptnet.state_data['flow'];
6961
return buptbase.ConvertFlow(flow);
@@ -80,9 +72,10 @@ buptnet.btnwait.start = function(){
8072
buptnet.btnwait.btn.text('ing...');
8173
buptnet.btnwait.btn.attr('disabled','disabled');
8274
}
83-
// buptnet.btnwait = Ladda.create(document.querySelector( '#btn-login'));
8475

85-
// 无动画的切换登录和登陆后的页面
76+
/**
77+
* 无动画的切换登录和登陆后的页面
78+
*/
8679
buptnet.ChangePage = function(){
8780
if (buptnet.state_last != buptnet.state){
8881

@@ -99,7 +92,10 @@ buptnet.ChangePage = function(){
9992
buptbase.log('change');
10093
}
10194

102-
// 获取一般情况下页面内嵌入的JavaScript变量
95+
/**
96+
* 获取一般情况下页面内嵌入的JavaScript变量
97+
* @param jqObj 返回的页面的化后的jQuery对象
98+
*/
10399
buptnet.GetScriptData = function(jqObj){
104100
// 获取页面内script参数
105101
var data = jqObj.filter('script').get(0).innerText;
@@ -108,10 +104,63 @@ buptnet.GetScriptData = function(jqObj){
108104
return buptnet.Script2Json(data);
109105
}
110106

107+
/**
108+
* 检查是否为BUPT-portal环境
109+
*/
110+
buptnet.BUPT_portal_Check = function(){
111+
$.ajax({
112+
type: "GET",
113+
dataType: "html",
114+
url: buptbase.urls.portal_server,
115+
async : false,
116+
success : function (result) {
117+
buptbase.log("in portal")
118+
$('#out-error').hide();
119+
$('#in-error').show();
120+
},
121+
error : function (data) {
122+
buptbase.log("out portal")
123+
$('#in-error').hide();
124+
$('#out-error').show();
125+
}
126+
});
127+
128+
}
129+
130+
/**
131+
* BUPT-portal环境且未接入校园网时候自动登录首选账号
132+
*/
133+
buptnet.BUPT_portal_login = function(){
134+
var su = buptnet.GetSuperUser();
135+
if (su.username){
136+
$.ajax({
137+
type: "POST",
138+
dataType: "html",
139+
url: buptbase.urls.portal_serverin,
140+
data: {'user':su.username,'pass':su.password},
141+
success : function (result) {
142+
buptbase.log("in portal");
143+
// 装载首选用户并登陆
144+
buptnet.LoadLoginInfo();
145+
buptnet.Login();
146+
// 切换面板
147+
$('#error').hide();
148+
$('#fun-panel').show();
149+
},
150+
error : function (data) {
151+
buptbase.log("out portal");
152+
}
153+
});
154+
} else {
155+
$('#inlogin-fail').show();
156+
}
157+
}
158+
111159
// 连接不上
112160
buptnet.Ajaxfaild = function(){
113161
$('#login-panel').hide();
114162
$('#fun-panel').hide();
163+
buptnet.BUPT_portal_Check();
115164
$('#error').fadeIn();
116165
}
117166

@@ -472,7 +521,7 @@ buptnet.LoadUserList = function(){
472521
/**
473522
* 绑定一些回调函数
474523
*/
475-
buptnet.BindButton = function (params) {
524+
buptnet.BindButton = function () {
476525
$('#btn-login').click(buptnet.Login);
477526
$('#btn-logoff').click(buptnet.Logoff);
478527

@@ -514,6 +563,8 @@ buptnet.BindButton = function (params) {
514563
});
515564
// 绑定,模态对话框
516565
$('#myModal').on('shown.bs.modal', function () {});
566+
//绑定portal下的一键登录
567+
$('#btn-inlogin').click(buptnet.BUPT_portal_login);
517568
}
518569

519570
/**

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"permissions":[
88
"tabs",
99
"http://10.3.8.211/",
10+
"http://10.3.8.214/",
1011
"http://pv.sohu.com/",
1112
"storage",
1213
"notifications",

0 commit comments

Comments
 (0)