-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
175 lines (164 loc) · 4.72 KB
/
background.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
chrome.webNavigation.onErrorOccurred.addListener(function(details) {
if(details.frameId == 0){
var info = SEManager.getSearchEngineInfo(details.url);
if(!info){return false;}
var uri = new Uri(details.url);
var gotoURL = "";
var markIndex = -1;
if(info.name && info.name.toLowerCase() == "google" &&
uri.path().toLowerCase() == "/url" && SEManager.general.tryfix){
var fixURL = uri.getQueryParamValue("url");
gotoURL = SearchEngine.decode(fixURL); //need more details
} else {
var se = SEManager.getSearchEngine(info.index);
var queryInfo = se.getQueryInfo(details.url, info.type);
if((queryInfo.content && SEManager.general.redirect)
|| (!queryInfo.content && SEManager.general.homepage)){
var begin = parseInt(uri.getQueryParamValue("SearchPlusIndex"));
if(isNaN(begin)){
begin = -1;
}
var index = SEManager.searchNextSearchIndexInOrder(info.index,
info.type, begin);
markIndex = index;
if(index != -1){
var se = SEManager.getSearchEngine(index);
gotoURL = se.getQueryURL(queryInfo);
}
}
}
if(gotoURL){
gotoURL = markSearchPlus(gotoURL);
gotoURL = markSearchEngineIndex(gotoURL, markIndex);
chrome.tabs.update(details.tabId,{url:gotoURL});
}
}
});
function checkValidSearchEngineUrl(tabId, changeInfo, tab) {
var info = null;
var msg = "Switch to";
var switchMsg = "Switch Search Engines";
if ((info = SEManager.getSearchEngineInfo(tab.url))) {
var name = SEManager.getNextSearchEngineName(info);
if(name){
msg = msg + " " + name;
chrome.pageAction.show(tabId);
var primary = SEManager.getPrimaryOptions(info.type);
if(!primary){
chrome.pageAction.setTitle({tabId : tabId, title : switchMsg});
chrome.pageAction.setPopup({tabId: tabId, popup : "popup.html"});
return ;
} else {
chrome.pageAction.setTitle({tabId : tabId, title : msg});
}
}
}
};
function markSearchEngineIndex(url, index){
if(!url) return "";
if(url.indexOf("?") == -1){
return url + "?SearchPlusIndex=" + index;
} else {
return url + "&SearchPlusIndex=" + index;
}
}
function markSearchPlus(url){
if(!url) return "";
if(url.indexOf("?") == -1){
return url + "?from=searchPlus";
} else {
return url + "&form=searchPlus";
}
}
function getQueryContent(id, name){
var value = "";
var elem = document.getElementById(id || "");
if(!elem){
elem = document.getElementsByName(name || "");
if(elem && elem.length){
elem = elem[0];
}
}
if(elem){
value = elem.value;
}
chrome.extension.sendRequest({content: value}, function() {});
}
var ActiveTabContent = "";
var ActiveProcessLock = true;
var ActiveTimer = null;
var ActiveQuery = {
id : -1,
info : {},
index : -1,
};
var waitResponseTime = 15;
function switchSearchEngine(tab, nextSEIndex){
ActiveTabContent = "";
ActiveProcessLock = true;
var info = SEManager.getSearchEngineInfo(tab.url);
if(!info || info.type == "unknown") return false;
var se = SEManager.getSearchEngine(info.index);
var inputInfo = se.getInputInfo(info.type);
ActiveQuery.info = se.getQueryInfo(tab.url, info.type);
ActiveQuery.id = tab.id;
if(nextSEIndex != undefined && nextSEIndex >= 0
&& nextSEIndex < SEManager.count()){
ActiveQuery.index = nextSEIndex;
} else {
ActiveQuery.index = SEManager.searchNextSearchIndexInOrder(info.index, info.type);
}
var contentScript = getQueryContent.toString() + "; getQueryContent(\"" +
inputInfo.id + "\", \"" + inputInfo.name + "\")";
try{
chrome.tabs.executeScript(null, {code:contentScript}, function(a){
ActiveTimer = setTimeout(function(){
if(ActiveProcessLock) {
switchURL();
}
}, waitResponseTime);
});
} catch(e){
switchURL();
}
}
function switchURL(){
clearTimeout(ActiveTimer);
if(ActiveProcessLock){
ActiveProcessLock = false;
var se = SEManager.getSearchEngine(ActiveQuery.index);
if(se){
var gotoURL = se.getQueryURL(ActiveQuery.info);
if(gotoURL){
gotoURL = markSearchPlus(gotoURL);
chrome.tabs.update(ActiveQuery.id,{url:gotoURL});
}
}
}
ActiveQuery = {
id : -1,
info : {},
index : -1
}
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if(ActiveProcessLock && request.content){
ActiveQuery.info.content = request.content;
switchURL();
}
});
chrome.tabs.onUpdated.addListener(checkValidSearchEngineUrl);
chrome.pageAction.onClicked.addListener(switchSearchEngine);
SEManager.addSearchEngine(Google);
SEManager.addSearchEngine(Amap);
SEManager.addSearchEngine(Baidu);
SEManager.addSearchEngine(Bing);
SEManager.addSearchEngine(Duckduckgo);
SEManager.addSearchEngine(QQ);
SEManager.addSearchEngine(So);
SEManager.addSearchEngine(Sogou);
SEManager.addSearchEngine(Soso);
SEManager.addSearchEngine(Youdao);
SEManager.addSearchEngine(Yahoo);
SEManager.initOptions();