-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
97 lines (87 loc) · 2.99 KB
/
background.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
const baseUrl = 'https://www.keysearch.co';
function objectToFormData(obj) {
const formData = new FormData();
Object.keys(obj).forEach(key => formData.append(key, obj[key]));
return formData;
}
function makeRequest(endpoint, params) {
return fetch(`${baseUrl}${endpoint}`, {
method: 'POST',
body: objectToFormData(params)
})
.then(response => response.text())
.catch(error => console.error('Error:', error));
}
function searchKeyword(keyword) {
const params = {
keyword: keyword,
location: "all",
search_type: "M",
text_search: "search"
};
return makeRequest('/research/search', params);
}
function getFilter(keyword) {
const params = {
keyword: keyword,
location: "all",
search_type: "M",
volume_start: 150,
volume_end: "",
cpc_start: "",
cpc_end: "",
keyword_start: "",
keyword_end: "",
keyword_to_filter: "",
score_start: "",
score_end: 29,
negative: ""
};
return makeRequest('/research/get-filter', params);
}
function righttablemainresults(keyword) {
const params = {
keyword: keyword,
location: "all",
search_type: "M",
};
return makeRequest('/research/righttablemainresults', params);
}
function extractKeywordsFromHTML(htmlString) {
let parser = new DOMParser();
let doc = parser.parseFromString(htmlString, "text/html");
const keywordElements = doc.querySelectorAll('.btn-info');
const keywords = Array.from(keywordElements).map(el => el.textContent.trim());
return keywords;
}
function addKeywordsToQueue(keywords) {
chrome.storage.local.get('keywordQueue', result => {
const queue = result.keywordQueue || [];
keywords.forEach(keyword => {
if (!queue.includes(keyword)) {
queue.push(keyword);
}
});
chrome.storage.local.set({ keywordQueue: queue });
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'searchKeyword') {
const keyword = request.keyword;
searchKeyword(keyword)
.then(data => {
chrome.runtime.sendMessage({message: 'downloadData', data: data, filename: `${keyword}_search.html`, type: 'text/html'});
return righttablemainresults(keyword);
})
.then(data => {
chrome.runtime.sendMessage({message: 'downloadData', data: data, filename: `${keyword}_maintable.html`, type: 'text/html'});
// Send raw HTML to the popup for parsing
chrome.runtime.sendMessage({message: 'parseHTML', data: data});
return getFilter(keyword);
})
.then(data => {
chrome.runtime.sendMessage({message: 'downloadData', data: data, filename: `${keyword}_results.html`, type: 'text/html'});
})
.catch(error => console.error(error));
}
});