-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.js
More file actions
281 lines (241 loc) · 6.62 KB
/
main.js
File metadata and controls
281 lines (241 loc) · 6.62 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* @author David Sottimano
* @email dsottimano@gmail.com
* @twitter twitter.com/dsottimano
* @create date 2020-12-14
* @desc CLI for Google search
*/
//change the default operator character here
let defaultOperator = ";";
//keep this variable here, there's weirdness with persistance
let params = [];
//regex patterns for checking if the function should run or be cleared
let existRegex = new RegExp(`^\\${defaultOperator}.+|\\B\\${defaultOperator}.+`, 'g');
let resetPatternRegex = new RegExp(`^\\${defaultOperator}r|\\B\\${defaultOperator}r`, 'g');
let geoPatternRegex = new RegExp(`^\\${defaultOperator}g|\\B\\${defaultOperator}g`, 'g');
/**** CONFIG OBJECT ****/
//add your new commands in this object
let patternObject = [{
operator: "s",
query: null,
param: null,
info: "compares safe search. domain required for input after operator ",
fn: function(q) {
this.query = "site:" + q;
this.param = "&safe=active"
chrome.tabs.create({'url': 'https://www.google.com/search?q='+ this.query + '&safe=images' }, function(tab) {
// opened tab, what to do, what to do...
});
} ,
fnTakesInput: true
},
{
operator: "c",
query: null,
param: null,
info: "shortcut for cache: operator",
fn: function(q) {
return this.query = "cache:" + q;
},
fnTakesInput: true
},
{
operator: "f",
query: null,
param: '&filter=0',
info: "removes auto filtering of results",
fn: null, fnTakesInput: false
},
{
operator: "th",
query: null,
param: '&tbs=qdr:h',
info: "set result recency of last hour",
fn: null,
fnTakesInput: false
},
{
operator: "td",
query: null,
param: '&tbs=qdr:d',
info: "set result recency of 24 hours",
fn: null,
fnTakesInput: false
},
{
operator: "tw",
query: null,
param: '&tbs=qdr:w',
info: "set result recency of 1 week",
fn: null,
fnTakesInput: false
},
{
operator: "ty",
query: null,
param: '&tbs=qdr:y',
info: "set result recency of 1 year",
fn: null,
fnTakesInput: false
},
{
operator: "100",
query: null,
param: '&num=100',
info: "set result number to 100",
fn: null,
fnTakesInput: false
},
{
operator: "10",
query: null,
param: '&num=10',
info: "set result number to 10",
fn: null,
fnTakesInput: false
},
{
operator: "20",
query: null,
param: '&num=20',
info: "set result number to 20",
fn: null,
fnTakesInput: false
},
{
operator: "1",
query: null,
param: '&num=1',
info: "set result number to 1",
fn: null,
fnTakesInput: false
},
{
operator: "dup",
query: '-site:yoursite.com "the verbatim text to check for plagiarized content"',
param: null,
info: "example pattern to find plagiarized content",
fn: null,
fnTakesInput: false
},
{
operator: "n",
query: null,
param: '&tbm=nws',
info: "change search to news",
fn: null,
fnTakesInput: false
},
{
operator: "i",
query: null,
param: '&tbm=isch',
info: "change search to image",
fn: null,
fnTakesInput: false
},
{
operator: "v",
query: null,
param: '&tbm=vid',
info: "change search to video",
fn: null,
fnTakesInput: false
},
{
operator: "ip",
query: 'whatsmyip',
param: null,
info: "change search to video",
fn: null,
fnTakesInput: false
},
{
operator: "g",
query: null,
param: null,
info: "to change country, interface lang and results lang. /gufrende would be &gl=fr&hl=en&lr=lang_de",
fn: function (q) {
let [gl, hl, lr] = q.match(/.{1,2}/g);
let intlParamString = '';
if (gl) intlParamString += `&gl=${gl}`;
if (hl) intlParamString += `&hl=${hl}`;
if (lr) intlParamString += `&lr=lang_${lr}`;
return this.param = intlParamString
},
fnTakesInput: true
},
{
operator: "esp",
query: null,
param: '&gl=es&hl=es&lr=lang_es&uule=w+CAIQICIMbWFkcmlkIHNwYWlu',
info: "changes location to Madrid Spain, in Spanish",
fn: null,
fnTakesInput: true
}
];
let errors = []
patternObject.map(x=> {
if (x.fnTakesInput && !x.query &!x.fn) {
errors.push({name: x, error: "query, param must be null and there must be a function"})
}
})
//listens for the popup to message and sends back the entire patternObject so we can display in html
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
sendResponse({patternObject,defaultOperator,errors})
}
);
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
//get the Google search URL
let url = new URL(details.url)
//if it's not google, just end the function
if (!url.hostname.includes("google")) return
//get the user query
let q = url.searchParams.get("q");
//if the default operator is not in the query, end the function
if (!q.match(existRegex)) return
//special check for ;r, which resets parameters. If this is in the query, return a clean search page and end the function
if (q.match(resetPatternRegex)) {
q = q.replace(defaultOperator + "r", "")
params = [];
return { redirectUrl: url.origin + "/search?q=" + q.trim() };
}
//break up the query by spaces to check each part
q = q.split(' ')
for (let part = 0; part < q.length; part++) {
if (q[part].match(existRegex)) {
for (let k in patternObject) {
//pattern matching, function execution, parameter adding
let exactPattern = new RegExp(`^${defaultOperator}${patternObject[k].operator}$`, 'g')
if (q[part].match(exactPattern)) {
//these are special input functions, like /g us where the next value after the operator (us) is used for the function
if (patternObject[k].fnTakesInput) {
if (patternObject[k].fn != null) patternObject[k].fn(q[part + 1]);
if (patternObject[k].param != null) params.push(patternObject[k].param);
q[part] = q[part].replace(q[part], patternObject[k].query || "").trim();
q[part + 1] = ''.trim();
part++
break;
//standard query
} else {
if (patternObject[k].fn != null) patternObject[k].fn(q[part]);
if (patternObject[k].param != null) params.push(patternObject[k].param);
console.log(params)
q[part] = q[part].replace(q[part], patternObject[k].query || "").trim();
break;
}
}
}
} else {
continue;
}
}
//remove falsey values from the query and turn into string
let newQuery = q.filter(Boolean).join(' ');
//de-dupe the params and turn them into a string
let newParams = [...new Set(params)].join('');
return { redirectUrl: url.origin + "/search?q=" + newQuery + newParams };
},
{ urls: ["*://*/search?*"] }, ["blocking"]);