1+ /*--------------------------------------------------------------
2+ >>> BACKGROUND
3+ ----------------------------------------------------------------
4+ # On installed
5+ # On updated
6+ --------------------------------------------------------------*/
7+
8+ /*--------------------------------------------------------------
9+ # ONINSTALLED
10+ --------------------------------------------------------------*/
11+
12+ var extension_id = chrome . runtime . id ,
13+ storage = { } ,
14+ for_search = [ ] ,
15+ all = {
16+ 0 : [ ] ,
17+ 1 : [ ] ,
18+ 2 : [ ]
19+ } ,
20+ qqqq = { } ,
21+ pre_all = {
22+ 0 : { } ,
23+ 1 : { } ,
24+ 2 : { }
25+ } ,
26+ top2 = {
27+ 0 : [ ] ,
28+ 1 : [ ] ,
29+ 2 : [ ] ,
30+ l0 : 0 ,
31+ l1 : 0 ,
32+ l2 : 0
33+ } ,
34+ PINNED_TABS = { } ,
35+ SEARCH_REGEX = new RegExp ( '[?&](' + SEARCH_PARAMS . join ( '|' ) + ')=([^&]+)' ) ;
36+
37+ function sort ( array , index , order_by ) {
38+ if ( order_by === 'asc' ) {
39+ if ( typeof array [ 0 ] [ index ] === 'number' ) {
40+ sorted = array . sort ( function ( a , b ) {
41+ return a [ index ] - b [ index ] ;
42+ } ) ;
43+ } else {
44+ sorted = array . sort ( function ( a , b ) {
45+ return a [ index ] . localeCompare ( b [ index ] ) ;
46+ } ) ;
47+ }
48+ } else {
49+ if ( typeof array [ 0 ] [ index ] === 'number' ) {
50+ sorted = array . sort ( function ( a , b ) {
51+ return b [ index ] - a [ index ] ;
52+ } ) ;
53+ } else {
54+ sorted = array . sort ( function ( a , b ) {
55+ return b [ index ] . localeCompare ( a [ index ] ) ;
56+ } ) ;
57+ }
58+ }
59+
60+ return array ;
61+ }
62+
63+ function historyCache ( start_time , end_time , step ) {
64+ chrome . history . search ( {
65+ endTime : end_time ,
66+ maxResults : 0 ,
67+ startTime : start_time ,
68+ text : ''
69+ } , function ( items ) {
70+ for ( var i = 0 , l = items . length ; i < l ; i ++ ) {
71+ var item = items [ i ] ;
72+
73+ var match = item . url . match ( / \/ [ ^ / ? # ] + / g) ,
74+ without_proto = decodeURIComponent ( item . url . match ( / \/ \/ ( .* ) / ) [ 1 ] ) ;
75+
76+ for_search . push ( [ without_proto , item . typedCount ] ) ;
77+
78+ if ( match ) {
79+ var current = storage ;
80+
81+ all [ 1 ] . push ( [ item . visitCount , item . title , without_proto , 0 , '' ] ) ;
82+
83+ var q = without_proto . match ( SEARCH_REGEX ) ;
84+
85+ if ( q ) {
86+ q = decodeURIComponent ( q [ 2 ] ) ;
87+
88+ if ( ! pre_all [ 2 ] [ match [ 0 ] ] ) {
89+ pre_all [ 2 ] [ match [ 0 ] ] = 0 ;
90+ }
91+
92+ pre_all [ 2 ] [ match [ 0 ] ] += item . visitCount ;
93+
94+ if ( ! qqqq [ 'q' + match [ 0 ] ] ) {
95+ qqqq [ 'q' + match [ 0 ] ] = { } ;
96+ }
97+
98+ if ( ! qqqq [ 'q' + match [ 0 ] ] [ q ] ) {
99+ qqqq [ 'q' + match [ 0 ] ] [ q ] = {
100+ url : without_proto ,
101+ visitCount : item . visitCount
102+ } ;
103+ }
104+ }
105+
106+ for ( var j = 0 , k = match . length ; j < k ; j ++ ) {
107+ var string = decodeURIComponent ( match [ j ] ) ;
108+
109+ if ( ! current [ string ] ) {
110+ current [ string ] = {
111+ d : 0
112+ } ;
113+ }
114+
115+ if ( j === 0 ) {
116+ current [ string ] . d += item . visitCount ;
117+ }
118+
119+ current = current [ string ] ;
120+
121+ if ( j + 1 === k ) {
122+ current . a = item . lastVisitTime ;
123+ current . b = item . title ;
124+ current . c = item . typedCount ;
125+ current . d = item . visitCount ;
126+ }
127+ }
128+ }
129+ }
130+
131+ if ( start_time > 0 ) {
132+ end_time -= step ;
133+ start_time -= step ;
134+
135+ if ( start_time < 0 ) {
136+ start_time = 0 ;
137+ }
138+
139+ console . log ( 'Caching: ' + ( 100 - start_time / ( global_time / 100 ) ) . toFixed ( 2 ) + '%' ) ;
140+
141+ chrome . tabs . query ( { active : true , currentWindow : true } , function ( tabs ) {
142+ for ( var i = 0 , l = tabs . length ; i < l ; i ++ ) {
143+ if ( tabs [ i ] . url . indexOf ( extension_id ) !== - 1 ) {
144+ chrome . tabs . sendMessage ( tabs [ i ] . id , { progress : ( 100 - start_time / ( global_time / 100 ) ) . toFixed ( 2 ) } ) ;
145+ }
146+ }
147+ } ) ;
148+
149+ setTimeout ( function ( ) {
150+ historyCache ( start_time , end_time , step ) ;
151+ } , 200 ) ;
152+ } else {
153+ for ( var key in storage ) {
154+ var item = storage [ key ] ;
155+
156+ all [ 0 ] . push ( [ key . substr ( 1 ) , item . d ] ) ;
157+ }
158+
159+ for ( var key in pre_all [ 2 ] ) {
160+ var item = pre_all [ 2 ] [ key ] ;
161+
162+ all [ 2 ] . push ( [ item , key . substr ( 1 ) ] ) ;
163+ }
164+
165+ top2 [ 0 ] = sort ( all [ 0 ] , 1 ) . slice ( 0 , 100 ) ;
166+ top2 [ 1 ] = sort ( all [ 1 ] , 0 ) . slice ( 0 , 100 ) ;
167+ top2 [ 2 ] = sort ( all [ 2 ] , 0 ) . slice ( 0 , 100 ) ;
168+ for_search = sort ( for_search , 1 ) ;
169+
170+ top2 . l0 = all [ 0 ] . length ;
171+ top2 . l1 = all [ 1 ] . length ;
172+ top2 . l2 = all [ 2 ] . length ;
173+
174+ chrome . storage . local . set ( {
175+ all : all ,
176+ top : top2 ,
177+ for_search : for_search ,
178+ cached : true
179+ } , function ( ) {
180+ chrome . storage . local . set ( storage , function ( ) {
181+ chrome . storage . local . set ( qqqq , function ( ) {
182+ chrome . tabs . query ( { active : true , currentWindow : true } , function ( tabs ) {
183+ for ( var i = 0 , l = tabs . length ; i < l ; i ++ ) {
184+ if ( tabs [ i ] . url . indexOf ( extension_id ) !== - 1 ) {
185+ chrome . tabs . sendMessage ( tabs [ i ] . id , { progress : 'loaded' } ) ;
186+ }
187+ }
188+ } ) ;
189+
190+ console . log ( 'Caching completed successfully' ) ;
191+
192+ console . log ( 'Data size: ~' + ( JSON . stringify ( {
193+ all,
194+ top2,
195+ storage,
196+ qqqq
197+ } ) . length * 2 / 1000 / 1000 ) . toFixed ( 2 ) + ' MB' ) ;
198+ } ) ;
199+ } ) ;
200+ } ) ;
201+ }
202+ } ) ;
203+ }
204+
205+ chrome . runtime . onInstalled . addListener ( function ( ) {
206+ chrome . storage . local . get ( 'cached' , function ( items ) {
207+ if ( items . cached !== true ) {
208+ var time = new Date ( ) . getTime ( ) ,
209+ step = 7776000000 ;
210+
211+ global_time = time ;
212+
213+ historyCache ( time - step , time , step ) ;
214+ }
215+ } ) ;
216+ } ) ;
217+
218+ chrome . tabs . query ( { } , function ( tabs ) {
219+ for ( var i = 0 , l = tabs . length ; i < l ; i ++ ) {
220+ var tab = tabs [ i ] ;
221+
222+ if ( tab . pinned === true ) {
223+ PINNED_TABS [ tab . id ] = tab ;
224+ }
225+ }
226+ } ) ;
227+
228+ chrome . tabs . onUpdated . addListener ( function ( tabId , changeInfo , tab ) {
229+ if ( tab . pinned === true ) {
230+ PINNED_TABS [ tab . id ] = tab ;
231+ }
232+ } ) ;
233+
234+ chrome . tabs . onRemoved . addListener ( function ( tabId ) {
235+ if ( PINNED_TABS [ tabId ] ) {
236+ chrome . storage . local . get ( 'recently_closed' , function ( items ) {
237+ var recently_closed = items . recently_closed || [ ] ,
238+ tab = PINNED_TABS [ tabId ] ;
239+
240+ recently_closed . push ( [ new Date ( ) . toString ( ) , tab . url , tab . title ] ) ;
241+
242+ chrome . storage . local . set ( {
243+ recently_closed : recently_closed . slice ( 0 , 20 )
244+ } , function ( ) {
245+ delete PINNED_TABS [ tabId ] ;
246+ } ) ;
247+ } ) ;
248+ }
249+ } ) ;
250+
251+ /*--------------------------------------------------------------
252+ # ONUPDATED
253+ --------------------------------------------------------------*/
0 commit comments