-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background-script.js
184 lines (152 loc) · 6.85 KB
/
background-script.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
chrome.runtime.onInstalled.addListener(function(details) {
if(details.reason == "install"){
chrome.storage.sync.set({
'hiddenItemsList': '{}'
})
console.log('Successfully initialised new install');
} else if ( details.reason == "update" ) {
console.log('Extension updated');
}
})
chrome.action.onClicked.addListener(async tab => {
var hiddenItemsList;
hiddenItemsList = await getHiddenItemsList();
await removeItems();
addButton();
readdItem();
// try {
// await chrome.scripting.insertCSS({
// target: {
// tabId: tab.id,
// },
// css: `.s-item a.s-item__link { border-style: solid; }`
// });
// } catch (error) {
// console.error('CSS insert failed! ' + error);
// }
});
chrome.webNavigation.onCompleted.addListener(function() {
chrome.notifications.create({
"type": "basic",
"iconUrl": "icons/ebay-48.png",
"title": 'eBay Item Remover',
"message": "eBay Item Remover is running - and you've just loaded an eBay page!"
});
}, {
url: [{
urlMatches : 'https://www.ebay.co.uk/'
}]
});
const getHiddenItemsList = async () => {
var hiddenItemsList;
return new Promise((resolve, reject) => {
chrome.storage.sync.get(['hiddenItemsList'], async (result) => {
hiddenItemsList = JSON.parse(result.hiddenItemsList);
hiddenItemsList = Object.values(hiddenItemsList);
resolve(hiddenItemsList);
});
});
}
async function removeItems() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
let hiddenItemsList = await getHiddenItemsList();
try {
await chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
args: [ hiddenItemsList ],
func: (hiddenItemsList) => {
document.querySelectorAll('.s-item a.s-item__link').forEach(element => {
let href = element.getAttribute('href');
href = href.split('/');
let itemID = href[href.length-1].split('?')[0];
if(hiddenItemsList.includes(parseInt(itemID))) {
console.log("Item " + itemID + " is in removelist!");
console.log(element.closest('.s-item'));
element.closest('.s-item').innerHTML = "REMOVED BY EBAY ITEM REMOVER <a href='#' class='ebayitemremover-extension_readditem' ebayitemremover-extension_itemid='" + itemID + "'>Readd</a>";
} else {
let parent = element.closest('.s-item');
console.log(itemID);
console.log(element.closest('.s-item'));
parent.insertAdjacentHTML('beforeend', '<span class="s-item__watchheart at-corner s-item__watchheart--watch ebayitemremover-extension_removeitem" ebayitemremover-extension_itemid=' + itemID + ' style="right: 30px; bottom: 10px;"><a aria-label="remove item" href="#"><span class="s-item__watchheart-icon"><svg class="icon icon--save-small" focusable="false" aria-hidden="true"><circle cx="10" cy="9" r="6.5" stroke="black" stroke-width="2" fill="none"></circle></svg><svg class="icon icon--save-selected-small" focusable="false" aria-hidden="true"><use xlink:href="#icon-save-selected-small"></use></svg></span></a></span>')
}
});
console.log(hiddenItemsList);
}
});
} catch (error) {
console.error('Get HREFs failed! ' + error);
}
}
async function addButton() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
let hiddenItemsList = await getHiddenItemsList();
try {
await chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
args: [ hiddenItemsList ],
func: (hiddenItemsList) => {
Array.from(document.getElementsByClassName('ebayitemremover-extension_removeitem')).forEach(element => {
element.addEventListener('click', (event) => {
let parent = event.target.closest('.ebayitemremover-extension_removeitem');
let itemID = parseInt(parent.getAttribute('ebayitemremover-extension_itemid'));
console.log(itemID + ' clicked');
let sureRemove = confirm('Sure you want to remove this item?');
if(sureRemove) {
hiddenItemsList[hiddenItemsList.length] = itemID;
} else {
event.preventDefault();
return;
}
chrome.storage.sync.set( {'hiddenItemsList': JSON.stringify(hiddenItemsList)}, function() {
// parent.innerHTML = "REMOVED BY EBAY ITEM REMOVER";
alert("Done\nYou need to click the extension icon for this to take effect")
} );
event.preventDefault();
});
})
}
}
);
} catch (error) {
console.log("Error adding removal button! " + error);
}
}
async function readdItem() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
let hiddenItemsList = await getHiddenItemsList();
try {
await chrome.scripting.executeScript({
target: {
tabId: tab.id,
},
args: [ hiddenItemsList ],
func: (hiddenItemsList) => {
Array.from(document.getElementsByClassName('ebayitemremover-extension_readditem')).forEach(element => {
element.addEventListener('click', (event) => {
let itemID = parseInt(event.currentTarget.getAttribute('ebayitemremover-extension_itemid'));
const index = hiddenItemsList.indexOf(itemID);
try {
hiddenItemsList.splice(index, 1);
} catch (error) {
alert("failed removing " + itemID);
}
chrome.storage.sync.set( {'hiddenItemsList': JSON.stringify(hiddenItemsList)}, function() {
alert("Done\nYou need to reload the page for this to take effect");
});
event.preventDefault();
event.preventDefault();
})
});
}
})
} catch (error) {
console.log("Error adding readd! " + error);
}
}