-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
163 lines (144 loc) · 5.6 KB
/
popup.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
var mobileUserAgent =
"Mozilla/5.0 (Linux; Android 10; SM-G970F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36";
var intervalId;
var searchType;
var searchGen;
document.addEventListener("DOMContentLoaded", function () {
var form = document.getElementById("search-form");
var stopButton = document.getElementById("stop-button");
var gameFixButton = document.getElementById("game-fix-button");
// Define the webRequest listener
function modifyUserAgent(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === "User-Agent") {
details.requestHeaders[i].value = mobileUserAgent;
break;
}
}
return { requestHeaders: details.requestHeaders };
}
// Add event listeners
form.addEventListener("submit", function (event) {
event.preventDefault();
var numSearchesD = document.getElementById("num-searchesD").value;
var numSearchesM = document.getElementById("num-searchesM").value;
searchType = document.getElementById("search-type").value;
searchGen = document.querySelector('input[name="search-gen"]:checked').value;
// Add the webRequest listener if mobile search is selected
if (searchType === "mobile") {
chrome.webRequest.onBeforeSendHeaders.addListener(
modifyUserAgent,
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);
}
chrome.storage.sync.get("isSearching", function (data) {
if(data.isSearching=== "undefined"){
chrome.storage.sync.set({ isSearching: true });
}
});
chrome.storage.sync.get("isSearching", function (data) {
if (data.isSearching === true) {
console.log("TERMINATING SEARCH");
return;
} else {
console.log("SEARCHING NOW");
chrome.storage.sync.set({ isSearching: true });
// Send message to background script to start searches
chrome.runtime.sendMessage({
type: "start-searches",
numSearchesD: numSearchesD,
numSearchesM: numSearchesM,
searchType: searchType,
searchGen: searchGen,
});
}
});
// // Send message to background script to start searches
// chrome.runtime.sendMessage({
// type: "start-searches",
// numSearchesD: numSearchesD,
// numSearchesM: numSearchesM,
// searchType: searchType,
// searchGen: searchGen,
// });
// Store the interval ID so that it can be cleared later
intervalId = setInterval(function () {
// Do something at each interval, if needed
}, 1000);
});
stopButton.addEventListener("click", function (event) {
clearInterval(intervalId);
// Remove the webRequest listener if mobile search is selected
if (searchType === "mobile") {
chrome.webRequest.onBeforeSendHeaders.removeListener(modifyUserAgent);
}
// Send message to background script to stop searches
chrome.runtime.sendMessage({ type: "stop-searches" });
});
// Retrieve the last value searched by the user (if any)
chrome.storage.sync.get("lastSearchValueD", function (data) {
if (data.lastSearchValueD) {
document.getElementById("num-searchesD").value = data.lastSearchValueD;
}
});
// Retrieve the last value searched by the user (if any)
chrome.storage.sync.get("lastSearchValueM", function (data) {
if (data.lastSearchValueM) {
document.getElementById("num-searchesM").value = data.lastSearchValueM;
}
});
// Retrieve the last value searched by the user (if any)
chrome.storage.sync.get("lastTypeValue", function (data) {
if (data.lastTypeValue) {
document.getElementById("search-type").value = data.lastTypeValue;
}
});
// Retrieve the last value searched by the user (if any)
chrome.storage.sync.get("lastMethodValue", function (data) {
if (data.lastMethodValue) {
var searchGen = data.lastMethodValue;
var radioButtons = document.querySelectorAll('input[name="search-gen"]');
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].value === searchGen) {
radioButtons[i].checked = true;
break;
}
}
}
});
// Listen for form submission and save the search value to storage
document.getElementById("search-form").addEventListener("submit", function (event) {
event.preventDefault();
const numSearchesD = document.getElementById("num-searchesD").value;
const numSearchesM = document.getElementById("num-searchesM").value;
const searchType = document.getElementById("search-type").value;
const searchGen = document.querySelector('input[name="search-gen"]:checked').value;
// Save the search value to storage
chrome.storage.sync.set({ lastSearchValueD: numSearchesD });
chrome.storage.sync.set({ lastSearchValueM: numSearchesM });
chrome.storage.sync.set({ lastTypeValue: searchType });
chrome.storage.sync.set({ lastMethodValue: searchGen });
});
gameFixButton.addEventListener('click', function () {
console.log("CLICKE");
var buttonClicked = "gameFixButton";
chrome.storage.local.set({ buttonClicked: buttonClicked }, function () {
chrome.tabs.executeScript({
file: 'contentscript.js'
});
});
});
// gameButton.addEventListener('click', function() {
// chrome.tabs.executeScript({
// file: 'contentscript.js',
// code: 'var buttonClicked = "gameButton";'
// });
// });
// gameFixButton.addEventListener('click', function() {
// chrome.tabs.executeScript({
// file: 'contentscript.js',
// code: 'var buttonClicked = "gameFixButton";'
// });
// });
});