-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (51 loc) · 1.58 KB
/
main.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
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.storage.sync.get(null, function(data) {
var queryParam = '';
var keys = Object.keys(data);
if (keys.length > 0) {
for (var i = 0; i < keys.length; i++)
{
if(keys[i].includes("reactant"))
{
var reactantKey = keys[i];
var index = parseInt(reactantKey.substring(reactantKey.length - 1, reactantKey.length));
if(data['active'] == parseInt(index)) {
var productKey = "product_" + index;
queryParam = data[reactantKey] + '=' + data[productKey]
}
}
}
} else {
queryParam = 'xYextDebug=true';
}
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
var url = tabs[0].url;
var queryParams = [];
if(url.indexOf("?") > -1) {
queryParams = url.substring(url.indexOf("?"), url.length).replace("?", "").split("&");
}
if(!queryParams.includes(queryParam)) {
queryParams.push(queryParam);
} else {
var paramIndex = queryParams.indexOf(queryParam);
queryParams.splice(paramIndex, 1);
}
newUrl = "";
if(url.indexOf("?") > -1) {
newUrl = url.substring(0, url.indexOf("?"));
} else {
newUrl = url;
}
for(var i = 0; i < queryParams.length; i++) {
if (i == 0) {
newUrl = newUrl + "?" + queryParams[i];
} else {
newUrl = newUrl + "&" + queryParams[i];
}
}
chrome.tabs.update({
url: newUrl
});
});
});
});