-
Notifications
You must be signed in to change notification settings - Fork 123
/
addHistoryFindbar_Fx25.uc.js
590 lines (537 loc) · 22.1 KB
/
addHistoryFindbar_Fx25.uc.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// ==UserScript==
// @name addHistoryFindbarFx25.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description add History to Findbar
// @include main
// @include chrome://global/content/viewSource.xul
// @include chrome://global/content/viewPartialSource.xul
// @compatibility Firefox 25
// @author Alice0775
// @version 2016/03/31 00:00 Fix textbox height
// @version 2015/02/25 00:30 Fix tabview take 2
// @version 2015/02/25 00:30 Fix tabview
// @version 2014/10/25 12:30 Fix viewsource
// @version 2014/10/22 23:30 Fix broken find from form-history
// @version 2014/10/21 19:30 Fix broken ENTER etc due to XUL/migemo input
// @version 2014/10/18 23:40 XUL/migemo input
// @version 2014/09/14 23:40 XUL/migemo
// @version 2014/09/14 23:20 more frequent status check
// @version 2013/11/28 23:00 XUL/migemo
// @version 2013/11/22 21:00 XUL/migemo
// @version 2013/11/22 17:30 Fix input and click caret position etc..
// @version 2013/11/16 12:30 Firefox25
// ==/UserScript==
// @version 2009/01/25 テーマに依存する
// @Note need Sub-Script/Overlay Loader v3.0.20mod +
var historyFindbar = {
// --config --
// ENTERキーでのみ保存する[true] どのキーでも保存する false
ONLYENTER : true,
//保存間隔(msec)
KEY_TIMER : 2000,
// --config --
adjustSizeDelay: 200,
adjustSizeTimer: null,
addHistoryFindbarTimeout: null,
lastInputValue: "",
get historyfindbar(){
delete this.historyfindbar;
return this.historyfindbar = document.getElementById("historyfindbar");
},
get _findField2(){
delete this._findField2;
return this._findField2 = document.getElementById("find-field2");
},
get inputbox(){
delete this.inputbox;
return this.inputbox = document.getAnonymousElementByAttribute(this._findField2,
"anonid", "textbox-input-box");
},
get historyPopup(){
delete this.historyPopup;
return this.historyPopup = document.getAnonymousElementByAttribute(this._findField2, "anonid", "historydropmarker");
},
get contextmenu(){
delete this.contextmenu;
return this.contextmenu = document.getAnonymousElementByAttribute(this.inputbox,
"anonid", "input-box-contextmenu");
},
init :function(){
var overlay = ' \
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
xmlns:html="http://www.w3.org/1999/xhtml"> \
<window id="main-window"> \
<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
pack="end" \
id="historyfindbar"> \
<hbox id="historyfindbar-box" \
align="center" \
flex="1" \
pack="end" \
style="position: fixed; width: auto; height: 29px; left: 0px; top: auto; bottom: 0px;"> \
<textbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
type="autocomplete" \
id="find-field2" \
autocompletesearch="form-history" \
autocompletesearchparam="findbar-history" \
disableAutocomplete="true" \
oninput="historyFindbar.copyToFindfield(event);" \
onkeypress="historyFindbar.copyToFindfield(event);" \
oncompositionstart="historyFindbar.handleEvent(event);" \
oncompositionend="historyFindbar.handleEvent(event);" \
onclick="historyFindbar.copyToFindfield(event);" \
ondrop="historyFindbar.handleEvent(event);" \
style="margin-left: 0px;"/> \
</hbox> \
</hbox> \
</window> \
<window id="viewSource"> \
<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
pack="end" \
id="historyfindbar"> \
<hbox id="historyfindbar-box" \
align="center" \
flex="1" \
pack="end" \
style="position: fixed; width: auto; height: 29px; left: 0px; top: auto; bottom: 0px;"> \
<textbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
type="autocomplete" \
id="find-field2" \
autocompletesearch="form-history" \
autocompletesearchparam="findbar-history" \
disableAutocomplete="true" \
oninput="historyFindbar.copyToFindfield(event);" \
onkeypress="historyFindbar.copyToFindfield(event);" \
oncompositionstart="historyFindbar.handleEvent(event);" \
oncompositionend="historyFindbar.handleEvent(event);" \
onclick="historyFindbar.copyToFindfield(event);" \
ondrop="historyFindbar.handleEvent(event);" \
style="margin-left: 0px;"/> \
</hbox> \
</hbox> \
</window> \
</overlay>';
overlay = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + encodeURI(overlay);
window.userChrome_js.loadOverlay(overlay, this);
},
observe: function(){
XPCOMUtils.defineLazyModuleGetter(this, "FormHistory",
"resource://gre/modules/FormHistory.jsm");
var style = ' \
@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul); \
findbar { \
transition-property: margin-bottom, opacity, visibility !important; \
transition-duration: 0ms, 0ms, 0s !important; \
transition-timing-function: ease-in-out, ease-in-out, linear !important; \
} \
\
findbar[hidden] { \
transition-delay: 0s, 0s, 0ms !important; \
} \
\
/*履歴を出すボタンを表示*/ \
#find-field2 .autocomplete-history-dropmarker { \
display: -moz-box; \
-moz-binding: url("chrome://global/content/bindings/autocomplete.xml#history-dropmarker"); \
} \
\
#find-field2 { \
-moz-appearance:none !important;\
} \
\
#historyfindbar[hidden] #find-field2 > popupset { \
display:none; \
} \
\
#find-field2[_moz-xmigemo-disable-ime="true"] { \
ime-mode: disabled !important; \
} \
#find-field2[_moz-xmigemo-inactivate-ime="true"] { \
ime-mode: inactive !important; \
} \
#tab-view-deck[selectedIndex="1"] ~ #historyfindbar { \
display:none; \
} \
'.replace(/\s+/g, " ");
var sspi = document.createProcessingInstruction(
'xml-stylesheet',
'type="text/css" href="data:text/css,' + encodeURIComponent(style) + '"'
);
document.insertBefore(sspi, document.documentElement);
sspi.getAttribute = function(name) {
return document.documentElement.getAttribute(name);
};
this.historyfindbar.setAttribute('hidden', true);
// コンテキストメニュー
const kXULNS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
let cxmenu = this.contextmenu;
let element, label, akey;
element = document.createElementNS(kXULNS, "menuseparator");
cxmenu.appendChild(element);
element = document.createElementNS(kXULNS, "menuitem");
label = "Clear Search History";
akey = "H";
element.setAttribute("label", label);
element.setAttribute("accesskey", akey);
element.setAttribute("oncommand", "historyFindbar.clearHistory();");
cxmenu.appendChild(element);
if (typeof gFindBar == 'undefined') {
window.gFindBar = document.getElementById("FindToolbar");
// gFindBar.getElement("findbar-textbox")
// gFindBar._findField = document.getAnonymousElementByAttribute(gFindBar, "anonid", "findbar-textbox");
}
window.addEventListener("resize", this, false);
window.addEventListener("findbaropen", this, false);
window.addEventListener("find", this, false);
window.addEventListener("findagain", this, false);
window.addEventListener("findclose", this, false);
//終了のためESCの監視と, 少なくともF3,F4が押されたとき保存する
window.addEventListener("keypress", this, true);
if (this.isMainBrowser())
gBrowser.tabContainer.addEventListener("TabSelect", this, false);
//fx25 for existing findbar
if ("gBrowser" in window && "getFindBar" in gBrowser) {
if (gBrowser.selectedTab._findBar) {
historyFindbar.patch(gBrowser.selectedTab._findBar);
}
}
//fx25 for newly created findbar
if ("gBrowser" in window && "getFindBar" in gBrowser) {
gBrowser.tabContainer.addEventListener("TabFindInitialized", function(event){
historyFindbar.patch(event.target._findBar);
});
}
},
patch: function(aFindBar) {
if (this.isMainBrowser() && gBrowser.isFindBarInitialized(gBrowser.selectedTab) && !gFindBar.hidden) {
setTimeout(function(self){
if (gBrowser.isFindBarInitialized(gBrowser.selectedTab) && !gFindBar.hidden) {
self.adjustSize();
}
}, 0, this);
setTimeout(function(self){
if (gBrowser.isFindBarInitialized(gBrowser.selectedTab) && !gFindBar.hidden) {
self.adjustSize();
self.historyfindbar.removeAttribute('hidden');
}
}, 800, this);
}
setTimeout(function(aFindBar){
aFindBar.close_org = aFindBar.close;
aFindBar.close = function() {
this.close_org();
this._dispatchFindEvent("close");
}
}.bind(this), 100, aFindBar);
},
uninit: function(){
window.removeEventListener('unload', this, false);
window.removeEventListener("resize", this, false);
window.removeEventListener("findbaropen", this, false);
window.removeEventListener("find", this, false);
window.removeEventListener("findagain", this, false);
window.removeEventListener("findclose", this, false);
if (this.isMainBrowser())
gBrowser.tabContainer.removeEventListener("TabSelect", this, false);
},
handleEvent: function(aEvent){
var controller = null;
//window.userChrome_js.debug("handleEvent " + aEvent.type);
switch (aEvent.type) {
case 'unload':
this.uninit();
break;
case 'resize':
//window.userChrome_js.debug("change gFindBar._findField.value " + gFindBar._findField.value);
if (this.adjustSizeTimer)
clearTimeout(this.adjustSizeTimer);
this.adjustSizeTimer = setTimeout(function(){
this.adjustSize();
}.bind(this), this.adjustSizeDelay, this);
break;
case 'findbaropen':
historyFindbar.statusModified();
// XUL/Migenoの時 および QuickFind の時は何もしたい
if ("XMigemoUI" in window && XMigemoUI.isQuickFind || gFindBar._findMode != gFindBar.FIND_NORMAL)
return;
//Findbarの表示状態を監視して, 非表示にしたり背景の色を変えたりする
// create an observer instance
gFindBar.historyfindbarObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
switch (mutation.attributeName) {
case "hidden":
this.historyfindbar.hidden = gFindBar.hidden;
break;
case "_moz-xmigemo-disable-ime":
case "_moz-xmigemo-inactivate-ime":
if (mutation.target.getAttribute("mutation.attributeName"))
this._findField2.setAttribute(attrName, 'true');
else
this._findField2.removeAttribute(attrName);
break;
case "status":
case "flash":
this.statusModified();
break;
}
}.bind(this));
}.bind(this));
// configuration of the observer:
var config = { attributes: true, subtree: true};
// pass in the target node, as well as the observer options
gFindBar.historyfindbarObserver.observe(gFindBar, config);
this.placeholderModified();
gFindBar._findField.addEventListener("focus", this, false);
this.historyfindbar.hidden = gFindBar.hidden;
this.adjustSize();
setTimeout(function(){
this._findField2.value = gFindBar._findField.value;
this._findField2.focus();
}.bind(this), 0, this);
break;
case 'find':
case "findagain":
historyFindbar.statusModified();
if ( gFindBar._findField.value != this.lastInputValue){
this.addToHistory(gFindBar._findField.value);
}
break;
case 'XMigemoFindBarClose':
case 'findclose':
// stop observing
if (gFindBar.historyfindbarObserver)
gFindBar.historyfindbarObserver.disconnect();
gFindBar._findField.removeEventListener("focus", this, false);
this.historyfindbar.hidden = true;
content.focus();
break;
case 'TabSelect':
setTimeout(function(){
if (gBrowser.isFindBarInitialized(aEvent.target)) {
this.historyfindbar.hidden = gFindBar.hidden;
this._findField2.value = gFindBar._findField.value;
this.statusModified();
} else
this.historyfindbar.hidden = true;
}.bind(this), 0);
break;
case 'focus':
this._findField2.focus();
break;
case 'compositionstart':
// Don't close the find toolbar while IME is composing.
gFindBar._isIMEComposing = true;
if (gFindBar._quickFindTimeout) {
clearTimeout(gFindBar._quickFindTimeout);
gFindBar._quickFindTimeout = null;
}
break;
case 'compositionend':
gFindBar._isIMEComposing = false;
if (gFindBar._findMode != gFindBar.FIND_NORMAL &&
!gFindBar.hidden)
gFindBar._setFindCloseTimeout();
break;
case 'drop': //fx3.1 more
aEvent.stopPropagation(); //fx3.6 more
aEvent.preventDefault(); //fx4.0 more
this.drop(aEvent);
break;
}
},
placeholderModified: function() {
if (gFindBar._findMode == gFindBar.FIND_TYPEAHEAD)
this._findField2.placeholder = gFindBar._fastFindStr;
else if (gFindBar._findMode == gFindBar.FIND_LINKS)
this._findField2.placeholder = gFindBar._fastFindLinksStr;
else
this._findField2.placeholder = gFindBar._normalFindStr;
},
statusModified: function() {
var status = gFindBar._findField.getAttribute("status");
if (status)
this._findField2.setAttribute("status", status);
else if (!status)
this._findField2.removeAttribute("status");
var cssProp = ["border-color", "background-color", "color"];
var style = window.getComputedStyle(gFindBar._findField, null);
for (var i = 0; i <=2; i++) {
this._findField2.style.setProperty(cssProp[i], style.getPropertyValue(cssProp[i]), "");
}
},
drop: function (aEvent) {
if ("findBarOnDropUseTextContent" in gFindBar._findField) {
gFindBar._findField.findBarOnDropUseTextContent(aEvent);
this._findField2.value = gFindBar._findField.value;
return;
}
var data = aEvent.dataTransfer.getData("text/plain");
//window.userChrome_js.debug("onDrop " + data);
if (data) {
this._findField2.value = data;
this.copyToFindfield(aEvent);
this.addToHistory(data);
}
},
getSupportedFlavours: function () {
var flavourSet = new FlavourSet();
flavourSet.appendFlavour("text/unicode");
flavourSet.appendFlavour("text/x-moz-url");
flavourSet.appendFlavour("application/x-moz-file", "nsIFile");
return flavourSet;
},
adjustSize: function(event){
//本来のfindbar-textboxに履歴付きのfind-field2を重なるように配置する。
if (!this.isMainBrowser() && gFindBar.hidden)
return;
else if (this.isMainBrowser() && (!gBrowser.isFindBarInitialized(gBrowser.selectedTab) || gFindBar.hidden))
return;
var textbox = gFindBar._findField;
var textbox_boxObject = textbox.boxObject;
var gFindBar_boxObject = gFindBar.boxObject;
var textboxX = textbox_boxObject.screenX - gFindBar_boxObject.screenX;
var textboxY = textbox_boxObject.screenY - gFindBar_boxObject.screenY;
var textboxW = textbox_boxObject.width;
var textboxH = textbox_boxObject.height;
this.textboxW = textboxW;
var historyfindField = this._findField2;
var historyfindbarbox = document.getElementById("historyfindbar-box");
historyfindbarbox.style.bottom = "0px";
historyfindbarbox.style.height = gFindBar_boxObject.height + "px";
historyfindbarbox.style.left = (gFindBar_boxObject.screenX
- this.historyfindbar.boxObject.screenX) + "px";
historyfindbarbox.style.bottom = (historyfindbarbox.boxObject.screenY
- gFindBar_boxObject.screenY) + "px";
var textboxblft = window.getComputedStyle(textbox, '').
getPropertyValue('border-left-width').replace('px','')
var textboxbrgt = window.getComputedStyle(textbox, '').
getPropertyValue('border-right-width').replace('px','')
var textboxbtop = window.getComputedStyle(textbox, '').
getPropertyValue('border-top-width').replace('px','')
var textboxbbot = window.getComputedStyle(textbox, '').
getPropertyValue('border-bottom-width').replace('px','')
historyfindField.style.width = -0+textboxW + "px";
historyfindField.style.height = "auto"; /*textboxH + "px";*/
//
historyfindbarbox.style.marginLeft = 0+textboxX + "px";
historyfindField.style.marginTop = textboxY + "px";
},
copyToFindfield: function(aEvent){
var textbox = this._findField2;
if (aEvent.originalTarget == this._findField2.inputField &&
aEvent.type == 'keypress' && "XMigemoUI" in window) {
XMigemoUI.onKeyPress(aEvent, XMigemoUI.getFindFieldFromContent(aEvent.originalTarget));
textbox.value = gFindBar._findField.value;
}
if (aEvent.originalTarget == this._findField2.inputField &&
aEvent.keyCode == KeyEvent.DOM_VK_TAB) {
this._handleTab(aEvent);
return;
}
if (aEvent.originalTarget == this._findField2.inputField &&
(aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE ||
aEvent.keyCode == KeyEvent.DOM_VK_RETURN ||
aEvent.keyCode == KeyEvent.DOM_VK_ENTER ||
aEvent.keyCode == KeyEvent.DOM_VK_PAGE_UP ||
aEvent.keyCode == KeyEvent.DOM_VK_PAGE_DOWN ||
aEvent.keyCode == KeyEvent.DOM_VK_UP ||
aEvent.keyCode == KeyEvent.DOM_VK_DOWN)) {
// do nothing if history drop down is openned
if (!!this.historyPopup.getAttribute("open")) {
return
}
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent ('keypress', true, true, window,
aEvent.ctrlKey, aEvent.altKey,
aEvent.shiftKey, aEvent.metaKey,
aEvent.keyCode, 0);
gFindBar._findField.dispatchEvent(evt);
}
//本来のfindbar-textboxに転記して, ダミーイベント送信
gFindBar._findField.value = textbox.value;
//gFindBar._findField.removeAttribute('status');
if (aEvent.type == 'input' ||
aEvent.type == 'drop'){
var evt = document.createEvent("UIEvents");
evt.initUIEvent("input", true, true, window, 0);
gFindBar._findField.dispatchEvent(evt);
}
if(textbox.value.replace(/ /g,'')===''){
gFindBar._findField.removeAttribute('status');
this.lastInputValue = '';
return;
}
//ENTERなら保存
//window.userChrome_js.debug("copyToFindfield " + this.lastInputValue +"\n"+textbox.value);
if (!this.ONLYENTER ||
aEvent.type == 'keypress' && aEvent.keyCode == KeyEvent.DOM_VK_RETURN ){
this.addToHistory(textbox.value);
}
},
_handleTab: function (aEvent) {
var shouldHandle = !aEvent.altKey && !aEvent.ctrlKey &&
!aEvent.metaKey;
//window.userChrome_js.debug(gFindBar._finishFAYT(aEvent));
if (shouldHandle &&
gFindBar._findMode == gFindBar.FIND_NORMAL) {
aEvent.preventDefault();
if (aEvent.shiftKey) {
content.focus();
} else {
if (gFindBar.getElement("find-previous").disabled)
gFindBar.getElement("highlight").focus();
else
gFindBar.getElement("find-previous").focus();
}
} else {
gFindBar._handleTab(aEvent);
}
},
addToHistory: function(value){
try {
if (PrivateBrowsingUtils.isWindowPrivate(window))
return;
} catch(ex) {
if (document.documentElement.getAttribute("titlemodifier_privatebrowsing") ==
document.documentElement.getAttribute("titlemodifier"))
return;
}
//データーベースに記入
if(this.lastInputValue == value) return;
if(value.replace(/ /g,'').length <= 2){
this.lastInputValue = '';
return;
}
if(this.addHistoryFindbarTimeout){
clearTimeout(this.addHistoryFindbarTimeout);
}
this.addHistoryFindbarTimeout = setTimeout(function(self){
self.lastInputValue = value;
if(!!self.lastInputValue) {
self.FormHistory.update(
{ op : "bump",
fieldname : "findbar-history",
value : value
},
{ handleError : function(aError) {
Components.utils.reportError("Saving search to form history failed: " + aError.message);
}});
}
}, this.KEY_TIMER, this);
},
clearHistory: function() {
try {
if (PrivateBrowsingUtils.isWindowPrivate(window))
return;
} catch(ex) {}
this.FormHistory.update(
{ op : "remove",
fieldname : "findbar-history"
}, null);
},
isMainBrowser: function() {
return ("gBrowser" in window && "tabContainer" in gBrowser);
}
}
historyFindbar.init();