forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontextAdvancedSearch.uc.js
More file actions
180 lines (165 loc) · 5.91 KB
/
contextAdvancedSearch.uc.js
File metadata and controls
180 lines (165 loc) · 5.91 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
// ==UserScript==
// @name contextAdvancedSearch.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description 右クリックの検索メニューを, 中クリックでNOT検索, 右クリックで AND検索を行う
// @include main
// @compatibility Firefox 2.0 3.0
// @author Alice0775
// @version 2009/05/14 00:00 isempty
// ==/UserScript==
// @version 2008/04/21 12:00 文字数を制限した
// @version 2008/04/15 23:10
// @Note
var contextAdvancedSearch = {
//エンジン名の一部と可能な機能
mFunc: {'Google' : {AND:'+%%S', OR:' OR %%S', NOT:' -%%S', SL:' site:%%S'},
'Yahoo' : {AND:'+"%%S', OR:' OR %%S', NOT:' -%%S', SL:' site:%%S'},
'goo ' : {AND:' %%S', OR:' OR %%S', NOT:' -%%S', SL:'' },
'Live Search' : {AND:'+%%S', OR:' OR %%S', NOT:' -(%%S)', SL:'' },
'Wikipedia-JP' : {AND:' %%S' , OR:' OR %%S', NOT:'' , SL:'' }
},
menuitem:document.getElementById("context-searchselect"),
init: function(){
this.menuitem.setAttribute("onclick","contextAdvancedSearch.handleEvent(event);");
//コンテキストメニューポップアップ時のイベント登録
document.getElementById('contentAreaContextMenu').
addEventListener('popupshowing', this, false);
},
uninit: function(){
document.getElementById('contentAreaContextMenu').
removeEventListener('popupshowing', this, false);
},
//現在のウインドウを得る
_getFocusedWindow: function(){
var focusedWindow = document.commandDispatcher.focusedWindow;
if (!focusedWindow || focusedWindow == window)
return window._content;
else
return focusedWindow;
},
//選択文字列を得る
_getselection: function() {
var targetWindow = this._getFocusedWindow();
var sel = Components.lookupMethod(targetWindow, 'getSelection').call(targetWindow);
if (sel && !sel.toString()) {
var node = document.commandDispatcher.focusedElement;
if (node &&
(node.type == "text" || node.type == "textarea") &&
'selectionStart' in node &&
node.selectionStart != node.selectionEnd) {
var offsetStart = Math.min(node.selectionStart, node.selectionEnd);
var offsetEnd = Math.max(node.selectionStart, node.selectionEnd);
return node.value.substr(offsetStart, offsetEnd-offsetStart);
}
}
return sel ?
sel.toString() : "";
},
handleEvent: function(event){
switch (event.type) {
case "click":
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return;
var str = searchBar.textbox.textValue;
if (event.button ==0 || str== '' || searchBar._textbox.hasAttribute("isempty")||searchBar.hasAttribute("empty")) return;
document.getElementById("contentAreaContextMenu").hidePopup();
event.stopPropagation();
event.preventDefault();
var aEngine = document.getElementById("searchbar").currentEngine;
var prop = this.checkEngine(aEngine)
if (!prop) return;
var sel = this._getselection();
switch (event.button)
{
case 1: // not
if (event.shiftKey)
{
var func = this.mFunc[prop].SL;
if (func == '') return;
var site = makeURI(this._getFocusedWindow().location.href).host;
this.addWord(func.replace('%%S', site));
}
else {
if (sel == "") return;
var func = this.mFunc[prop].NOT;
if (func == '') return;
this.addWord(func.replace('%%S', sel));
}
break;
case 2: // and (shift+ site:
if (sel == "") return;
var func = this.mFunc[prop].AND;
if (func == '') return;
this.addWord(func.replace('%%S', sel));
break;
}
this.goSearch()
break;
case "popupshowing":
this.popup();
break;
}
},
addWord: function(str){
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return;
str = searchBar.textbox.textValue + str.substring(0,1024);
searchBar.textbox.textValue = str;
searchBar.removeAttribute("empty");
searchBar._textbox.removeAttribute("isempty")
},
goSearch: function(){
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return;
var str = searchBar.textbox.textValue ;
if (this.getVer() > 2){ //Fx3
var where = "current";
}else{
var where = false;
}
searchBar.doSearch(str, where);
},
checkEngine: function(aEngine){
var aEnginename = aEngine.name
for (var prop in this.mFunc) {
if (aEnginename.indexOf(prop) != -1){
return prop;
}
}
return null;
},
popup: function(){
var searchBar = document.getElementById("searchbar");
if (!searchBar)
return;
var aEngine = searchBar.currentEngine;
var prop = this.checkEngine(aEngine)
this.setTooltip(prop);
},
setTooltip: function(prop){
var func, text = '';
if (!prop){
this.menuitem.removeAttribute("tooltiptext");
} else {
func = this.mFunc[prop].NOT;
if (func != '') text = "M:not "
func = this.mFunc[prop].AND;
if (func != '') text = text + "R:and "
func = this.mFunc[prop].SL;
if (func != '') text = text + "Shift+M:site"
this.menuitem.setAttribute("tooltiptext",text);
}
},
getVer: function(){
const Cc = Components.classes;
const Ci = Components.interfaces;
var info = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
var ver = parseInt(info.version.substr(0,3) * 10,10) / 10;
return ver;
}
}
contextAdvancedSearch.init();
window.addEventListener("unload", function(){ contextAdvancedSearch.uninit();}, false);