Mercurial > google_bookmarks_incsearch
annotate chrome/content/init.js @ 6:ac5648afee47 default tip
change hot key from ctrl to alt.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Sat, 23 May 2009 11:56:41 +0900 |
parents | c47ec96326ad |
children |
rev | line source |
---|---|
0 | 1 var DEFAULT_NUMBER_OF_RESULTS = 10; |
2 var DEFAULT_SIZE_OF_INPUT_AREA = 80; | |
3 var USER_EXT_JS = "user-extension.js" | |
4 var USER_EXT_JS_SAMPLE = "user-extension-sample.js" | |
5 var USER_CSS = "user.css" | |
6 var USER_CSS_SAMPLE = "user-sample.css" | |
7 | |
8 if (!createTableSql) { | |
9 var createTableSql = "CREATE TABLE bookmark(id INTEGER, url TEXT, title TEXT, info TEXT, tags TEXT, time TEXT, search_text TEXT, PRIMARY KEY(id))"; | |
10 } | |
11 | |
12 var database = new Database('bookmark', EXTENSION_NAME); | |
13 | |
14 var prefSvc = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService); | |
15 var prefBranch = prefSvc.getBranch('extensions.' + EXTENSION_NAME + '.'); | |
16 var incsearch = null; | |
17 | |
18 window.addEventListener('load', function(){ | |
19 var text = document.getElementById('text'); | |
20 var viewArea = document.getElementById('viewArea'); | |
21 var status = document.getElementById('status'); | |
22 var sync = document.getElementById('sync'); | |
23 var setting = document.getElementById('setting'); | |
24 var help = document.getElementById('help'); | |
25 var loadingBox = document.getElementById('loadingBox'); | |
26 var loadingImage = document.getElementById('loadingImage'); | |
27 var loadingMessage = document.getElementById('loadingMessage'); | |
28 var pageLinkTop = document.getElementById('pageLinkTop'); | |
29 var pageLinkBottom = document.getElementById('pageLinkBottom'); | |
30 | |
31 var userId = null; | |
32 try { | |
33 userId = prefBranch.getCharPref("userId"); | |
34 } catch(e){ | |
35 } | |
36 | |
37 var numberOfResults = DEFAULT_NUMBER_OF_RESULTS; | |
38 try { | |
39 numberOfResults = prefBranch.getIntPref("numberOfResults"); | |
40 } catch(e){ | |
41 } | |
42 | |
43 var sizeOfInputArea = DEFAULT_SIZE_OF_INPUT_AREA; | |
44 try { | |
45 sizeOfInputArea = prefBranch.getIntPref("sizeOfInputArea"); | |
46 } catch(e){ | |
47 } | |
48 | |
49 text.size = sizeOfInputArea; | |
50 | |
51 | |
52 try { | |
53 if (!userId || !database.connection.tableExists('bookmark')) { | |
54 if (database.connection.tableExists('bookmark')) { | |
55 database.execute("DROP TABLE bookmark"); | |
56 } | |
57 database.execute(createTableSql); | |
58 } | |
59 } catch(e) { | |
60 | |
61 status.innerHTML = e.message || e; | |
62 loading.style.display = 'none'; | |
63 throw e; | |
64 } | |
65 | |
66 // global | |
67 incsearch = new IncSearch( | |
68 text, | |
69 viewArea, | |
70 { | |
71 dispMax: numberOfResults, | |
72 status: status, | |
73 pageLinkTop: pageLinkTop, | |
74 pageLinkBottom: pageLinkBottom, | |
75 database: database, | |
76 userId: userId | |
77 } | |
78 ); | |
79 incsearch.input.focus(); | |
80 | |
81 var update = function() { | |
82 incsearch.input.focus(); | |
83 new BookmarkLoader(loadingBox, loadingMessage, database); | |
84 }; | |
85 | |
86 sync.addEventListener('click', update, false); | |
87 | |
88 // settings | |
89 var settingBox = document.getElementById('settingBox'); | |
90 var settingSave = document.getElementById('settingSave'); | |
91 var settingCancel = document.getElementById('settingCancel'); | |
92 var numberOfResultElement = document.getElementById('numberOfResults'); | |
93 var sizeOfInputAreaElement = document.getElementById('sizeOfInputArea'); | |
94 | |
95 var openSetting = function() { | |
96 Glayer.showBox(settingBox); | |
97 numberOfResultElement.value = incsearch.dispMax; | |
98 numberOfResultElement.focus(); | |
99 sizeOfInputAreaElement.value = text.size; | |
100 }; | |
101 | |
102 var closeSetting = function() { | |
103 Glayer.hideBox(settingBox); | |
104 incsearch.input.focus(); | |
105 }; | |
106 | |
107 setting.addEventListener('click', openSetting, false); | |
108 | |
109 numberOfResultElement.addEventListener( | |
110 'keydown', | |
111 function(event) { | |
112 if (event.keyCode == 13) { | |
113 settingSave.click(); | |
114 } | |
115 }, | |
116 false); | |
117 | |
118 sizeOfInputAreaElement.addEventListener( | |
119 'keydown', | |
120 function(event) { | |
121 if (event.keyCode == 13) { | |
122 settingSave.click(); | |
123 } | |
124 }, | |
125 false); | |
126 | |
127 settingSave.addEventListener( | |
128 'click', | |
129 function() { | |
130 Glayer.hideBox(settingBox); | |
131 incsearch.dispMax = parseInt(numberOfResultElement.value) || incsearch.dispMax; | |
132 prefBranch.setIntPref('numberOfResults', incsearch.dispMax); | |
133 incsearch.input.size = parseInt(sizeOfInputAreaElement.value) || incsearch.input.size; | |
134 prefBranch.setIntPref('sizeOfInputArea', incsearch.input.size); | |
135 | |
136 incsearch.reset(); | |
137 incsearch.input.focus(); | |
138 }, | |
139 false); | |
140 | |
141 settingCancel.addEventListener('click', closeSetting, false); | |
142 | |
143 // help | |
144 var helpBox = document.getElementById('helpBox'); | |
145 var helpClose = document.getElementById('helpClose'); | |
146 | |
147 var openHelp = function() { | |
148 Glayer.showBox(helpBox); | |
149 helpClose.focus(); | |
150 }; | |
151 | |
152 var closeHelp = function() { | |
153 Glayer.hideBox(helpBox); | |
154 incsearch.input.focus(); | |
155 }; | |
156 | |
157 var hideMsgBox = function() { | |
158 helpBox.style.display = 'none'; | |
159 settingBox.style.display = 'none'; | |
160 loadingBox.style.display = 'none'; | |
161 | |
162 var confirmBox = document.getElementById('glayer_confirm'); | |
163 if (confirmBox) { | |
164 confirmBox.style.display = 'none'; | |
165 } | |
166 var alertBox = document.getElementById('glayer_alert'); | |
167 if (alertBox) { | |
168 alertBox.style.display = 'none'; | |
169 } | |
170 } | |
171 | |
172 help.addEventListener('click', openHelp, false); | |
173 helpClose.addEventListener('click', closeHelp, false); | |
174 | |
175 // shortcut | |
176 document.addEventListener( | |
177 'keydown', | |
178 function(event) { | |
6
ac5648afee47
change hot key from ctrl to alt.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
0
diff
changeset
|
179 if (event.altKey) { |
0 | 180 switch(event.keyCode) { |
181 case 85: // u (Sync) | |
182 hideMsgBox(); | |
183 update(); | |
184 IncSearch._stopEvent(event); | |
185 break; | |
186 case 83: // s (Setting) | |
187 if (settingBox.style.display == '') { | |
188 closeSetting(); | |
189 } else { | |
190 hideMsgBox(); | |
191 openSetting(); | |
192 } | |
193 IncSearch._stopEvent(event); | |
194 break; | |
195 case 191: // / (Help) | |
196 if (helpBox.style.display == '') { | |
197 closeHelp(); | |
198 } else { | |
199 hideMsgBox(); | |
200 openHelp(); | |
201 } | |
202 IncSearch._stopEvent(event); | |
203 break; | |
204 default: | |
205 break; | |
206 } | |
207 } | |
208 }, | |
209 false); | |
210 | |
211 | |
212 if (!userId) update(); | |
213 | |
214 }, false); | |
215 | |
216 window.addEventListener('unload', function(){ | |
217 if (database.connection.transactionInProgress) database.connection.rollbackTransaction(); | |
218 }, false); | |
219 | |
220 | |
221 (function() { | |
222 function getContents(aURL){ | |
223 var ioService = Components.classes["@mozilla.org/network/io-service;1"] | |
224 .getService(Components.interfaces.nsIIOService); | |
225 var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"] | |
226 .getService(Components.interfaces.nsIScriptableInputStream); | |
227 | |
228 var channel = ioService.newChannel(aURL, null, null); | |
229 var input = channel.open(); | |
230 scriptableStream.init(input); | |
231 var str = scriptableStream.read(input.available()); | |
232 scriptableStream.close(); | |
233 input.close(); | |
234 return str; | |
235 } | |
236 | |
237 var ios = Components.classes["@mozilla.org/network/io-service;1"] | |
238 .getService(Components.interfaces.nsIIOService); | |
239 var fileHandler = ios.getProtocolHandler("file") | |
240 .QueryInterface(Components.interfaces.nsIFileProtocolHandler); | |
241 | |
242 // user extension javascript | |
243 var userExtJs = Components.classes["@mozilla.org/file/directory_service;1"] | |
244 .getService(Components.interfaces.nsIProperties) | |
245 .get("ProfD", Components.interfaces.nsIFile); | |
246 | |
247 userExtJs.append(EXTENSION_NAME); | |
248 userExtJs.append(USER_EXT_JS); | |
249 | |
250 if (!userExtJs.exists()) { | |
251 var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] | |
252 .createInstance(Components.interfaces.nsIFileOutputStream); | |
253 | |
254 foStream.init(userExtJs, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate | |
255 | |
256 var sample = getContents("chrome://" + EXTENSION_NAME + "/content/" + USER_EXT_JS_SAMPLE); | |
257 foStream.write(sample, sample.length); | |
258 foStream.close(); | |
259 } | |
260 | |
261 var userExtJsURL = fileHandler.getURLSpecFromFile(userExtJs); | |
262 | |
263 document.write('<script type="text/javascript" src="'+ userExtJsURL +'"><\/script>'); | |
264 | |
265 // user stylesheet | |
266 var userCss = Components.classes["@mozilla.org/file/directory_service;1"] | |
267 .getService(Components.interfaces.nsIProperties) | |
268 .get("ProfD", Components.interfaces.nsIFile); | |
269 | |
270 userCss.append(EXTENSION_NAME); | |
271 userCss.append(USER_CSS); | |
272 | |
273 if (!userCss.exists()) { | |
274 var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] | |
275 .createInstance(Components.interfaces.nsIFileOutputStream); | |
276 | |
277 foStream.init(userCss, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate | |
278 | |
279 var sample = getContents("chrome://" + EXTENSION_NAME + "/content/" + USER_CSS_SAMPLE); | |
280 foStream.write(sample, sample.length); | |
281 foStream.close(); | |
282 } | |
283 | |
284 var userCssURL = fileHandler.getURLSpecFromFile(userCss); | |
285 | |
286 document.write('<link rel="stylesheet" href="' + userCssURL + '" type="text/css" />'); | |
287 })(); |