-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.js
144 lines (124 loc) · 3.29 KB
/
option.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var MAX = 20;
/**
* 获取element
*
* @param {string} id
* @return {HTMLElement}
*/
function g( id ) {
return document.getElementById( id );
}
/**
* RecordCount选择发生变化的event handler
*
* @param {HTMLEvent} e
*/
function recordCountOnchange( e ) {
localStorage.setItem( KEY_RECORDCOUNT, g( "RecordCount" ).value );
}
/**
* 绘制列表视图
*/
function renderList() {
var i = 0;
var len = 0;
var html = [ '<dl>' ];
var count = parseInt( localStorage.getItem( KEY_RECORDCOUNT ), 10 );
var tabs;
var titles;
var j;
var jLen;
for ( ; i < count; i++ ) {
tabs = localStorage.getItem( KEY_PREFIX + i );
titles = localStorage.getItem( KEY_TPREFIX + i );
if ( !tabs ) {
break;
}
tabs = JSON.parse( tabs );
titles = JSON.parse( titles );
html.push( '<dt data-index="' + i
+ '">[<i>恢复Tab序列</i>][<del>删除</del>] 保存的第'
+ ( i + 1 ) + '个序列</dt>' );
for ( j = 0, jLen = tabs.length; j < jLen; j++ ) {
html.push( '<dd><a target="_blank" href="' + tabs[ j ] + '">' + titles[ j ] + '</a></dd>' );
}
}
html.push( '</dl>' );
g( "ResultList" ).innerHTML = html.join( '' );
}
/**
* 结果列表点击事件处理函数
*/
function listClickHandler( e ) {
e = e || window.event;
var tar = e.srcElement || e.target;
var index = tar.parentNode.getAttribute( 'data-index' );
switch ( tar.tagName ) {
case "DEL":
deleteTabsByIndex( index );
break;
case "I":
restoreTabsByIndex( index );
break;
}
}
/**
* 删除tabs记录
*
* @param {number} index
*/
function deleteTabsByIndex( index ) {
var recordCount = parseInt( localStorage.getItem( KEY_RECORDCOUNT ), 10 );
var tabs;
var titles;
localStorage.removeItem( KEY_PREFIX + index );
localStorage.removeItem( KEY_TPREFIX + index );
index++;
for ( ; index < recordCount; index++ ) {
tabs = localStorage.getItem( KEY_PREFIX + index );
titles = localStorage.getItem( KEY_TPREFIX + index );
if ( !tabs ) {
break;
}
localStorage.setItem( KEY_PREFIX + ( index - 1 ), tabs );
localStorage.setItem( KEY_TPREFIX + ( index - 1 ), titles );
localStorage.removeItem( KEY_PREFIX + index );
localStorage.removeItem( KEY_TPREFIX + index );
}
renderList();
}
/**
* 恢复tabs
*
* @param {number} index
*/
function restoreTabsByIndex( index ) {
var tabs = localStorage.getItem( KEY_PREFIX + index );
var len;
var i = 0;
if ( !tabs ) {
return;
}
tabs = JSON.parse( tabs );
for ( len = tabs.length; i < len; i++ ) {
chrome.tabs.create( { url: tabs[ i ] } );
}
}
// 初始化onload后页面行为
(function () {
var rcEl = g( "RecordCount" );
var i = 1;
var op;
for ( ; i <= MAX; i++ ) {
op = document.createElement( 'option' );
op.value = i;
op.innerText = i;
rcEl.appendChild( op );
}
rcEl.value = localStorage.getItem( KEY_RECORDCOUNT );
rcEl.onchange = recordCountOnchange;
op = null;
rcEl = null;
renderList();
g( "ResultList" ).onclick = listClickHandler;
})();