forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenableKeywordsSearchInSearchBar.uc.js
172 lines (156 loc) · 6.25 KB
/
enableKeywordsSearchInSearchBar.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
// ==UserScript==
// @name enableKeywordsSearchInSearchBar.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description Enable Keywords Search In SearchBar
// @include main
// @compatibility Firefox 17-24
// @author Alice0775
// @Note Keywords Searchを検索バーから行えるようにする
// @Note サーチバーを表示していないとダメ
// @Note kusizasi for conquerymodoki may be broken
// @version 2014/06/11 21:00 fix keyword
// @version 2013/07/18 18:30 Bug 846635 - Use asynchronous getCharsetForURI in getShortcutOrURI in Firefox25 and later
// @note (For Firefox25+, use enableKeywordsSearchInSearchBar_Fx25.uc.js)
// ==/UserScript==
(function(){
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return
window.kusizasi = function(name, param){
const nsIBSS = Components.interfaces.nsIBrowserSearchService;
const searchService =Components.classes["@mozilla.org/browser/search-service;1"].getService(nsIBSS);
var popup = document.getElementById("context-conqueryModoki-popup");
if (!conqueryModoki || !popup)
return;
var walker = document.createTreeWalker(popup, NodeFilter.SHOW_ELEMENT, null, true);
var target;
while ((target = walker.nextNode())){
if (target.localName.toLowerCase() != "menu" )
continue;
if (target.getAttribute("label") == name.replace(/^{/,'')){
var result = document.evaluate("*/*[@class = 'menuitem-iconic searchbar-engine-menuitem']",target,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
//alert(result.snapshotLength);
var searchBar = conqueryModoki.searchBar();
if (searchBar)
searchBar.value = param;
var anewTab = true;
for(var i=0; i<result.snapshotLength; i++){
try{
setTimeout(function(self, aEngine,selected,anewTab){
self.loadSearch(aEngine, selected, anewTab, false, false) //検索実行
setTimeout(function(){conqueryModoki._dispatchEvent();},0);
},100, conqueryModoki, result.snapshotItem(i).engine, param, anewTab);
}catch(e){}
}
conqueryModoki.clearSearchBar();
}
}
}
if ("getShortcutOrURIAndPostData" in window) {
var func = getShortcutOrURIAndPostData.toString();
func = func.replace(
'throw new Task.Result({ postData: submission.postData,',
'throw new Task.Result({ engine: engine, param: param, postData: submission.postData,'
);
eval("getShortcutOrURIAndPostData = " + func);
//Fx25+
searchBar.doSearch__keyworks = searchBar.doSearch;
searchBar.doSearch = function(aData, aWhere){
var shortcutURL = null;
var aPostDataRef = {};
var offset = aData.indexOf(" ");
if (offset > 0) {
var self = this;
Task.spawn(function() {
let data = yield getShortcutOrURIAndPostData(aData);
if (data.engine && data.engine.name.match(/^{/) && !engine.name.match(/}/)) {
kusizasi(engine.name, param);
return;
}
if (data.url && data.url != aData) {
// allow third-party services to fixup this URL
if (data.url && data.url != aData) {
//remove keyword
self._textbox.value = aData.substr(offset + 1);
try {
var textBox = self._textbox;
// Save the current value in the form history
if (textBox.value && !PrivateBrowsingUtils.isWindowPrivate(window)) {
self.FormHistory.update(
{ op : "bump",
fieldname : textBox.getAttribute("autocompletesearchparam"),
value : textBox.value },
{ handleError : function(aError) {
Components.utils.reportError("Saving search to form history failed: " + aError.message);
}});
}
} catch(ee) {}
//do keyword search
if (/^javascript:/.test(data.url))
aWhere = "current";
openUILinkIn(data.url, aWhere, null, data.postData);
}
} else {
self.doSearch__keyworks(aData, aWhere)
}
});
} else {
//do normal search
this.doSearch__keyworks(aData, aWhere)
}
}
} else {
// Fx24 and earlier
if (gURLBar && "handleCommand" in gURLBar){
var func = gURLBar.handleCommand.toString();
//Hack Noscript [noscriptBM.js]
func = func.replace(
'var url = getShortcutOrURI(shortcut, {});',
'$& \
if (!url){ \
this.handleRevert(); \
return; \
}'
);
eval("gURLBar.handleCommand = " + func);
}
var func = getShortcutOrURI.toString();
func = func.replace(
'if (engine) {',
'if (engine) { \
if(engine.name.match(/^{/) && !engine.name.match(/}/) && !!param.replace(/^\s+/,\'\').replace(/\s+$/,\'\')){ \
if ("conqueryModoki" in window) \
kusizasi(engine.name, param); \
return ""; \
}'
);
eval("getShortcutOrURI = " + func);
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return
{ //素Fx
searchBar.doSearch__keyworks = searchBar.doSearch;
searchBar.doSearch = function(aData, aWhere){
var shortcutURL = null;
var aPostDataRef = {};
var offset = aData.indexOf(" ");
if (offset > 0) {
shortcutURL = getShortcutOrURI(aData, aPostDataRef);
if (!shortcutURL)
return;
}
if (shortcutURL && shortcutURL != aData){
//remove keyword
this._textbox.value = aData.substr(offset + 1);
//do keyword search
if (/^javascript:/.test(shortcutURL))
aWhere = "current";
openUILinkIn(shortcutURL, aWhere, null, aPostDataRef.value, null);
} else {
//do normal search
this.doSearch__keyworks(aData, aWhere)
}
}
}
}
})();