Commit c99ed893 authored by antoor's avatar antoor

Add the view-site module

增加浏览网站模块
parent 8099cc6a
## 浏览网站模块
> 用于浏览目标站点,从而进行一些登陆操作(自动设置cookie等登陆信息
## 实现思路
1. 打开一个浏览窗口,访问站点域名
2. 创建一个tabbar,用于管理cookie
3. 关闭浏览窗口,tabbar会随时刷新cookie的改动,然后显示到UI中
4. 保存cookie,关闭tabbar(将会把Cookie内容设置为请求的Header.Cookie
const CM = {
/**
* Cookie操作对象
* @type {object}
*/
cookies: antSword.remote.session.defaultSession.cookies,
/**
* 获取Cookie
* @param {Object} opt = {} 查询条件{url, name, domain, path, secure, session}
* @docLink http://electron.atom.io/docs/api/session/#cookiesgetfilter-callback
* @return {Promise} [description]
*/
get: (opt = {}) => {
return new Promise((res, rej) => {
CM.cookies.get(opt, (err, _cookies) => {
if (err) { return rej(err) }
return res(_cookies);
// let _cs = [];
// _cookies.map((_) => {
// _cs.push(
// _['name'] + '=' + _['value']
// )
// });
// return res(_cs.join('; '));
})
})
},
/**
* 获取Cookie字符串
* @param {object} opt = {}
* @return {[type]} [description]
*/
getStr: (opt = {}) => {
return new Promise((res, rej) => {
CM.cookies.get(opt, (err, _cookies) => {
if (err) { return rej(err) }
let _cs = [];
_cookies.map((_) => {
_cs.push(
_['name'] + '=' + _['value']
)
});
return res(_cs.join('; '));
})
})
}
}
module.exports = CM;
/**
* 网站浏览模块
* 开写:2016/07/01
*/
const CookieMgr = require('./cookiemgr');
class ViewSite {
constructor(opts) {
const hash = String(Math.random()).substr(2, 10);
// 初始化UI::tabbar
const tabbar = antSword['tabbar'];
tabbar.addTab(
`tab_viewsite_${hash}`,
`<i class="fa fa-chrome"></i> ${opts['ip']}`,
null, null, true, true
);
tabbar.attachEvent('onTabClick', (id,lid) => {
if (id !== `tab_viewsite_${hash}`) { return };
});
this.opts = opts;
this.cell = tabbar.cells(`tab_viewsite_${hash}`);
// 初始化工具栏
this.toolbar = this._initToolbar();
this.grid = this._initGrid();
// 定时刷新Cookie
this._refreshCookie();
const inter = setInterval(() => {
if (this.grid.clearAll instanceof Function) {
this._refreshCookie();
} else {
clearInterval(inter);
}
}, 1000);
// 打开浏览窗口
this._loadURL(opts.url);
}
/**
* 初始化工具栏
* @return {[type]} [description]
*/
_initToolbar() {
const toolbar = this.cell.attachToolbar();
toolbar.loadStruct([
{ id: 'save', type: 'button', icon: 'save', text: '保存' },
{ type: 'separator' },
]);
toolbar.attachEvent('onClick', (id) => {
switch(id) {
case 'save':
this._saveCookie();
break;
}
})
return toolbar;
}
/**
* 初始化grid
* @return {[type]} [description]
*/
_initGrid() {
const grid = this.cell.attachGrid();
// 设置grid头
grid.setHeader('Name,Value,Domain,Path,Expires / Max-Age,Size,HTTP,Secure');
grid.setColTypes("ro,ro,ro,ro,ro,ro,ro,ro");
grid.setColSorting('str,str,str,str,str,str,str,str');
grid.setInitWidths("120,*,120,50,150,50,50,60");
grid.setColAlign("left,left,left,left,left,right,center,left");
grid.enableMultiselect(true);
grid.init();
return grid;
}
/**
* 刷新Cookie
* @return {[type]} [description]
*/
_refreshCookie() {
CookieMgr.get({
url: this.opts['url']
}).then((cookie) => {
let data = [];
cookie.map((c, i) => {
data.push({
id: i + 1,
data: [
c.name, c.value, c.domain,
c.path, new Date(c.expirationDate).toUTCString(),
c.name.length + c.value.length, c.httpOnly, c.secure
]
});
});
// 刷新UI
this.grid.clearAll();
this.grid.parse({
'rows': data
}, 'json');
})
}
/**
* 保存Cookie到配置
* @return {[type]} [description]
*/
_saveCookie() {
CookieMgr.getStr({
url: this.opts.url
}).then((cookie) => {
// 1. 获取旧数据
const oldHttpConf = (antSword.ipcRenderer.sendSync('shell-findOne', this.opts._id).httpConf || {});
// 2. 添加新数据(cookie)
const httpConf = Object.assign({}, oldHttpConf, {
headers: Object.assign({}, oldHttpConf.headers || {}, {
Cookie: cookie
})
})
// 3. 更新数据
const ret = antSword.ipcRenderer.sendSync('shell-updateHttpConf', {
_id: this.opts._id,
conf: httpConf
});
if (ret === 1) {
toastr.success('保存Cookie成功!', '成功');
} else {
toastr.error('保存Cookie失败!' + ret, '失败');
}
})
}
/**
* 初始化浏览窗口
* @param {[type]} url [description]
* @return {[type]} [description]
*/
_loadURL(url) {
let win = new antSword['remote'].BrowserWindow({
width: 930,
height: 666,
minWidth: 888,
minHeight: 555,
show: false,
webPreferences: {
nodeIntegration: false,
},
title: 'AntSword.Store'
});
win.on('close', () => {
win = null;
});
win.loadURL(url);
win.show();
win.openDevTools();
}
}
module.exports = ViewSite;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment