-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
170 lines (133 loc) · 4.83 KB
/
popup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file Chrome Browser Action Popup UI-Controller
* @author https://github.com/andre-st
*
*/
/*
* Note:
* As opposed to background.js the popup scripts are short-lived:
* Popups are webpages and the webpage is reloaded(!) every time
* the user clicks on the popup button.
*
* Relevant Chrome API:
* - https://developer.chrome.com/extensions/i18n
*
*/
"use strict";
// ------------------------- Helper functions ---------------------------
//
// Note to myself: if proliferates, switch to jQuery bloat.
//
function getTabMan()
{
return chrome.extension.getBackgroundPage().tabman;
}
function elem( id )
{
return document.getElementById( id );
}
function bind( ids, ev, cb )
{
const arr = Array.isArray( ids ) ? ids : [ids];
arr.forEach( id => elem( id ).addEventListener( ev, cb ) );
}
function i18n( msgId )
{
return chrome.i18n.getMessage( msgId );
}
function i18nAll( attr, cb )
{
const list = document.querySelectorAll( '[' + attr + ']' );
for( var i = 0; i < list.length; i++ )
cb( list[i], i18n( list[i].getAttribute( attr ) ) );
}
// ----------------------------------------------------------------------
function displayStats( tabCount, winCount )
{
const msg = tabCount + ( tabCount != 1 ? ' Tabs in ' : ' Tab in ' )
+ winCount + ( winCount != 1 ? ' Windows' : ' Window' );
elem( 'stats' ).innerHTML = msg;
elem( 'btnMergeNext' ).disabled = (winCount < 2);
}
function displaySplitSessionState( statename )
{
elem( 'splitControls' ).className = statename;
}
function displayMergeSessionState( statename )
{
elem( 'mergeControls' ).className = statename;
const btn = elem( 'btnMergeNext' );
const capt = i18n( statename == 'awaiting' ? 'btnMergeNextAwait' : 'btnMergeNext' );
btn.innerHTML = '<span>' + capt + '</span>';
btn.blur();
}
function previewSearchResult( tabs, numMatchWin )
{
const list = elem( 'searchMatchList' );
while( list.hasChildNodes() )
list.removeChild( list.firstChild );
tabs.forEach( function( t )
{
const text = document.createTextNode( t.title );
const item = document.createElement ( 'LI' );
const icon = document.createElement ( 'IMG' );
if( t.favIconUrl
&& !t.favIconUrl.startsWith( 'chrome://theme/' ) ) // Denied: chrome://theme/IDR_EXTENSIONS_FAVICON@2x
icon.src = t.favIconUrl;
item.addEventListener( 'click', function( evt )
{
chrome.tabs.update( t.id, { active: true, selected: true });
evt.stopPropagation();
});
item.appendChild( icon );
item.appendChild( text );
list.appendChild( item );
});
displayStats( list.childElementCount, numMatchWin );
}
function changePopupState( statename )
{
document.body.className = statename;
if( statename == 'popupSearchState' )
getTabMan().drySearch( '', previewSearchResult );
}
document.addEventListener( 'DOMContentLoaded', function()
{
// There is no native i18n support for HTML at the time of development:
//
i18nAll( 'data-i18n', (e,m) => e.innerHTML = m );
i18nAll( 'data-i18n-title', (e,m) => e.title = m );
i18nAll( 'data-i18n-placeholder', (e,m) => e.placeholder = m );
// Mouse/Keyboard events:
//
bind( 'btnSort', 'click', getTabMan().sort );
bind( 'btnMoveTabsToBetterWins', 'click', getTabMan().moveTabsToBetterWins );
bind( 'btnSplitAtTab', 'click', getTabMan().splitAtTab );
bind( 'btnSplitHighlighted', 'click', getTabMan().splitHighlighted );
bind( 'btnSplitAtCenter', 'click', getTabMan().splitAtCenter );
bind( 'btnSuspend', 'click', getTabMan().suspendTabs );
bind( 'btnMergeNext', 'click', getTabMan().mergeNext );
bind( 'btnMergeCancel', 'click', getTabMan().cancelMerge );
bind( 'editSearch', 'keyup', function( ev )
{
getTabMan().drySearch( this.value, previewSearchResult );
if( ev.keyCode === 13 ) // "Enter"
getTabMan().search( this.value );
});
bind( 'editSearch', 'focus', () => changePopupState( 'popupSearchState' ) );
bind( ['multiWinControls'], 'mouseenter', () => elem( 'stats' ).className = 'highlighted' );
bind( ['multiWinControls'], 'mouseleave', () => elem( 'stats' ).className = '' );
// Indicators/widget states:
//
getTabMan().addMergeSessionListener( displayMergeSessionState );
getTabMan().addSplitSessionListener( displaySplitSessionState );
getTabMan().updateStats ( displayStats );
});
chrome.runtime.getBackgroundPage( bgWindow =>
{
//`Extension context invalidated` error is thrown if `window.closed` is true and call to
//`window.chrome.i18n` or other `window.chrome` api happens. Use bgWindow.chrome instead.
// Use winChrome for tab-related calls like winChrome.tabs.getCurrent.
window.winChrome = window.chrome;
window.chrome = bgWindow.chrome;
});