Skip to content

Commit 3a66020

Browse files
committed
Add blur listener and noFetchEval function, large clean up
1 parent 2716459 commit 3a66020

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/chrome/background.js

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ const Fetcher = function (cache) { // Pass reload to avoid cached result
129129
host = hostNew;
130130
filePath = filePath || filePathValue;
131131
func = func || fetchResolver;
132-
if (!popup.up() && !filePath.includes('.dat')) return;
133132
path = path || (!http ? 'https:' : 'http:') + '//' + www + host + '/' + filePath;
134133
fetch(path, requestInit)
135134
.then(onlyOK)
@@ -147,9 +146,8 @@ const Fetcher = function (cache) { // Pass reload to avoid cached result
147146
});
148147
};
149148

150-
//Popup API
149+
//Popup
151150
const popup = (function () {
152-
let up = 0;
153151

154152
function sellersUpdate() {
155153
(data.result.status === 0) ?
@@ -168,69 +166,57 @@ const popup = (function () {
168166
function (tabs) {
169167
if (!tabs || tabs.length === 0) return;
170168
const request = Fetcher('default');
171-
if (up) request.getDomain(tabs[0].url);
169+
request.getDomain(tabs[0].url);
172170
}
173171
);
174172
}
175173

176-
// Capture URL if loaded later than the popup was open
177-
function lateLoadURL(tabId, changeInfo, pTab) {
178-
if (changeInfo.url && pTab.active) {
179-
// Send message new fetch is upcomming
180-
popup.message('reset');
181-
const request = Fetcher('default');
182-
if (up) request.getDomain(changeInfo.url);
183-
}
184-
}
185-
186174
let saveTimeout;
175+
let tabUpdatedListener;
187176

188177
return {
189-
up: (sendResponse) => {
190-
if (sendResponse) sendResponse(up);
191-
return up;
178+
noFetchEval: function (d) {
179+
chrome.storage.local.set(d, function () {
180+
if (d.sellers !== undefined) {
181+
config.sellers = d.sellers;
182+
sellersUpdate();
183+
}
184+
});
192185
},
193186
load: function () {
194-
up = 1;
195-
chrome.storage.local.set({popupActive: up});
196-
chrome.tabs.onUpdated.addListener(lateLoadURL);
187+
if (!tabUpdatedListener) {
188+
chrome.tabs.onUpdated.addListener(getTab);
189+
tabUpdatedListener = true;
190+
}
197191
getTab();
198192
},
199-
unload: function () {
200-
up = 0;
201-
chrome.storage.local.set({popupActive: up});
202-
chrome.tabs.onUpdated.removeListener(lateLoadURL);
203-
},
204193
getSync: async function(sendResponse) {
205-
const data = await config.getSync();
206-
if (sendResponse) {
207-
sendResponse(data);
208-
}
209-
return data;
194+
const d = await config.getSync();
195+
if (sendResponse) {
196+
sendResponse(d);
197+
}
198+
return d;
210199
},
211-
saveSync: function (data) {
200+
saveSync: function (d) {
212201
if (saveTimeout) clearTimeout(saveTimeout);
213202

214203
saveTimeout = setTimeout(function() {
215-
chrome.storage.local.set(data, function () {
216-
if (data.sellers !== undefined) {
217-
config.sellers = data.sellers;
218-
if (up) sellersUpdate();
204+
chrome.storage.local.set(d, function () {
205+
if (d.sellers !== undefined) {
206+
config.sellers = d.sellers;
219207
}
220208
});
221209
}, 500);
222210
},
223211
message: function (resObj) {
224-
if (up) {
225-
try {
226-
chrome.runtime.sendMessage(resObj, () => {
227-
if (chrome.runtime.lastError) {
228-
console.error('Message recipient unavailable:', chrome.runtime.lastError);
229-
}
230-
});
231-
} catch (error) {
232-
console.error('Error sending message:', error);
233-
}
212+
try {
213+
chrome.runtime.sendMessage(resObj, () => {
214+
if (chrome.runtime.lastError) {
215+
console.log('Message recipient unavailable:', chrome.runtime.lastError);
216+
}
217+
});
218+
} catch (error) {
219+
console.log('Error sending message:', error);
234220
}
235221
},
236222
refetch: function () {
@@ -393,7 +379,7 @@ chrome.runtime.onMessage.addListener((message, _, sendResponse) => {
393379
return true;
394380
}
395381

396-
if (handler && message.action === 'saveSync') {
382+
if (handler && (message.action === 'saveSync' || message.action === 'noFetchEval')) {
397383
handler(message.data);
398384
sendResponse({received: 'true'});
399385
return true;

src/chrome/popup.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,31 @@ function waitForServiceWorker() {
218218
async function initPopup() {
219219
await waitForServiceWorker();
220220
// Now safe to send messages
221-
sendMessageWithRetry({action: 'getSync'}, enterSync);
221+
sendMessageWithRetry({ action: 'getSync' }, enterSync);
222222
}
223223

224-
document.getElementById('sellers').addEventListener('input', async (e) => {
224+
dom.sellersArea.addEventListener('input', async (e) => {
225225
const data = e.target.innerText;
226226
const cleanData = (data === `\n`) ? '' : data;
227227

228228
if (cleanData !== sellers) {
229-
await chrome.runtime.sendMessage({
229+
sendMessageWithRetry({
230230
action: 'saveSync',
231231
data: { sellers: data }
232232
});
233233
}
234234
});
235235

236+
dom.sellersArea.addEventListener('blur', async (e) => {
237+
const data = e.target.innerText;
238+
const cleanData = (data === `\n`) ? '' : data;
239+
240+
if (cleanData !== sellers) {
241+
sendMessageWithRetry({ action: 'noFetchEval', data: { sellers: cleanData }});
242+
}
243+
});
244+
245+
236246
chrome.runtime.onMessage.addListener(handleMessage);
237247

238248
initPopup();

0 commit comments

Comments
 (0)