-
Notifications
You must be signed in to change notification settings - Fork 123
/
SearchOnEngineChangeForFx38.uc.js
136 lines (124 loc) · 4.63 KB
/
SearchOnEngineChangeForFx38.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
// ==UserScript==
// @name SearchOnEngineChangeForFx38.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description
// @include main
// @compatibility Firefox 38.0+
// @author Alice0775
// @version 2015/12/20 21:00 Ignore garbage pref
// @version 2015/12/20 20:00 Fix default search engine should be preference setting for new searchbar
// @version 2015/12/20 10:00 Fix nsIPrefBranch Service
// @version 2015/12/20 10:00 Fix Firefox 38.0+
// @version 2015/01/26 10:00 Fix Focus the content area
// @version 2014/10/21 19:00 デフォルトの検索エンジンは一番最初の奴にするようにした
// @version 2013/05/19 00:00 Bug 831008 Disable Mutation Events in chrome/XUL
// @version 2013/05/18 23:20 Bug 738818
// @version 2008/06/24 19:20 修正した
// @Note
// ==/UserScript==
// @version 2008/06/24 18:50 browser.search.openintabがtrueの時2重にタブが開くbug
// @version 2008/06/23 02:00 キーボード選択時も
// @version 2008/06/23 01:50 なんか非互換まくりだったので, 作り直し
// @version 2008/06/22 22:00
var searchOnEngineChange = {
// -- config --
KeepDefaultEngine: true, //デフォルトの検索エンジンに戻す
ClearWordAfterSearch: true, //検索後検索ワードをクリア
// -- config --
searchBar: null,
popup: null,
get xPref() {
delete this.xPref;
return this.xPref = Components.classes["@mozilla.org/preferences;1"]
.getService(Components.interfaces.nsIPrefBranch);
},
init: function(){
this.searchBar = document.getElementById("searchbar");
if(!this.searchBar)return;
if (!/ClearWordAfterSearch/.test(this.searchBar.doSearch.toString())) {
this.searchBar.doSearch_org = this.searchBar.doSearch;
this.searchBar.doSearch = function(aData, aWhere, aEngine){
this.doSearch_org(aData, aWhere, aEngine);
if (searchOnEngineChange.ClearWordAfterSearch){
searchOnEngineChange.clearWord();
}
if (searchOnEngineChange.KeepDefaultEngine){
let defaultEngine = searchOnEngineChange.getDefaultEngine();
setTimeout(function(){Services.search.currentEngine = defaultEngine;},0);
}
}
}
this.popup = document.getAnonymousElementByAttribute(this.searchBar, "anonid", "searchbar-popup");
if (this.popup) {
this.popup.addEventListener("command", this, false);
this.popup.addEventListener("click", this, false);
}
window.addEventListener("aftercustomization", this, false);
},
isOldSearchBar: function() {
try {
// new searchbar
if (typeof classicthemerestorerjs != "undefined")
return this.xPref.getBoolPref("extensions.classicthemerestorer.ctroldsearch");
} catch(x) {}
try {
// old searchbar
if (this.popup)
return !this.xPref.getBoolPref("browser.search.showOneOffButtons");
} catch(x) {}
// default new searchbar
return false;
},
getDefaultEngine: function() {
if (!this.isOldSearchBar()) {
let aEngineName = this.xPref.getCharPref("browser.search.defaultenginename");
return Services.search.getEngineByName(aEngineName);
}
return Services.search.getVisibleEngines({ })[0];
},
handleEvent: function(event){
switch (event.type) {
case "unload":
if (this.popup) {
this.popup.removeEventListener("command", this, true);
this.popup.removeEventListener("click", this, true);
}
window.removeEventListener("aftercustomization", this, false);
break;
case "aftercustomization":
this.init();
break;
case "click":
if (event.button != 0)
this.doSearch(event);
break;
case "command":
this.doSearch(event);
break;
}
},
clearWord: function(){
this.searchBar.value = "";
this.searchBar.updateDisplay();
},
where: function(aEvent){
var where = "current";
if (aEvent.button == 2)
return;
where = whereToOpenLink(aEvent, false, true);
return where;
},
doSearch: function(event){
var target = event.target;
var aEngine = target.engine;
if (!aEngine)
return;
var aData = this.searchBar._textbox.value;
if (!aData)
return;
var aWhere = this.where(event);
this.searchBar.doSearch(aData, aWhere, aEngine);
}
};
searchOnEngineChange.init();
window.addEventListener('unload', searchOnEngineChange, false);