-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
142 lines (119 loc) · 5 KB
/
content.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
// Function to copy text to the clipboard
function copyToClipboard(text) {
const textarea = document.createElement("textarea");
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
}
function exportToCSV() {
if (window.copiedData && window.copiedData.length > 0) {
// Adding header line
const header = 'Keyword,"Search Volume","Competition"';
// Combining header and data, enclosing values in double quotes
const csvContent = `${header}\n${window.copiedData.map(data => `"${data.replace(/"/g, '""')}"`).join('\n')}`;
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "copied_data.txt");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} else {
alert("No data to export. Please copy some data first.");
}
}
// Function to export only keywords
function exportKeywords() {
if (window.copiedData && window.copiedData.length > 0) {
const keywordsOnly = window.copiedData.map(data => {
const match = data.match(/^(.*?),/);
return match ? match[1] : "";
});
const csvContent = keywordsOnly.join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8' });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("download", "keywords_only.txt");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
} else {
alert("No data to export. Please copy some data first.");
}
}
// Function to extract and copy keyword data
function copyKeywordData() {
const searchVolume = document.querySelector('[data-testid="gauge-search-volume"]');
const competition = document.querySelector('[data-testid="gauge-competition"]');
const keywordElement = document.querySelector('var');
if (searchVolume && competition && keywordElement) {
const searchVolumeText = searchVolume.innerText.trim();
const competitionText = competition.innerText.trim();
const keywordText = keywordElement.innerText.trim();
const combinedText = `${keywordText},${searchVolumeText},${competitionText}`;
// Save the copied data in a variable
if (!window.copiedData) {
window.copiedData = [];
}
window.copiedData.push(combinedText);
// Notify the user that the data has been added to the clipboard
} else {
alert("Unable to extract keyword data. Please check the page structure.");
}
}
// Function to create buttons with styling
function createButtons() {
// Copy Keyword button
const copyButton = document.createElement("button");
copyButton.innerText = "Copy Keyword";
copyButton.addEventListener("click", copyKeywordData);
copyButton.style.position = "fixed";
copyButton.style.top = "10%";
copyButton.style.left = "35%";
copyButton.style.backgroundColor = "skyblue";
copyButton.style.color = "white";
copyButton.style.borderRadius = "50px";
copyButton.style.width = "150px";
copyButton.style.height = "40px";
copyButton.style.transform = "translateY(-50%)";
copyButton.style.zIndex = "9999";
// Export All to CSV button
const exportButton = document.createElement("button");
exportButton.innerText = "Export All to CSV";
exportButton.addEventListener("click", exportToCSV);
exportButton.style.position = "fixed";
exportButton.style.top = "10%";
exportButton.style.left = "50%";
exportButton.style.color = "white";
exportButton.style.backgroundColor = "orange";
exportButton.style.borderRadius = "50px";
exportButton.style.width = "150px";
exportButton.style.height = "40px";
exportButton.style.transform = "translateY(-50%)";
exportButton.style.zIndex = "9999";
// Export Keywords Only button
const exportKeywordsButton = document.createElement("button");
exportKeywordsButton.innerText = "Export Keywords Only";
exportKeywordsButton.addEventListener("click", exportKeywords);
exportKeywordsButton.style.position = "fixed";
exportKeywordsButton.style.top = "10%";
exportKeywordsButton.style.left = "65%";
exportKeywordsButton.style.color = "white";
exportKeywordsButton.style.backgroundColor = "green";
exportKeywordsButton.style.borderRadius = "50px";
exportKeywordsButton.style.width = "200px";
exportKeywordsButton.style.height = "40px";
exportKeywordsButton.style.transform = "translateY(-50%)";
exportKeywordsButton.style.zIndex = "9999";
document.body.appendChild(copyButton);
document.body.appendChild(exportButton);
document.body.appendChild(exportKeywordsButton);
}
// Run the functions when the content script is injected
createButtons();