-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
115 lines (96 loc) · 4.13 KB
/
Copy pathbackground.js
File metadata and controls
115 lines (96 loc) · 4.13 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
let config = {};
let allResults = [];
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('[background] Message received:', message);
if (message.type === 'startScraping') {
chrome.storage.local.set({ scrapingActive: true }, () => {
chrome.storage.local.get([
'startUrl', 'maxPage', 'mainTag', 'lockSelector', 'valueSelector'
], (items) => {
config = items;
allResults = [];
const urlObj = new URL(config.startUrl);
const searchParams = urlObj.searchParams;
let spgFound = false;
for (const [key, value] of searchParams.entries()) {
if (value.startsWith('SPG:')) {
const startNum = parseInt(value.split('SPG:')[1], 10) || 1;
searchParams.set(key, startNum.toString());
spgFound = true;
break;
}
}
if (!spgFound) {
let numParamFound = false;
for (const [key, value] of searchParams.entries()) {
if (!isNaN(parseInt(value, 10))) {
searchParams.set(key, '1');
numParamFound = true;
break;
}
}
if (!numParamFound && Object.keys(config).includes('startUrl')) {
console.warn("[background] No numeric or 'SPG:' page parameter found in startUrl. Ensure content.js can correctly infer page 1.");
}
}
const updatedStartUrl = urlObj.toString();
console.log('[background] Updated startUrl after SPG detection:', updatedStartUrl);
config.startUrl = updatedStartUrl;
chrome.tabs.update(sender.tab.id, { url: updatedStartUrl });
});
});
}
if (message.type === 'scrapedData') {
console.log(`[background] Data received from page ${message.page}:`, message.data);
chrome.storage.local.get('outputCleaner', (data) => {
let cleanedData = message.data;
const outputCleanerRegexString = data.outputCleaner;
if (outputCleanerRegexString) {
try {
const regex = new RegExp(outputCleanerRegexString, 'g');
cleanedData = message.data.map(item => {
const matches = [...item.matchAll(regex)];
return matches.map(m => m[0]).join(' ') || item;
});
console.log('[background] Data cleaned:', cleanedData);
} catch (e) {
console.warn('[background] Invalid regex from storage, skipping cleaning:', outputCleanerRegexString);
}
}
allResults.push(...cleanedData);
chrome.storage.local.set({ scrapedResults: allResults }, () => {
console.log('[background] Updated scrapedResults in chrome.storage.local');
});
chrome.runtime.sendMessage({ type: 'updateOutput', data: cleanedData });
const currentPage = message.page;
if (currentPage < Number(config.maxPage)) {
const nextPage = currentPage + 1;
console.log(`[background] Navigating to next page: ${nextPage}`);
setTimeout(() => {
const currentUrl = new URL(config.startUrl);
const searchParams = currentUrl.searchParams;
let pageParamFound = false;
for (const [key, value] of searchParams.entries()) {
if (value.startsWith('SPG:') || !isNaN(parseInt(value, 10))) {
searchParams.set(key, nextPage.toString());
pageParamFound = true;
break;
}
}
if (!pageParamFound) {
console.warn(`[background] No page parameter found in URL ${config.startUrl} to update for next page. Ensure your URL structure supports pagination.`);
}
const nextUrl = currentUrl.toString();
console.log(`[background] Next URL: ${nextUrl}`);
chrome.tabs.update(sender.tab.id, { url: nextUrl });
}, 3000);
} else {
console.log('[background] ✅ Scraping complete. Setting scrapingActive to false.');
chrome.storage.local.set({ scrapingActive: false }, () => {
chrome.tabs.sendMessage(sender.tab.id, { type: 'scrapingComplete' });
});
}
});
}
return true;
});