forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenableKeywordsSearchInSearchBar_Fx25.uc.js
More file actions
225 lines (200 loc) · 8.81 KB
/
enableKeywordsSearchInSearchBar_Fx25.uc.js
File metadata and controls
225 lines (200 loc) · 8.81 KB
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
// ==UserScript==
// @name enableKeywordsSearchInSearchBar_Fx25.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description Enable Keywords Search In SearchBar
// @include main
// @compatibility Firefox 25+
// @author Alice0775
// @Note Keywords Searchを検索バーから行えるようにする
// @Note conqueryModoki2がある場合は, 親フォルダにKeywordsを付加していれば, Keywords 串刺し検索が出来る
// @Note サーチバーを表示していないとダメ
// @version 2014/03/31 00:00 add fail safe setTimeout
// @version 2013/11/21 12:30 Firefox25 分離
// @version 2013/07/18 18:30 Bug 846635 - Use asynchronous getCharsetForURI in getShortcutOrURI in Firefox25
// @version 2012/12/08 22:30 Bug 788290 Bug 788293 Remove E4X
// @version 2012/12/08 22:30 Bug 788290 Bug 788293 Remove E4X
// ==/UserScript==
// @version 2009/08/02 Tab Mix Plusはもはや対応しない
// @version 2009/06/13 Tab Mix Plus0.3.7.4pre.090516
// @version 2009/05/08 00:30 キワードサーチのurlがjavascriptの場合はカレントタブに開くように
// @version 2009/02/18 14:30 Full Screen でも動作するように
// @version 2008/09/26 11:00 For checked in Bug 337345
// @version 2008/07/26 15:30 NoScript 1.7.7をインストールしている場合に対応
// @version 2008/07/26 conqueryModoki2がある場合はKeywords 串刺し検索を追加
// @version 2008/07/04 Tab Mix Plusにも
// @version 2008/07/02
(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 (gURLBar && "_canonizeURL" in gURLBar){
var func = gURLBar._canonizeURL.toString();
//Hack Noscript [noscriptBM.js]
func = func.replace(
' let data = yield getShortcutOrURIAndPostData(url);',
'let data = yield window.kusizasi.getShortcutOrURIAndPostData(url); \
if (!data.url){ \
this.handleRevert(); \
return; \
}'
);
eval("gURLBar._canonizeURL = " + func);
}
window.kusizasi.getShortcutOrURIAndPostData = function(aURL) {
return Task.spawn(function() {
let mayInheritPrincipal = false;
let postData = null;
let shortcutURL = null;
let keyword = aURL;
let param = "";
let offset = aURL.indexOf(" ");
if (offset > 0) {
keyword = aURL.substr(0, offset);
param = aURL.substr(offset + 1);
}
let engine = Services.search.getEngineByAlias(keyword);
if (engine && !!param.trim()) {
if("conqueryModoki" in window && engine.name.match(/^{/) && !engine.name.match(/}/)){
if (param && !PrivateBrowsingUtils.isWindowPrivate(window)) {
conqueryModoki.searchBar().FormHistory.update(
{ op : "bump",
fieldname : "searchbar-history",
value : param },
{ handleError : function(aError) {
Components.utils.reportError("Saving search to form history failed: " + aError.message);
}});
}
kusizasi(engine.name, param);
throw new Task.Result({ postData: null,
url: null,
mayInheritPrincipal: mayInheritPrincipal });
}
let submission = engine.getSubmission(param);
postData = submission.postData;
throw new Task.Result({ postData: submission.postData,
url: submission.uri.spec,
mayInheritPrincipal: mayInheritPrincipal });
}
[shortcutURL, postData] =
PlacesUtils.getURLAndPostDataForKeyword(keyword);
if (!shortcutURL)
throw new Task.Result({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
let escapedPostData = "";
if (postData)
escapedPostData = unescape(postData);
if (/%s/i.test(shortcutURL) || /%s/i.test(escapedPostData)) {
let charset = "";
const re = /^(.*)\&mozcharset=([a-zA-Z][_\-a-zA-Z0-9]+)\s*$/;
let matches = shortcutURL.match(re);
if (matches)
[, shortcutURL, charset] = matches;
else {
// Try to get the saved character-set.
try {
// makeURI throws if URI is invalid.
// Will return an empty string if character-set is not found.
charset = yield PlacesUtils.getCharsetForURI(makeURI(shortcutURL));
} catch (e) {}
}
// encodeURIComponent produces UTF-8, and cannot be used for other charsets.
// escape() works in those cases, but it doesn't uri-encode +, @, and /.
// Therefore we need to manually replace these ASCII characters by their
// encodeURIComponent result, to match the behavior of nsEscape() with
// url_XPAlphas
let encodedParam = "";
if (charset && charset != "UTF-8")
encodedParam = escape(convertFromUnicode(charset, param)).
replace(/[+@\/]+/g, encodeURIComponent);
else // Default charset is UTF-8
encodedParam = encodeURIComponent(param);
shortcutURL = shortcutURL.replace(/%s/g, encodedParam).replace(/%S/g, param);
if (/%s/i.test(escapedPostData)) // POST keyword
postData = getPostDataStream(escapedPostData, param, encodedParam,
"application/x-www-form-urlencoded");
}
else if (param) {
// This keyword doesn't take a parameter, but one was provided. Just return
// the original URL.
postData = null;
throw new Task.Result({ postData: postData, url: aURL,
mayInheritPrincipal: mayInheritPrincipal });
}
// This URL came from a bookmark, so it's safe to let it inherit the current
// document's principal.
mayInheritPrincipal = true;
throw new Task.Result({ postData: postData, url: shortcutURL,
mayInheritPrincipal: mayInheritPrincipal });
});
};
window.kusizasi.getShortcutOrURI = function(aURI) {
// Firefox 25 and later
var getShortcutOrURIAndPostData = kusizasi.getShortcutOrURIAndPostData;
var done = false;
Task.spawn(function() {
var data = yield getShortcutOrURIAndPostData(aURI);
aURI = data.url;
done = true;
});
// this should be rewritten in asynchronous style...
setTimeout(function(){done = true;}, 1000);
var thread = Cc['@mozilla.org/thread-manager;1'].getService().mainThread;
while (!done)
{
thread.processNextEvent(true);
}
return aURI;
};
searchBar.doSearch__keyworks = searchBar.doSearch;
searchBar.doSearch = function doSearch(aData, aWhere) {
var textBox = this._textbox;
var shortcutURL = null;
var aPostDataRef = {};
var offset = aData.indexOf(" ");
if (offset > 0) {
shortcutURL = kusizasi.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);
} else {
searchBar.doSearch__keyworks(aData, aWhere);
}
};
})();