-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
96 lines (87 loc) · 3.32 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
document.addEventListener('DOMContentLoaded', function() {
const highlightNofollowButton = document.getElementById('highlightNofollow');
const highlightDofollowButton = document.getElementById('highlightDofollow');
const exportNofollowButton = document.getElementById('exportNofollow');
const resetHighlightsButton = document.getElementById('resetHighlights');
let nofollowHighlighted = false;
let dofollowHighlighted = false;
highlightNofollowButton.addEventListener('click', function() {
if (nofollowHighlighted) {
resetHighlights();
nofollowHighlighted = false;
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: highlightNofollowLinks
});
});
nofollowHighlighted = true;
}
});
highlightDofollowButton.addEventListener('click', function() {
if (dofollowHighlighted) {
resetHighlights();
dofollowHighlighted = false;
} else {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: highlightDofollowLinks
});
});
dofollowHighlighted = true;
}
});
exportNofollowButton.addEventListener('click', function() {
exportLinks('nofollow');
});
resetHighlightsButton.addEventListener('click', function() {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: resetHighlights
});
});
nofollowHighlighted = false;
dofollowHighlighted = false;
});
function highlightNofollowLinks() {
const links = document.querySelectorAll('a[rel~="nofollow"]');
links.forEach(link => {
link.style.border = '2px solid red';
});
}
function highlightDofollowLinks() {
const links = document.querySelectorAll('a:not([rel~="nofollow"])');
links.forEach(link => {
link.style.border = '2px solid green';
});
}
function resetHighlights() {
const links = document.querySelectorAll('a');
links.forEach(link => {
link.style.border = 'none';
});
}
function exportLinks(rel) {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: function(rel) {
const links = Array.from(document.querySelectorAll(`a[rel~="${rel}"]`));
const csvContent = links.map(link => `${link.href},${link.textContent}`).join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${rel}_links.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
args: [rel]
});
});
}
});